From 0e2dd2598a7d50da4a37696d8df4087f11839fbe Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Thu, 16 May 2024 16:26:15 -0600 Subject: [PATCH 1/5] - Prove users can provide their own proxy in custom HttpClient --- EasyPost.Tests/ClientTest.cs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/EasyPost.Tests/ClientTest.cs b/EasyPost.Tests/ClientTest.cs index 1e3427e7..440c5912 100644 --- a/EasyPost.Tests/ClientTest.cs +++ b/EasyPost.Tests/ClientTest.cs @@ -1,4 +1,5 @@ using System; +using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -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() { From 9617cc404f17b87e22ed8ed50c989fd08be993c8 Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Fri, 17 May 2024 02:40:42 -0600 Subject: [PATCH 2/5] - Handle different error for proxy test on GitHub Runner vs local --- EasyPost.Tests/ClientTest.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/EasyPost.Tests/ClientTest.cs b/EasyPost.Tests/ClientTest.cs index 440c5912..6583e054 100644 --- a/EasyPost.Tests/ClientTest.cs +++ b/EasyPost.Tests/ClientTest.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Net; using System.Net.Http; using System.Threading; @@ -138,7 +139,20 @@ public async void TestHttpClientCustomProxy() } catch (HttpRequestException e) { - Assert.Equal($"Connection refused ({proxyAddress})", e.Message); + // GitHub runner will reject the connection attempt, this is considered a pass + if (e.Message.Contains("No connection could be made because the target machine actively refused it")) + { + Assert.True(true); + } + else // Evaluate the error message + { +#if NET5_0_OR_GREATER + Assert.Equal($"Connection refused ({proxyAddress})", e.Message); +#else + // Message is inconsistent in .NET Framework, so just assert that the exception was thrown + Assert.True(true); +#endif + } } } From 2fbb4d3b0da44da0f866e14fa26a2c33956a1f95 Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Fri, 17 May 2024 04:05:55 -0600 Subject: [PATCH 3/5] - Linting --- EasyPost.Tests/ClientTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EasyPost.Tests/ClientTest.cs b/EasyPost.Tests/ClientTest.cs index 6583e054..ece22d8a 100644 --- a/EasyPost.Tests/ClientTest.cs +++ b/EasyPost.Tests/ClientTest.cs @@ -149,8 +149,8 @@ public async void TestHttpClientCustomProxy() #if NET5_0_OR_GREATER Assert.Equal($"Connection refused ({proxyAddress})", e.Message); #else - // Message is inconsistent in .NET Framework, so just assert that the exception was thrown - Assert.True(true); + // Message is inconsistent in .NET Framework, so just assert that the exception was thrown + Assert.True(true); #endif } } From 2af7f8c1813f4aa9e827c7d2a164de603d4159ba Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Fri, 17 May 2024 10:54:44 -0600 Subject: [PATCH 4/5] - Add proxy usage example to README.md --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index f7b1ff2b..d34d5403 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,28 @@ client.Hooks.OnRequestExecuting += (sender, args) => { /* ... */ }; client.Hooks.OnRequestExecuting -= OnRequestExecutingHandler; ``` +### Proxies + +If you need to use a proxy to make requests to the EasyPost API, you can define a custom `HttpClientHandler` on a custom `HttpClient` passed to the `ClientConfiguration` constructor. + +```csharp +// Define a custom HttpClientHandler with details about the proxy +HttpClientHandler handler = new() +{ + UseProxy = true, + Proxy = new WebProxy($"http://localhost:8888"), +}; + +// Define a custom HttpClient with the custom handler +HttpClient httpClient = new(handler: handler); + +// Pass the custom HttpClient to the ClientConfiguration constructor when creating a new EasyPost Client +Client client = new(new ClientConfiguration(FakeApikey) +{ + CustomHttpClient = httpClient, +}); +``` + ## Documentation API documentation can be found at: . From ec66774800ee7a953991b96543f7493d35007052 Mon Sep 17 00:00:00 2001 From: Nate Harris Date: Fri, 17 May 2024 12:02:11 -0600 Subject: [PATCH 5/5] Update README.md Co-authored-by: Junjie(Jack) Chen --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d34d5403..b2a26074 100644 --- a/README.md +++ b/README.md @@ -226,8 +226,8 @@ If you need to use a proxy to make requests to the EasyPost API, you can define // Define a custom HttpClientHandler with details about the proxy HttpClientHandler handler = new() { - UseProxy = true, - Proxy = new WebProxy($"http://localhost:8888"), + UseProxy = true, + Proxy = new WebProxy($"http://localhost:8888"), }; // Define a custom HttpClient with the custom handler