summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/test/System.Net.Http.Formatting.Test.Unit/UriExtensionsTests.cs
blob: 5172a5efd98feedea8212069b0399ef7092e7ba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Collections.Specialized;
using System.Net.Http.Formatting.DataSets;
using Microsoft.TestCommon;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Extensions;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Net.Http
{
    public class UriExtensionsTests
    {
        private static readonly Uri TestAddress = new Uri("http://www.example.com");
        private static readonly Type TestType = typeof(string);

        [Fact]
        [Trait("Description", "UriExtensionMethods is public and static.")]
        public void TypeIsCorrect()
        {
            Assert.Type.HasProperties(typeof(UriExtensions), TypeAssert.TypeProperties.IsPublicVisibleClass | TypeAssert.TypeProperties.IsStatic);
        }

        [Fact]
        [Trait("Description", "ParseQueryString(Uri) throws with null 'this'.")]
        public void ParseQueryStringThrowsWithNull()
        {
            Assert.ThrowsArgumentNull(() => ((Uri)null).ParseQueryString(), "address");
        }

        [Theory]
        [TestDataSet(typeof(HttpUnitTestDataSets), "Uris")]
        [Trait("Description", "ParseQueryString(Uri) succeeds with valid URIs.")]
        public void ParseQueryStringSucceeds(Uri address)
        {
            NameValueCollection result = address.ParseQueryString();
            Assert.NotNull(result);

            bool addressContainsQuery = address.Query.Contains("?");
            if (!addressContainsQuery)
            {
                Assert.Empty(result);
            }
            else
            {
                Assert.True(result.Count > 0, "Uri with query string should return non-empty set.");
            }
        }

        [Fact]
        [Trait("Description", "TryReadQueryAsJson(Uri, out JsonObject) throws with null 'this'.")]
        public void TryReadQueryAsJsonThrowsWithNull()
        {
            JObject value;
            Assert.ThrowsArgumentNull(() => ((Uri)null).TryReadQueryAsJson(out value), "address");
        }

        [Theory]
        [TestDataSet(typeof(HttpUnitTestDataSets), "Uris")]
        [Trait("Description", "TryReadQueryAsJson(Uri, out JsonObject) succeeds with valid URIs.")]
        public void TryReadQueryAsJsonSucceeds(Uri address)
        {
            JObject value;
            Assert.True(address.TryReadQueryAsJson(out value), "Expected 'true' as result");
            Assert.NotNull(value);
            Assert.IsType<JObject>(value);
        }

        [Fact]
        [Trait("Description", "TryReadQueryAs(Uri, Type, out object) throws with null 'this'.")]
        public void TryReadQueryAsThrowsWithNull()
        {
            object value;
            Assert.ThrowsArgumentNull(() => ((Uri)null).TryReadQueryAs(TestType, out value), "address");
            Assert.ThrowsArgumentNull(() => TestAddress.TryReadQueryAs(null, out value), "type");
        }

        [Fact]
        [Trait("Description", "TryReadQueryAs(Uri, Type, out object) succeeds with valid URIs.")]
        public void TryReadQueryAsSucceeds()
        {
            object value;
            UriBuilder address = new UriBuilder("http://some.host");

            address.Query = "a=2";
            Assert.True(address.Uri.TryReadQueryAs(typeof(SimpleObject1), out value), "Expected 'true' reading valid data");
            SimpleObject1 so1 = (SimpleObject1)value;
            Assert.NotNull(so1);
            Assert.Equal(2, so1.a);

            address.Query = "b=true";
            Assert.True(address.Uri.TryReadQueryAs(typeof(SimpleObject2), out value), "Expected 'true' reading valid data");
            SimpleObject2 so2 = (SimpleObject2)value;
            Assert.NotNull(so2);
            Assert.True(so2.b, "Value should have been true");

            address.Query = "c=hello";
            Assert.True(address.Uri.TryReadQueryAs(typeof(SimpleObject3), out value), "Expected 'true' reading valid data");
            SimpleObject3 so3 = (SimpleObject3)value;
            Assert.NotNull(so3);
            Assert.Equal("hello", so3.c);

            address.Query = "c=";
            Assert.True(address.Uri.TryReadQueryAs(typeof(SimpleObject3), out value), "Expected 'true' reading valid data");
            so3 = (SimpleObject3)value;
            Assert.NotNull(so3);
            Assert.Equal("", so3.c);

            address.Query = "c=null";
            Assert.True(address.Uri.TryReadQueryAs(typeof(SimpleObject3), out value), "Expected 'true' reading valid data");
            so3 = (SimpleObject3)value;
            Assert.NotNull(so3);
            Assert.Equal("null", so3.c);
        }

        [Fact]
        [Trait("Description", "TryReadQueryAs<T>(Uri, out T) throws with null 'this'.")]
        public void TryReadQueryAsTThrowsWithNull()
        {
            object value;
            Assert.ThrowsArgumentNull(() => ((Uri)null).TryReadQueryAs<object>(out value), "address");
        }

        [Fact]
        [Trait("Description", "TryReadQueryAs<T>(Uri, out T) succeeds with valid URIs.")]
        public void TryReadQueryAsTSucceeds()
        {
            UriBuilder address = new UriBuilder("http://some.host");
            address.Query = "a=2";
            SimpleObject1 so1;
            Assert.True(address.Uri.TryReadQueryAs<SimpleObject1>(out so1), "Expected 'true' reading valid data");
            Assert.NotNull(so1);
            Assert.Equal(2, so1.a);

            address.Query = "b=true";
            SimpleObject2 so2;
            Assert.True(address.Uri.TryReadQueryAs<SimpleObject2>(out so2), "Expected 'true' reading valid data");
            Assert.NotNull(so2);
            Assert.True(so2.b, "Value should have been true");

            address.Query = "c=hello";
            SimpleObject3 so3;
            Assert.True(address.Uri.TryReadQueryAs<SimpleObject3>(out so3), "Expected 'true' reading valid data");
            Assert.NotNull(so3);
            Assert.Equal("hello", so3.c);

            address.Query = "c=";
            Assert.True(address.Uri.TryReadQueryAs<SimpleObject3>(out so3), "Expected 'true' reading valid data");
            Assert.NotNull(so3);
            Assert.Equal("", so3.c);

            address.Query = "c=null";
            Assert.True(address.Uri.TryReadQueryAs<SimpleObject3>(out so3), "Expected 'true' reading valid data");
            Assert.NotNull(so3);
            Assert.Equal("null", so3.c);
        }


        public class SimpleObject1
        {
            public int a { get; set; }
        }

        public class SimpleObject2
        {
            public bool b { get; set; }
        }

        public class SimpleObject3
        {
            public string c { get; set; }
        }
    }
}