forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUriExtensionsTests.cs
More file actions
164 lines (139 loc) · 5.76 KB
/
Copy pathUriExtensionsTests.cs
File metadata and controls
164 lines (139 loc) · 5.76 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. 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;
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]
public void TypeIsCorrect()
{
Assert.Type.HasProperties(typeof(UriExtensions), TypeAssert.TypeProperties.IsPublicVisibleClass | TypeAssert.TypeProperties.IsStatic);
}
[Fact]
public void ParseQueryStringThrowsWithNull()
{
Assert.ThrowsArgumentNull(() => ((Uri)null).ParseQueryString(), "address");
}
[Theory]
[TestDataSet(typeof(HttpTestData), "UriTestData")]
public void ParseQueryStringSucceeds(Uri address)
{
var 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]
public void TryReadQueryAsJsonThrowsWithNull()
{
JObject value;
Assert.ThrowsArgumentNull(() => ((Uri)null).TryReadQueryAsJson(out value), "address");
}
[Theory]
[TestDataSet(typeof(HttpTestData), "UriTestData")]
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]
public void TryReadQueryAsThrowsWithNull()
{
object value;
Assert.ThrowsArgumentNull(() => ((Uri)null).TryReadQueryAs(TestType, out value), "address");
Assert.ThrowsArgumentNull(() => TestAddress.TryReadQueryAs(null, out value), "type");
}
[Fact]
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]
public void TryReadQueryAsTThrowsWithNull()
{
object value;
Assert.ThrowsArgumentNull(() => ((Uri)null).TryReadQueryAs<object>(out value), "address");
}
[Fact]
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; }
}
}
}