Skip to content

Commit

Permalink
- Prove users can provide their own proxy in custom HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
nwithan8 committed May 16, 2024
1 parent 9c1832e commit 0e2dd25
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions EasyPost.Tests/ClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -109,6 +110,38 @@ public void TestHttpClientOverride()
Assert.Equal(httpClient, overrideClient.CustomHttpClient);
}

[Fact]
public async void TestHttpClientCustomProxy()
{
const string proxyAddress = "localhost:8888";

// Define a custom proxy in a custom HttpClientHandler in a custom HttpClient
HttpClientHandler handler = new()
{
UseProxy = true,
Proxy = new WebProxy($"http://{proxyAddress}"),
};
HttpClient httpClient = new(handler: handler);

Client client = new(new ClientConfiguration(FakeApikey)
{
CustomHttpClient = httpClient,
});

Assert.Equal(httpClient, client.CustomHttpClient);

// Assert that the proxy is set in the HttpClient by attempting to make a request (should fail due to invalid proxy address)
try
{
await client.Address.Create(new Parameters.Address.Create());
Assert.Fail("Expected HttpRequestException");
}
catch (HttpRequestException e)
{
Assert.Equal($"Connection refused ({proxyAddress})", e.Message);
}
}

[Fact]
public async Task TestRequestHooks()
{
Expand Down

0 comments on commit 0e2dd25

Please sign in to comment.