Skip to content

Commit

Permalink
Fix QueryStringParser (#389)
Browse files Browse the repository at this point in the history
* Fix QueryStringParser

* add extra test
  • Loading branch information
StefH committed Dec 9, 2019
1 parent 178f2cf commit 45d8c0c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/WireMock.Net/RequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public RequestMessage([NotNull] UrlDetails urlDetails, [NotNull] string method,

Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
Cookies = cookies;
RawQuery = WebUtility.UrlDecode(urlDetails.Url.Query);
RawQuery = urlDetails.Url.Query;
Query = QueryStringParser.Parse(RawQuery);
}

Expand Down
3 changes: 2 additions & 1 deletion src/WireMock.Net/Util/QueryStringParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace WireMock.Util
{
Expand Down Expand Up @@ -30,7 +31,7 @@ string[] JoinParts(string[] parts)
.Split(new[] { '&', ';' }, StringSplitOptions.RemoveEmptyEntries) // Support "?key=value;key=anotherValue" and "?key=value&key=anotherValue"
.Select(parameter => parameter.Split(new[] { '=' }, 2, StringSplitOptions.RemoveEmptyEntries))
.GroupBy(parts => parts[0], JoinParts)
.ToDictionary(grouping => grouping.Key, grouping => new WireMockList<string>(grouping.SelectMany(x => x)));
.ToDictionary(grouping => grouping.Key, grouping => new WireMockList<string>(grouping.SelectMany(x => x).Select(WebUtility.UrlDecode)));
}
}
}
32 changes: 30 additions & 2 deletions test/WireMock.Net.Tests/Util/QueryStringParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,34 @@ public void Parse_With1ParamContainingComma()
result["key"].Should().Equal(new WireMockList<string>(new[] { "1", "2", "3" }));
}

[Fact]
public void Parse_With1ParamContainingEscapedAnd()
{
// Assign
string query = "?winkel=C%26A";

// Act
var result = QueryStringParser.Parse(query);

// Assert
result.Count.Should().Be(1);
result["winkel"].Should().Equal(new WireMockList<string>(new[] { "C&A" }));
}

[Fact]
public void Parse_With1ParamContainingParentheses()
{
// Assign
string query = "?Transaction=(123)";

// Act
var result = QueryStringParser.Parse(query);

// Assert
result.Count.Should().Be(1);
result["Transaction"].Should().Equal(new WireMockList<string>(new[] { "(123)" }));
}

[Fact]
public void Parse_WithMultipleParamWithSameKey()
{
Expand Down Expand Up @@ -227,12 +255,12 @@ public void Parse_WithComplex()

// Assert
result.Count.Should().Be(6);
result["q"].Should().Equal(new WireMockList<string>("energy+edge"));
result["q"].Should().Equal(new WireMockList<string>("energy edge"));
result["rls"].Should().Equal(new WireMockList<string>("com.microsoft:en-au"));
result["ie"].Should().Equal(new WireMockList<string>("UTF-8"));
result["oe"].Should().Equal(new WireMockList<string>("UTF-8"));
result["startIndex"].Should().Equal(new WireMockList<string>());
result["startPage"].Should().Equal(new WireMockList<string>("1%22"));
result["startPage"].Should().Equal(new WireMockList<string>("1\""));
}
}
}

0 comments on commit 45d8c0c

Please sign in to comment.