Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
Add a test to ensure UriReplacementHandler respects request options
Browse files Browse the repository at this point in the history
  • Loading branch information
NetherGranite committed Apr 20, 2024
1 parent b295919 commit 8a032df
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,4 +66,38 @@ public async Task Calls_Uri_ReplacementAsync()

mockReplacement.Verify(static x=> x.Replace(It.IsAny<Uri>()), Times.Once());
}

[Fact]
public async Task Calls_Uri_Replacement_From_Request_OptionsAsync()
{
var mockReplacement = new Mock<IUriReplacementHandlerOption>();
mockReplacement.Setup(static x => x.IsEnabled()).Returns(true);
mockReplacement.Setup(static x => x.Replace(It.IsAny<Uri>())).Returns(new Uri("http://changed"));

var handler = new UriReplacementHandler<IUriReplacementHandlerOption>()
{
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<Uri>()), Times.Once());
}

/// <summary>
/// Sets a <see cref="IRequestOption"/> in <see cref="HttpRequestMessage"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="httpRequestMessage">The <see cref="HttpRequestMessage"/> representation of the request.</param>
/// <param name="option">The request option.</param>
private static void SetRequestOption<T>(HttpRequestMessage httpRequestMessage, T option) where T : IRequestOption
{
#if NET5_0_OR_GREATER
httpRequestMessage.Options.Set(new HttpRequestOptionsKey<T>(typeof(T).FullName!), option);
#else
httpRequestMessage.Properties.Add(typeof(T).FullName!, option);
#endif
}
}

0 comments on commit 8a032df

Please sign in to comment.