1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
+ using System . Linq ;
4
5
using Xunit ;
5
6
6
7
namespace Microsoft . AspNetCore . Http . Features
@@ -12,9 +13,7 @@ public void QueryReturnsParsedQueryCollection()
12
13
{
13
14
// Arrange
14
15
var features = new FeatureCollection ( ) ;
15
- var request = new HttpRequestFeature ( ) ;
16
- request . QueryString = "foo=bar" ;
17
- features [ typeof ( IHttpRequestFeature ) ] = request ;
16
+ features [ typeof ( IHttpRequestFeature ) ] = new HttpRequestFeature { QueryString = "foo=bar" } ;
18
17
19
18
var provider = new QueryFeature ( features ) ;
20
19
@@ -25,6 +24,23 @@ public void QueryReturnsParsedQueryCollection()
25
24
Assert . Equal ( "bar" , queryCollection [ "foo" ] ) ;
26
25
}
27
26
27
+ [ Theory ]
28
+ [ InlineData ( "?key1=value1&key2=value2" ) ]
29
+ [ InlineData ( "key1=value1&key2=value2" ) ]
30
+ public void ParseQueryWithUniqueKeysWorks ( string queryString )
31
+ {
32
+ var features = new FeatureCollection ( ) ;
33
+ features [ typeof ( IHttpRequestFeature ) ] = new HttpRequestFeature { QueryString = queryString } ;
34
+
35
+ var provider = new QueryFeature ( features ) ;
36
+
37
+ var queryCollection = provider . Query ;
38
+
39
+ Assert . Equal ( 2 , queryCollection . Count ) ;
40
+ Assert . Equal ( "value1" , queryCollection [ "key1" ] . FirstOrDefault ( ) ) ;
41
+ Assert . Equal ( "value2" , queryCollection [ "key2" ] . FirstOrDefault ( ) ) ;
42
+ }
43
+
28
44
[ Theory ]
29
45
[ InlineData ( "?q" , "q" ) ]
30
46
[ InlineData ( "?q&" , "q" ) ]
@@ -34,9 +50,7 @@ public void QueryReturnsParsedQueryCollection()
34
50
public void KeyWithoutValuesAddedToQueryCollection ( string queryString , string emptyParam )
35
51
{
36
52
var features = new FeatureCollection ( ) ;
37
- var request = new HttpRequestFeature ( ) ;
38
- request . QueryString = queryString ;
39
- features [ typeof ( IHttpRequestFeature ) ] = request ;
53
+ features [ typeof ( IHttpRequestFeature ) ] = new HttpRequestFeature { QueryString = queryString } ;
40
54
41
55
var provider = new QueryFeature ( features ) ;
42
56
@@ -53,15 +67,73 @@ public void KeyWithoutValuesAddedToQueryCollection(string queryString, string em
53
67
public void EmptyKeysNotAddedToQueryCollection ( string queryString )
54
68
{
55
69
var features = new FeatureCollection ( ) ;
56
- var request = new HttpRequestFeature ( ) ;
57
- request . QueryString = queryString ;
58
- features [ typeof ( IHttpRequestFeature ) ] = request ;
70
+ features [ typeof ( IHttpRequestFeature ) ] = new HttpRequestFeature { QueryString = queryString } ;
59
71
60
72
var provider = new QueryFeature ( features ) ;
61
73
62
74
var queryCollection = provider . Query ;
63
75
64
76
Assert . Equal ( 0 , queryCollection . Count ) ;
65
77
}
78
+
79
+ [ Fact ]
80
+ public void ParseQueryWithEmptyKeyWorks ( )
81
+ {
82
+ var features = new FeatureCollection ( ) ;
83
+ features [ typeof ( IHttpRequestFeature ) ] = new HttpRequestFeature { QueryString = "?=value1&=" } ;
84
+
85
+ var provider = new QueryFeature ( features ) ;
86
+
87
+ var queryCollection = provider . Query ;
88
+
89
+ Assert . Single ( queryCollection ) ;
90
+ Assert . Equal ( new [ ] { "value1" , "" } , queryCollection [ "" ] ) ;
91
+ }
92
+
93
+ [ Fact ]
94
+ public void ParseQueryWithDuplicateKeysGroups ( )
95
+ {
96
+ var features = new FeatureCollection ( ) ;
97
+ features [ typeof ( IHttpRequestFeature ) ] = new HttpRequestFeature { QueryString = "?key1=valueA&key2=valueB&key1=valueC" } ;
98
+
99
+ var provider = new QueryFeature ( features ) ;
100
+
101
+ var queryCollection = provider . Query ;
102
+
103
+ Assert . Equal ( 2 , queryCollection . Count ) ;
104
+ Assert . Equal ( new [ ] { "valueA" , "valueC" } , queryCollection [ "key1" ] ) ;
105
+ Assert . Equal ( "valueB" , queryCollection [ "key2" ] . FirstOrDefault ( ) ) ;
106
+ }
107
+
108
+ [ Fact ]
109
+ public void ParseQueryWithEmptyValuesWorks ( )
110
+ {
111
+ var features = new FeatureCollection ( ) ;
112
+ features [ typeof ( IHttpRequestFeature ) ] = new HttpRequestFeature { QueryString = "?key1=&key2=" } ;
113
+
114
+ var provider = new QueryFeature ( features ) ;
115
+
116
+ var queryCollection = provider . Query ;
117
+
118
+ Assert . Equal ( 2 , queryCollection . Count ) ;
119
+ Assert . Equal ( string . Empty , queryCollection [ "key1" ] . FirstOrDefault ( ) ) ;
120
+ Assert . Equal ( string . Empty , queryCollection [ "key2" ] . FirstOrDefault ( ) ) ;
121
+ }
122
+
123
+ [ Theory ]
124
+ [ InlineData ( "?" ) ]
125
+ [ InlineData ( "" ) ]
126
+ [ InlineData ( null ) ]
127
+ public void ParseEmptyOrNullQueryWorks ( string queryString )
128
+ {
129
+ var features = new FeatureCollection ( ) ;
130
+ features [ typeof ( IHttpRequestFeature ) ] = new HttpRequestFeature { QueryString = queryString } ;
131
+
132
+ var provider = new QueryFeature ( features ) ;
133
+
134
+ var queryCollection = provider . Query ;
135
+
136
+ Assert . Empty ( queryCollection ) ;
137
+ }
66
138
}
67
139
}
0 commit comments