Skip to content

Commit 987e283

Browse files
author
SCHUMACHER Brice
committed
fix : (RequestBuilder.Uri.cs) Encode only query values instead of whole query string
1 parent b235b9e commit 987e283

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212

1313
### 🛠 Technical
1414

15-
- Minor bug fixes on tools (CI, csproj, etc...)
15+
- Minor bug fixes on tools (CI, csproj, etc...)
16+
17+
## 202
18+
19+
### 🐛 Bug Fixes
20+
21+
- Fixed a bug where the entire query string was encoded instead of only the query parameter keys and values.

NotoriousClient.Tests.Unit/RequestBuilderURIUnitTests.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ namespace NotoriousClient.Tests.Unit
66
public class RequestBuilderURIUnitTests
77
{
88
#region URI
9-
[GWTFact(given: "a url, an endpoint, and an HTTP Verb",
10-
when: "i build a request",
9+
[GWTFact(given: "a url, an endpoint, and an HTTP Verb",
10+
when: "i build a request",
1111
then: "request has right url, endpoint and verb")]
1212
public void RequestBuilder_Should_HaveRightUrlEndpointAndVerb()
1313
{
1414
string url = "https://toto.com";
1515
Endpoint endpoint = new Endpoint("/pandas", Method.Get);
1616

1717
RequestBuilder requestBuilder = new RequestBuilder(url, endpoint);
18-
HttpRequestMessage request = requestBuilder.Build();
18+
HttpRequestMessage request = requestBuilder.Build();
1919

2020
Assert.Equal(HttpMethod.Get, request.Method);
2121
Assert.NotNull(request.RequestUri);
22-
Assert.Equal("https://toto.com/pandas", request.RequestUri!.ToString());
22+
Assert.Equal("https://toto.com/pandas", request.RequestUri.AbsoluteUri);
2323
}
2424

2525
[GWTFact(given: "a url with and end slash, an endpoint with a start slash, and an HTTP Verb",
@@ -35,7 +35,7 @@ public void RequestBuilder_Should_HandleUrlSlashProperly()
3535

3636
Assert.Equal(HttpMethod.Get, request.Method);
3737
Assert.NotNull(request.RequestUri);
38-
Assert.Equal("https://toto.com/pandas", request.RequestUri!.ToString());
38+
Assert.Equal("https://toto.com/pandas", request.RequestUri.AbsoluteUri);
3939
}
4040

4141
[GWTFact(given: "a url, an endpoint, and an HTTP Verb",
@@ -51,7 +51,7 @@ public void RequestBuilder_Should_AddUrlSlashProperly()
5151

5252
Assert.Equal(HttpMethod.Get, request.Method);
5353
Assert.NotNull(request.RequestUri);
54-
Assert.Equal("https://toto.com/pandas", request.RequestUri!.ToString());
54+
Assert.Equal("https://toto.com/pandas", request.RequestUri.AbsoluteUri);
5555
}
5656
#endregion
5757

@@ -70,7 +70,7 @@ public void RequestBuilder_Should_HaveOneQueryParams()
7070

7171
Assert.Equal(HttpMethod.Get, request.Method);
7272
Assert.NotNull(request.RequestUri);
73-
Assert.Equal("https://toto.com/pandas?toto%3dtoto", request.RequestUri!.ToString());
73+
Assert.Equal("https://toto.com/pandas?toto=toto", request.RequestUri.AbsoluteUri);
7474
}
7575

7676
[GWTFact(given: "a request with two query parameters",
@@ -88,7 +88,24 @@ public void RequestBuilder_Should_HaveTwoQueryParams()
8888

8989
Assert.Equal(HttpMethod.Get, request.Method);
9090
Assert.NotNull(request.RequestUri);
91-
Assert.Equal("https://toto.com/pandas?toto%3dtoto%26toto2%3dtoto2", request.RequestUri!.ToString());
91+
Assert.Equal("https://toto.com/pandas?toto=toto&toto2=toto2", request.RequestUri.AbsoluteUri);
92+
}
93+
94+
[GWTFact(given: "a request with one query parameters that have an accent",
95+
when: "i build a request",
96+
then: "request has encoded query values")]
97+
public void RequestBuilder_Should_ShouldEncodeQueryValues()
98+
{
99+
string url = "https://toto.com";
100+
Endpoint endpoint = new Endpoint("/pandas", Method.Get);
101+
102+
IRequestBuilder requestBuilder = new RequestBuilder(url, endpoint)
103+
.AddQueryParameter("této", "této");
104+
HttpRequestMessage request = requestBuilder.Build();
105+
106+
Assert.Equal(HttpMethod.Get, request.Method);
107+
Assert.NotNull(request.RequestUri);
108+
Assert.Equal("https://toto.com/pandas?t%C3%A9to=t%C3%A9to", request.RequestUri.AbsoluteUri);
92109
}
93110
#endregion
94111

NotoriousClient/Builder/RequestBuilder.Uri.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Specialized;
3-
using System.Web;
1+
using System.Web;
42

53
namespace NotoriousClient.Builder
64
{
@@ -82,14 +80,14 @@ private string HandleUriQueryParams(string uri, Dictionary<string, string> query
8280
{
8381
if (queryParams.Count > 0)
8482
{
85-
string queryParamsString = string.Join("&", queryParams.Select(kvp => string.Format("{0}={1}", kvp.Key, kvp.Value)));
86-
return string.Format(uri + "?{0}", HttpUtility.UrlEncode(queryParamsString));
83+
string queryParamsString = string.Join("&", queryParams.Select(kvp => string.Format("{0}={1}", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value))));
84+
return string.Format(uri + "?{0}", queryParamsString);
8785
}
8886
else
8987
{
9088
return uri;
9189
}
92-
90+
9391
}
9492

9593
private string HandleUriEndPointParams(string uri, Dictionary<string, string> endPointParams)

NotoriousClient/NotoriousClient.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PackageId>NotoriousClient</PackageId>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8-
<VersionPrefix>2.0.1</VersionPrefix>
8+
<VersionPrefix>2.0.2</VersionPrefix>
99
<PackageReadmeFile>README.md</PackageReadmeFile>
1010
<Authors>Brice SCHUMACHER</Authors>
1111
<RepositoryUrl>https://github.com/Notorious-Coding/Notorious-Client/</RepositoryUrl>

0 commit comments

Comments
 (0)