-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added overload to method invocation to allow query string parameters …
…to be passed in via the SDK instead of being uncermoniously added to the end of the produced HttpRequestMessage URI Signed-off-by: Whit Waldo <[email protected]>
- Loading branch information
Showing
4 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// ------------------------------------------------------------------------ | ||
// Copyright 2024 The Dapr Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ------------------------------------------------------------------------ | ||
|
||
#nullable enable | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Text; | ||
|
||
namespace Dapr.Client | ||
{ | ||
/// <summary> | ||
/// Provides extensions specific to HTTP types. | ||
/// </summary> | ||
public static class HttpExtensions | ||
{ | ||
/// <summary> | ||
/// Appends key/value pairs to the query string on an HttpRequestMessage. | ||
/// </summary> | ||
/// <param name="message">The message to append the query string parameters to.</param> | ||
/// <param name="queryStringParameters">The key/value pairs to populate the query string with.</param> | ||
public static void AddQueryParameters(this HttpRequestMessage? message, | ||
IReadOnlyCollection<KeyValuePair<string, string>>? queryStringParameters) | ||
{ | ||
ArgumentNullException.ThrowIfNull(message, nameof(message)); | ||
if (queryStringParameters is null || message.RequestUri is null) | ||
return; | ||
|
||
var uriBuilder = new UriBuilder(message.RequestUri); | ||
var qsBuilder = new StringBuilder(uriBuilder.Query); | ||
foreach (var kvParam in queryStringParameters) | ||
{ | ||
if (qsBuilder.Length > 0) | ||
qsBuilder.Append('&'); | ||
qsBuilder.Append($"{Uri.EscapeDataString(kvParam.Key)}={Uri.EscapeDataString(kvParam.Value)}"); | ||
} | ||
|
||
uriBuilder.Query = qsBuilder.ToString(); | ||
message.RequestUri = uriBuilder.Uri; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using Xunit; | ||
|
||
namespace Dapr.Client.Test.Extensions | ||
{ | ||
public class HttpExtensionTest | ||
{ | ||
[Fact] | ||
public void AddQueryParameters_ReturnsEmptyQueryStringWithNullParameters() | ||
{ | ||
const string uri = "https://localhost/mypath"; | ||
var httpRq = new HttpRequestMessage(HttpMethod.Get, uri); | ||
httpRq.AddQueryParameters(null); | ||
Assert.Equal(uri, httpRq.RequestUri.AbsoluteUri); | ||
} | ||
|
||
[Fact] | ||
public void AddQueryParameters_ReturnsOriginalQueryStringWithNullParameters() | ||
{ | ||
const string uri = "https://localhost/mypath?a=0&b=1"; | ||
var httpRq = new HttpRequestMessage(HttpMethod.Get, uri); | ||
httpRq.AddQueryParameters(null); | ||
Assert.Equal(uri, httpRq.RequestUri.AbsoluteUri); | ||
} | ||
|
||
[Fact] | ||
public void AddQueryParameters_BuildsQueryString() | ||
{ | ||
var httpRq = new HttpRequestMessage(HttpMethod.Get, "https://localhost/mypath?a=0"); | ||
httpRq.AddQueryParameters(new List<KeyValuePair<string,string>> | ||
{ | ||
new("test", "value") | ||
}); | ||
Assert.Equal("https://localhost/mypath?a=0&test=value", httpRq.RequestUri.AbsoluteUri); | ||
} | ||
|
||
[Fact] | ||
public void AddQueryParameters_BuildQueryStringWithDuplicateKeys() | ||
{ | ||
var httpRq = new HttpRequestMessage(HttpMethod.Get, "https://localhost/mypath"); | ||
httpRq.AddQueryParameters(new List<KeyValuePair<string,string>> | ||
{ | ||
new("test", "1"), | ||
new("test", "2"), | ||
new("test", "3") | ||
}); | ||
Assert.Equal("https://localhost/mypath?test=1&test=2&test=3", httpRq.RequestUri.AbsoluteUri); | ||
} | ||
|
||
[Fact] | ||
public void AddQueryParameters_EscapeSpacesInValues() | ||
{ | ||
var httpRq = new HttpRequestMessage(HttpMethod.Get, "https://localhost/mypath"); | ||
httpRq.AddQueryParameters(new List<KeyValuePair<string,string>> | ||
{ | ||
new("name1", "John Doe"), | ||
new("name2", "Jane Doe") | ||
}); | ||
Assert.Equal("https://localhost/mypath?name1=John%20Doe&name2=Jane%20Doe", httpRq.RequestUri.AbsoluteUri); | ||
} | ||
} | ||
} |