From 8a032df0d3f220442451a59b96f672e32eff95b6 Mon Sep 17 00:00:00 2001 From: NetherGranite Date: Sat, 20 Apr 2024 16:22:10 -0600 Subject: [PATCH] Add a test to ensure UriReplacementHandler respects request options --- .../Middleware/UriReplacementHandlerTests.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) 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 + } }