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

Commit ca0a77e

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

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,39 @@ 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 KeyWithoutValuesAddedToQueryCollection(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.True(queryCollection.Keys.Contains(emptyParam));
44+
Assert.Equal(string.Empty, queryCollection[emptyParam]);
45+
}
46+
47+
[Fact]
48+
public void EmptyKeysNotAddedToQueryCollection()
49+
{
50+
var features = new FeatureCollection();
51+
var request = new HttpRequestFeature();
52+
request.QueryString = "&&";
53+
features[typeof(IHttpRequestFeature)] = request;
54+
55+
var provider = new QueryFeature(features);
56+
57+
var queryCollection = provider.Query;
58+
59+
Assert.Equal(0, queryCollection.Count);
60+
}
2761
}
2862
}

0 commit comments

Comments
 (0)