-
Notifications
You must be signed in to change notification settings - Fork 0
Add proxy support to NOAA clients via UseProxy option #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d0304f4
Add proxy support to NOAA clients via UseProxy option
alex1ozr 78c6333
Bind NoaaClient and Proxy options from the provided configuration
alex1ozr 71dfb23
Document breaking AddNoaaClients signature change
alex1ozr 1223789
Bump NoaaClient version to 1.3.0
alex1ozr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
142 changes: 142 additions & 0 deletions
142
tests/UnitTests/NoaaClient/ServiceCollectionExtensionsTests.cs
This file contains hidden or 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,142 @@ | ||
| using System.Net; | ||
| using System.Reflection; | ||
| using AuroraScienceHub.Integrations.NoaaClient; | ||
| using AuroraScienceHub.Integrations.NoaaClient.Rtsw; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Http; | ||
| using Shouldly; | ||
|
|
||
| namespace AuroraScienceHub.Integrations.UnitTests.NoaaClient; | ||
|
|
||
| /// <summary> | ||
| /// Tests for <see cref="ServiceCollectionExtensions"/> proxy configuration. | ||
| /// </summary> | ||
| public sealed class ServiceCollectionExtensionsTests | ||
| { | ||
| private static readonly Uri ProxyAddress = new("http://proxy.example.com:8080"); | ||
|
|
||
| [Fact(DisplayName = "Noaa clients do not use a custom proxy when UseProxy is not enabled")] | ||
| public void AddNoaaClients_WhenUseProxyDisabled_ConfiguresClientWithoutProxy() | ||
| { | ||
| // Arrange / Act | ||
| var (_, proxy) = CreatePrimaryHandlerProxy(CreateConfiguration(useProxy: false), typeof(IRtswClient)); | ||
|
|
||
| // Assert | ||
| proxy.ShouldBeNull(); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Noaa clients route through the configured proxy when UseProxy is enabled")] | ||
| public void AddNoaaClients_WhenUseProxyEnabled_ConfiguresProxyAddress() | ||
| { | ||
| // Arrange / Act | ||
| var (useProxy, proxy) = CreatePrimaryHandlerProxy( | ||
| CreateConfiguration(useProxy: true, address: ProxyAddress), | ||
| typeof(IRtswClient)); | ||
|
|
||
| // Assert | ||
| useProxy.ShouldBeTrue(); | ||
| var webProxy = proxy.ShouldBeOfType<WebProxy>(); | ||
| webProxy.Address.ShouldBe(ProxyAddress); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Noaa clients apply proxy credentials when configured")] | ||
| public void AddNoaaClients_WhenProxyCredentialsConfigured_AppliesCredentials() | ||
| { | ||
| // Arrange / Act | ||
| var (_, proxy) = CreatePrimaryHandlerProxy( | ||
| CreateConfiguration(useProxy: true, address: ProxyAddress, userName: "user", password: "pass"), | ||
| typeof(IRtswClient)); | ||
|
|
||
| // Assert | ||
| var webProxy = proxy.ShouldBeOfType<WebProxy>(); | ||
| var credentials = webProxy.Credentials.ShouldBeOfType<NetworkCredential>(); | ||
| credentials.UserName.ShouldBe("user"); | ||
| credentials.Password.ShouldBe("pass"); | ||
| webProxy.UseDefaultCredentials.ShouldBeFalse(); | ||
| } | ||
|
|
||
| [Fact(DisplayName = "Noaa clients fail fast when UseProxy is enabled but the proxy address is missing")] | ||
| public void AddNoaaClients_WhenUseProxyEnabledWithoutAddress_ThrowsInvalidOperationException() | ||
| { | ||
| // Arrange / Act | ||
| var exception = Should.Throw<InvalidOperationException>( | ||
| () => CreatePrimaryHandlerProxy(CreateConfiguration(useProxy: true), typeof(IRtswClient))); | ||
|
|
||
| // Assert | ||
| exception.Message.ShouldContain("Proxy"); | ||
| } | ||
|
|
||
| private static (bool UseProxy, IWebProxy? Proxy) CreatePrimaryHandlerProxy( | ||
| IConfiguration configuration, | ||
| Type clientType) | ||
| { | ||
| var services = new ServiceCollection(); | ||
| services.AddNoaaClients(configuration); | ||
|
|
||
| using var provider = services.BuildServiceProvider(); | ||
|
|
||
| var handlerFactory = provider.GetRequiredService<IHttpMessageHandlerFactory>(); | ||
| var handler = handlerFactory.CreateHandler(clientType.Name); | ||
| var primaryHandler = UnwrapPrimaryHandler(handler); | ||
|
|
||
| return primaryHandler switch | ||
| { | ||
| HttpClientHandler httpClientHandler => (httpClientHandler.UseProxy, httpClientHandler.Proxy), | ||
| SocketsHttpHandler socketsHandler => (socketsHandler.UseProxy, socketsHandler.Proxy), | ||
| _ => throw new InvalidOperationException( | ||
| $"Unexpected primary handler type {primaryHandler.GetType().Name}."), | ||
| }; | ||
| } | ||
|
|
||
| private static HttpMessageHandler UnwrapPrimaryHandler(HttpMessageHandler handler) | ||
| { | ||
| const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; | ||
|
|
||
| while (true) | ||
| { | ||
| if (handler is HttpClientHandler or SocketsHttpHandler) | ||
| { | ||
| return handler; | ||
| } | ||
|
|
||
| var innerHandlerProperty = handler.GetType().GetProperty("InnerHandler", flags) | ||
| ?? throw new InvalidOperationException( | ||
| $"Cannot unwrap message handler of type {handler.GetType().Name}."); | ||
|
|
||
| handler = (HttpMessageHandler)innerHandlerProperty.GetValue(handler)!; | ||
| } | ||
| } | ||
|
|
||
| private static IConfiguration CreateConfiguration( | ||
| bool useProxy, | ||
| Uri? address = null, | ||
| string? userName = null, | ||
| string? password = null) | ||
| { | ||
| var data = new Dictionary<string, string?> | ||
| { | ||
| [$"{NoaaClientOptions.OptionKey}:ServerUrl"] = "https://noaa.test", | ||
| [$"{NoaaClientOptions.OptionKey}:UseProxy"] = useProxy.ToString(), | ||
| }; | ||
|
|
||
| if (address is not null) | ||
| { | ||
| data["Proxy:Address"] = address.ToString(); | ||
| } | ||
|
|
||
| if (userName is not null) | ||
| { | ||
| data["Proxy:UserName"] = userName; | ||
| } | ||
|
|
||
| if (password is not null) | ||
| { | ||
| data["Proxy:Password"] = password; | ||
| } | ||
|
|
||
| return new ConfigurationBuilder() | ||
| .AddInMemoryCollection(data) | ||
| .Build(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.