Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/RestClient/Authenticators/OAuth/src/OAuthAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClickView.Extensions.RestClient.Authenticators.OAuth;
namespace ClickView.Extensions.RestClient.Authenticators.OAuth;

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -56,7 +56,7 @@ public virtual async Task AuthenticateAsync(IClientRequest request, Cancellation
return;
}

request.AddHeader("Authorization", "Bearer " + oAuthToken);
request.AddOrUpdateHeader("Authorization", "Bearer " + oAuthToken);
}

protected void AddTokenSource(ITokenSource tokenSource)
Expand Down Expand Up @@ -116,4 +116,4 @@ protected IAuthenticatorEndpointFactory CreateEndpointFactory(string endpoint)

return new DefaultEndpointFactory(Options.Endpoints);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClickView.Extensions.RestClient.Authentication;
namespace ClickView.Extensions.RestClient.Authentication;

using System;
using System.Text;
Expand All @@ -24,7 +24,7 @@ public BasicAccessAuthenticator(string userId, string password)

public Task AuthenticateAsync(IClientRequest request, CancellationToken token = default)
{
request.AddHeader("Authorization", $"Basic {_encoded}");
request.AddOrUpdateHeader("Authorization", $"Basic {_encoded}");
return Task.CompletedTask;
}

Expand All @@ -33,4 +33,4 @@ private static string ToBase64(string s)
var bytes = Encoding.UTF8.GetBytes(s);
return Convert.ToBase64String(bytes);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClickView.Extensions.RestClient.Authentication;
namespace ClickView.Extensions.RestClient.Authentication;

using System.Threading;
using System.Threading.Tasks;
Expand All @@ -20,8 +20,8 @@ public HeaderAuthenticator(string key, string value)

public Task AuthenticateAsync(IClientRequest request, CancellationToken token = default)
{
request.AddHeader(_key, _value);
request.AddOrUpdateHeader(_key, _value);

return Task.CompletedTask;
}
}
}
14 changes: 13 additions & 1 deletion src/RestClient/RestClient/src/Requests/BaseRestClientRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public void AddHeader(string key, IEnumerable<string> values)
_headers.Add(key, values);
}

public void AddOrUpdateHeader(string key, string value)
{
_headers.Remove(key); // no-op if it does not exist
_headers.Add(key, value);
}

public void AddOrUpdateHeader(string key, IEnumerable<string> values)
{
_headers.Remove(key); // no-op if it does not exist
_headers.Add(key, values);
}

public void AddBody(object body)
{
if (!MethodSupportsBody(Method))
Expand All @@ -53,7 +65,7 @@ public void AddBody(object body)
_content = body;
return;

bool MethodSupportsBody(HttpMethod method)
static bool MethodSupportsBody(HttpMethod method)
{
return method == HttpMethod.Post ||
#if NETCOREAPP2_1_OR_GREATER
Expand Down
4 changes: 3 additions & 1 deletion src/RestClient/RestClient/src/Requests/IClientRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public interface IClientRequest

void AddHeader(string key, string value);
void AddHeader(string key, IEnumerable<string> values);
void AddOrUpdateHeader(string key, string value);
void AddOrUpdateHeader(string key, IEnumerable<string> values);
void AddBody(object body);
}
}
1 change: 0 additions & 1 deletion src/RestClient/RestClient/src/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ private static HttpClient CreateClient(RestClientOptions options)

PrepareHttpClient(httpClient, options);


return httpClient;
}

Expand Down
26 changes: 26 additions & 0 deletions src/RestClient/RestClient/test/RestClientRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ public void AddBody_GetRequest_ThrowsException()
Assert.Equal("Cannot add body to GET", ex.Message);
}

[Fact]
public void AddOrUpdateHeader_SingleValue_SameHeaderTwice_DoesNotThrow()
{
const string key = "X-Test-Header";
const string value = "TestValue";

var request = new RestClientRequest(HttpMethod.Post, "test");
request.AddOrUpdateHeader(key, value);
request.AddOrUpdateHeader(key, value);

Assert.Single(request.Headers);
}

[Fact]
public void AddOrUpdateHeader_MultipleValues_SameHeaderTwice_DoesNotThrow()
{
const string key = "X-Test-Header";
string[] values = ["TestValue1", "TestValue2"];

var request = new RestClientRequest(HttpMethod.Post, "test");
request.AddOrUpdateHeader(key, values);
request.AddOrUpdateHeader(key, values);

Assert.Single(request.Headers);
}

private class TestSerializer : ISerializer
{
public string Format => "test";
Expand Down
4 changes: 2 additions & 2 deletions src/RestClient/RestClient/test/RestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public async Task ExecuteAsync_DefaultUserAgentSet_RequestContainsDefaultUserAge
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>()
)
.Callback<HttpRequestMessage, CancellationToken>((request, token) =>
.Callback<HttpRequestMessage, CancellationToken>((request, _) =>
{
userAgentHeader = request.Headers.UserAgent.ToString();
})
Expand Down Expand Up @@ -244,4 +244,4 @@ protected override async ValueTask<RestClientResponse<string>> ParseResponseAsyn
return new RestClientResponse<string>(message, await message.Content.ReadAsStringAsync());
}
}
}
}
Loading