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

Commit e854d3a

Browse files
author
moozzyk
committed
Allow query string parameters without values
Addresses #624
1 parent 440c6e4 commit e854d3a

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Microsoft.AspNetCore.WebUtilities/QueryHelpers.cs

+7-1
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

+39
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,44 @@ 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+
[InlineData("?q=", "q")]
33+
[InlineData("?q=&", "q")]
34+
public void KeyWithoutValuesAddedToQueryCollection(string queryString, string emptyParam)
35+
{
36+
var features = new FeatureCollection();
37+
var request = new HttpRequestFeature();
38+
request.QueryString = queryString;
39+
features[typeof(IHttpRequestFeature)] = request;
40+
41+
var provider = new QueryFeature(features);
42+
43+
var queryCollection = provider.Query;
44+
45+
Assert.True(queryCollection.Keys.Contains(emptyParam));
46+
Assert.Equal(string.Empty, queryCollection[emptyParam]);
47+
}
48+
49+
[Theory]
50+
[InlineData("?&&")]
51+
[InlineData("?&")]
52+
[InlineData("&&")]
53+
public void EmptyKeysNotAddedToQueryCollection(string queryString)
54+
{
55+
var features = new FeatureCollection();
56+
var request = new HttpRequestFeature();
57+
request.QueryString = queryString;
58+
features[typeof(IHttpRequestFeature)] = request;
59+
60+
var provider = new QueryFeature(features);
61+
62+
var queryCollection = provider.Query;
63+
64+
Assert.Equal(0, queryCollection.Count);
65+
}
2766
}
2867
}

0 commit comments

Comments
 (0)