Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 169312a

Browse files
author
moozzyk
committed
Allow query string parameters without values
Addresses #624
1 parent 8f233ea commit 169312a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Microsoft.AspNetCore.WebUtilities/QueryHelpers.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ public static Dictionary<string, StringValues> ParseNullableQuery(string querySt
140140
scanIndex = 1;
141141
}
142142

143-
144143
int textLength = queryString.Length;
145144
int equalIndex = queryString.IndexOf('=');
146145
if (equalIndex == -1)
@@ -171,6 +170,13 @@ public static Dictionary<string, StringValues> ParseNullableQuery(string querySt
171170
equalIndex = textLength;
172171
}
173172
}
173+
else
174+
{
175+
if (delimiterIndex > scanIndex)
176+
{
177+
accumulator.Append(queryString.Substring(scanIndex, delimiterIndex - scanIndex), string.Empty);
178+
}
179+
}
174180
scanIndex = delimiterIndex + 1;
175181
}
176182

test/Microsoft.AspNetCore.Http.Tests/Features/QueryFeatureTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,38 @@ public void QueryReturnsParsedQueryCollection()
2424
// Assert
2525
Assert.Equal("bar", queryCollection["foo"]);
2626
}
27+
28+
[Theory]
29+
[InlineData("?q", "q")]
30+
[InlineData("?q&", "q")]
31+
[InlineData("?q1=abc&q2", "q2")]
32+
public void QueryCollection_TEST(string queryString, string emptyParam)
33+
{
34+
var features = new FeatureCollection();
35+
var request = new HttpRequestFeature();
36+
request.QueryString = queryString;
37+
features[typeof(IHttpRequestFeature)] = request;
38+
39+
var provider = new QueryFeature(features);
40+
41+
var queryCollection = provider.Query;
42+
43+
Assert.Equal(string.Empty, queryCollection[emptyParam]);
44+
}
45+
46+
[Fact]
47+
public void QueryCollection_TEST2()
48+
{
49+
var features = new FeatureCollection();
50+
var request = new HttpRequestFeature();
51+
request.QueryString = "&&";
52+
features[typeof(IHttpRequestFeature)] = request;
53+
54+
var provider = new QueryFeature(features);
55+
56+
var queryCollection = provider.Query;
57+
58+
Assert.Equal(0, queryCollection.Count);
59+
}
2760
}
2861
}

0 commit comments

Comments
 (0)