Skip to content

Commit a6353d1

Browse files
committed
Port over tests from QueryHelpersTests + fixes
1 parent 3685ee2 commit a6353d1

File tree

2 files changed

+88
-23
lines changed

2 files changed

+88
-23
lines changed

src/Http/Http/src/Features/QueryFeature.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,9 @@ public IQueryCollection Query
7373

7474
var result = ParseNullableQueryInternal(current);
7575

76-
if (result == null)
77-
{
78-
_parsedValues = QueryCollection.Empty;
79-
}
80-
else
81-
{
82-
_parsedValues = new QueryCollectionInternal(result);
83-
}
76+
_parsedValues = result is not null
77+
? new QueryCollectionInternal(result)
78+
: QueryCollection.Empty;
8479
}
8580
return _parsedValues;
8681
}
@@ -165,7 +160,10 @@ public IQueryCollection Query
165160
}
166161
else
167162
{
168-
accumulator.Append(querySegment);
163+
if (!querySegment.IsEmpty)
164+
{
165+
accumulator.Append(querySegment);
166+
}
169167
}
170168

171169
if (delimiterIndex < 0)
@@ -207,11 +205,6 @@ public void Append(ReadOnlySpan<char> key, ReadOnlySpan<char> value = default)
207205
/// </summary>
208206
public void Append(string key, string value)
209207
{
210-
if (key.Length == 0)
211-
{
212-
return;
213-
}
214-
215208
if (_accumulator is null)
216209
{
217210
_accumulator = new AdaptiveCapacityDictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase);

src/Http/Http/test/Features/QueryFeatureTests.cs

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Linq;
45
using Xunit;
56

67
namespace Microsoft.AspNetCore.Http.Features
@@ -12,9 +13,7 @@ public void QueryReturnsParsedQueryCollection()
1213
{
1314
// Arrange
1415
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" };
1817

1918
var provider = new QueryFeature(features);
2019

@@ -25,6 +24,23 @@ public void QueryReturnsParsedQueryCollection()
2524
Assert.Equal("bar", queryCollection["foo"]);
2625
}
2726

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+
2844
[Theory]
2945
[InlineData("?q", "q")]
3046
[InlineData("?q&", "q")]
@@ -34,9 +50,7 @@ public void QueryReturnsParsedQueryCollection()
3450
public void KeyWithoutValuesAddedToQueryCollection(string queryString, string emptyParam)
3551
{
3652
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 };
4054

4155
var provider = new QueryFeature(features);
4256

@@ -53,15 +67,73 @@ public void KeyWithoutValuesAddedToQueryCollection(string queryString, string em
5367
public void EmptyKeysNotAddedToQueryCollection(string queryString)
5468
{
5569
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 };
5971

6072
var provider = new QueryFeature(features);
6173

6274
var queryCollection = provider.Query;
6375

6476
Assert.Equal(0, queryCollection.Count);
6577
}
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+
}
66138
}
67139
}

0 commit comments

Comments
 (0)