Skip to content

Commit

Permalink
Updated to add authentication client to the service builder for the t…
Browse files Browse the repository at this point in the history
…ests

Updated to add authentication client to the service builder for the tests.  All tests now passing.
  • Loading branch information
vaughanknight committed Jun 18, 2024
1 parent 9aabd27 commit 8127a92
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private async Task<Stream> GetStreamWithAuthRetryAsync(string uriPath)

private async Task EnsureTokenAsync()
{
if (this._authenticationClient.DefaultRequestHeaders.Authorization == null)
if (this._client.DefaultRequestHeaders.Authorization == null)
{
await this.UpdateAuthTokenAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private static void AddWattTimeClient(IServiceCollection services, IConfiguratio
});

var httpClientBuilder = services.AddHttpClient<WattTimeClient>(IWattTimeClient.NamedClient);
var authenticationClientBuilder = services.AddHttpClient<WattTimeClient>(IWattTimeClient.NamedAuthenticationClient);

var Proxy = configSection.GetSection("Proxy").Get<WebProxyConfiguration>();
if (Proxy != null && Proxy.UseProxy == true)
Expand All @@ -47,15 +48,18 @@ private static void AddWattTimeClient(IServiceCollection services, IConfiguratio
{
throw new Exceptions.ConfigurationException("Proxy Url is not configured.");
}
httpClientBuilder.ConfigurePrimaryHttpMessageHandler(() =>
new HttpClientHandler() {
Proxy = new WebProxy {
Address = new Uri(Proxy.Url),
Credentials = new NetworkCredential(Proxy.Username, Proxy.Password),
BypassProxyOnLocal = true
}
var handler = new HttpClientHandler()
{
Proxy = new WebProxy
{
Address = new Uri(Proxy.Url),
Credentials = new NetworkCredential(Proxy.Username, Proxy.Password),
BypassProxyOnLocal = true
}
);
};

httpClientBuilder.ConfigurePrimaryHttpMessageHandler(() => handler );
authenticationClientBuilder.ConfigurePrimaryHttpMessageHandler(() => handler );
}
services.TryAddSingleton<IWattTimeClient, WattTimeClient>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ class ServiceCollectionExtensionTests
private readonly string ProxyPassword = $"DataSources:Configurations:WattTimeTest:Proxy:Password";
private readonly string UseProxyKey = $"DataSources:Configurations:WattTimeTest:Proxy:UseProxy";



[Test]
public void ClientProxyTest_With_Invalid_ProxyURL_ThrowsException()
public void ClientProxyTest_With_Missing_ProxyURL_ThrowsException()
{
// Arrange
var settings = new Dictionary<string, string> {
{ ForecastDataSourceKey, ForecastDataSourceValue },
{ EmissionsDataSourceKey, EmissionsDataSourceValue },
{ UsernameKey, Username },
{ PasswordKey, Password },
{ ProxyUrl, "http://fakeproxy:8080" },
{ UseProxyKey, "true" },
};

Expand All @@ -44,23 +45,22 @@ public void ClientProxyTest_With_Invalid_ProxyURL_ThrowsException()
.AddEnvironmentVariables()
.Build();
var serviceCollection = new ServiceCollection();
serviceCollection.AddWattTimeForecastDataSource(configuration.DataSources());
var serviceProvider = serviceCollection.BuildServiceProvider();
var client = serviceProvider.GetRequiredService<IWattTimeClient>();

// Act & Assert
Assert.ThrowsAsync<HttpRequestException>(async () => await client.GetRegionAsync("lat", "long"));
Assert.Throws<ConfigurationException>(() => serviceCollection.AddWattTimeForecastDataSource(configuration.DataSources()));
Assert.Throws<ConfigurationException>(() => serviceCollection.AddWattTimeEmissionsDataSource(configuration.DataSources()));
}

[Test]
public void ClientProxyTest_With_Missing_ProxyURL_ThrowsException()
public void ClientProxyTest_With_Invalid_ProxyURL_ThrowsException()
{
// Arrange
var settings = new Dictionary<string, string> {
{ ForecastDataSourceKey, ForecastDataSourceValue },
{ EmissionsDataSourceKey, EmissionsDataSourceValue },
{ UsernameKey, Username },
{ PasswordKey, Password },
{ ProxyUrl, "http://fakeproxy:8080" },
{ UseProxyKey, "true" },
};

Expand All @@ -69,10 +69,13 @@ public void ClientProxyTest_With_Missing_ProxyURL_ThrowsException()
.AddEnvironmentVariables()
.Build();
var serviceCollection = new ServiceCollection();
serviceCollection.AddWattTimeForecastDataSource(configuration.DataSources());

var serviceProvider = serviceCollection.BuildServiceProvider();
var client = serviceProvider.GetRequiredService<IWattTimeClient>();

// Act & Assert
Assert.Throws<ConfigurationException>(() => serviceCollection.AddWattTimeForecastDataSource(configuration.DataSources()));
Assert.Throws<ConfigurationException>(() => serviceCollection.AddWattTimeEmissionsDataSource(configuration.DataSources()));
Assert.ThrowsAsync<HttpRequestException>(async () => await client.GetRegionAsync("lat", "long"));
}

[TestCase(true, TestName = "ClientProxyTest, successful: denotes adding WattTime data sources using proxy.")]
Expand Down

0 comments on commit 8127a92

Please sign in to comment.