diff --git a/CHANGELOG.md b/CHANGELOG.md index 907525b..d462a63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.3.12] - 2024-04-22 + +- UriReplacementHandler improvements to be added to middleware pipeline by default and respects options set in the HttpRequestMessage (https://github.com/microsoft/kiota-http-dotnet/issues/242) - Adds `ConfigureAwait(false)` calls to async calls (https://github.com/microsoft/kiota-http-dotnet/issues/240). ## [1.3.11] - 2024-04-19 diff --git a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UriReplacementHandlerTests.cs b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UriReplacementHandlerTests.cs index 02df8ce..293edf4 100644 --- a/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UriReplacementHandlerTests.cs +++ b/Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/UriReplacementHandlerTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.Kiota.Abstractions; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; using Moq; @@ -65,4 +66,38 @@ public async Task Calls_Uri_ReplacementAsync() mockReplacement.Verify(static x=> x.Replace(It.IsAny()), Times.Once()); } + + [Fact] + public async Task Calls_Uri_Replacement_From_Request_OptionsAsync() + { + var mockReplacement = new Mock(); + mockReplacement.Setup(static x => x.IsEnabled()).Returns(true); + mockReplacement.Setup(static x => x.Replace(It.IsAny())).Returns(new Uri("http://changed")); + + var handler = new UriReplacementHandler() + { + InnerHandler = new FakeSuccessHandler() + }; + var msg = new HttpRequestMessage(HttpMethod.Get, "http://localhost"); + SetRequestOption(msg, mockReplacement.Object); + var client = new HttpClient(handler); + await client.SendAsync(msg); + + mockReplacement.Verify(static x=> x.Replace(It.IsAny()), Times.Once()); + } + + /// + /// Sets a in . + /// + /// + /// The representation of the request. + /// The request option. + private static void SetRequestOption(HttpRequestMessage httpRequestMessage, T option) where T : IRequestOption + { +#if NET5_0_OR_GREATER + httpRequestMessage.Options.Set(new HttpRequestOptionsKey(typeof(T).FullName!), option); +#else + httpRequestMessage.Properties.Add(typeof(T).FullName!, option); +#endif + } } diff --git a/src/KiotaClientFactory.cs b/src/KiotaClientFactory.cs index 5dd9498..6c7d1ec 100644 --- a/src/KiotaClientFactory.cs +++ b/src/KiotaClientFactory.cs @@ -8,6 +8,7 @@ using System.Net.Http; using Microsoft.Kiota.Abstractions.Authentication; using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; +using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; namespace Microsoft.Kiota.Http.HttpClientLibrary { @@ -36,6 +37,7 @@ public static IList CreateDefaultHandlers() return new List { //add the default middlewares as they are ready + new UriReplacementHandler(), new RetryHandler(), new RedirectHandler(), new ParametersNameDecodingHandler(), diff --git a/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj b/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj index 99406a3..c36fcbc 100644 --- a/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj +++ b/src/Microsoft.Kiota.Http.HttpClientLibrary.csproj @@ -15,7 +15,7 @@ https://aka.ms/kiota/docs true true - 1.3.11 + 1.3.12 true diff --git a/src/Middleware/UriReplacementHandler.cs b/src/Middleware/UriReplacementHandler.cs index c1fe66d..4bb2fe3 100644 --- a/src/Middleware/UriReplacementHandler.cs +++ b/src/Middleware/UriReplacementHandler.cs @@ -12,21 +12,29 @@ namespace Microsoft.Kiota.Http.HttpClientLibrary.Middleware; /// A type with the rules used to perform a URI replacement. public class UriReplacementHandler : DelegatingHandler where TUriReplacementHandlerOption : IUriReplacementHandlerOption { - private readonly TUriReplacementHandlerOption uriReplacement; + private readonly TUriReplacementHandlerOption? _uriReplacement; /// /// Creates a new UriReplacementHandler. /// /// An object with the URI replacement rules. - public UriReplacementHandler(TUriReplacementHandlerOption uriReplacement) + public UriReplacementHandler(TUriReplacementHandlerOption? uriReplacement = default) { - this.uriReplacement = uriReplacement; + this._uriReplacement = uriReplacement; } /// protected override async Task SendAsync( HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { + var uriReplacement = request.GetRequestOption() ?? _uriReplacement; + + // If there is no URI replacement to apply, then just skip this handler. + if(uriReplacement is null) + { + return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); + } + Activity? activity; if(request.GetRequestOption() is { } obsOptions) {