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

Commit

Permalink
Allow query string parameters without values
Browse files Browse the repository at this point in the history
Addresses #624
  • Loading branch information
moozzyk committed May 23, 2016
1 parent 8f233ea commit 675df65
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Microsoft.AspNetCore.WebUtilities/QueryHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ public static Dictionary<string, StringValues> ParseNullableQuery(string querySt
scanIndex = 1;
}


int textLength = queryString.Length;
int equalIndex = queryString.IndexOf('=');
if (equalIndex == -1)
Expand Down Expand Up @@ -171,6 +170,13 @@ public static Dictionary<string, StringValues> ParseNullableQuery(string querySt
equalIndex = textLength;
}
}
else
{
if (delimiterIndex > scanIndex)
{
accumulator.Append(queryString.Substring(scanIndex, delimiterIndex - scanIndex), string.Empty);
}
}
scanIndex = delimiterIndex + 1;
}

Expand Down
39 changes: 39 additions & 0 deletions test/Microsoft.AspNetCore.Http.Tests/Features/QueryFeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,44 @@ public void QueryReturnsParsedQueryCollection()
// Assert
Assert.Equal("bar", queryCollection["foo"]);
}

[Theory]
[InlineData("?q", "q")]
[InlineData("?q&", "q")]
[InlineData("?q1=abc&q2", "q2")]
[InlineData("?q=", "q")]
[InlineData("?q=&", "q")]
public void KeyWithoutValuesAddedToQueryCollection(string queryString, string emptyParam)
{
var features = new FeatureCollection();
var request = new HttpRequestFeature();
request.QueryString = queryString;
features[typeof(IHttpRequestFeature)] = request;

var provider = new QueryFeature(features);

var queryCollection = provider.Query;

Assert.True(queryCollection.Keys.Contains(emptyParam));
Assert.Equal(string.Empty, queryCollection[emptyParam]);
}

[Theory]
[InlineData("?&&")]
[InlineData("?&")]
[InlineData("&&")]
public void EmptyKeysNotAddedToQueryCollection(string queryString)
{
var features = new FeatureCollection();
var request = new HttpRequestFeature();
request.QueryString = queryString;
features[typeof(IHttpRequestFeature)] = request;

var provider = new QueryFeature(features);

var queryCollection = provider.Query;

Assert.Equal(0, queryCollection.Count);
}
}
}

0 comments on commit 675df65

Please sign in to comment.