Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions APIMatic.Core.Test/GlobalConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using APIMatic.Core.Http.Configuration;
using APIMatic.Core.Proxy;
using NUnit.Framework;

namespace APIMatic.Core.Test
Expand Down Expand Up @@ -44,16 +45,29 @@ public async Task TestGlobalRequestHeaders()
[Test]
public async Task TestGlobalRequest_NullUserAgent()
{
var httpClientConfiguration = new CoreHttpClientConfiguration.Builder().Build();

var proxyConfig = new CoreProxyConfiguration(
address: "http://localhost",
port: 8080,
user: "user",
pass: "pass",
tunnel: true
);

var httpClientConfiguration = new CoreHttpClientConfiguration.Builder()
.ProxyConfiguration(proxyConfig)
.Build();

var request = await new GlobalConfiguration.Builder()
.HttpConfiguration(httpClientConfiguration)
.UserAgent(null)
.ServerUrls(new Dictionary<Enum, string>
{
{ MockServer.Server1, "http://my/path:3000/{one}"},
{ MockServer.Server2, "https://my/path/{two}"}
{ MockServer.Server1, "http://my/path:3000/{one}" },
{ MockServer.Server2, "https://my/path/{two}" }
}, MockServer.Server1)
.Build().GlobalRequestBuilder().Build();

Assert.IsFalse(request.Headers.ContainsKey("user-agent"));
}
}
Expand Down
27 changes: 15 additions & 12 deletions APIMatic.Core.Test/Http/CoreHttpClientConfigurationTest.cs
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 APIMatic.Core.Http.Configuration;
using APIMatic.Core.Proxy;
using NUnit.Framework;

namespace APIMatic.Core.Test.Http
Expand All @@ -14,13 +15,23 @@ public class CoreHttpClientConfigurationTest
[SetUp]
public void SetupCoreHttpClient()
{
_config = new CoreHttpClientConfiguration.Builder().Build();
var proxyConfig = new CoreProxyConfiguration(
address: "http://localhost",
port: 8080,
user: "user",
pass: "pass",
tunnel: true
);
_config = new CoreHttpClientConfiguration.Builder()
.ProxyConfiguration(proxyConfig)
.Build();


}

[Test]
public void Builder_BuildWithPrameters_CoreHttpClientConfiguration()
public void Builder_BuildWithParameters_CoreHttpClientConfiguration()
{
// Arrange
var timeout = 180;
var skipSslCertVerification = true;
var numberOfRetries = 3;
Expand All @@ -30,7 +41,6 @@ public void Builder_BuildWithPrameters_CoreHttpClientConfiguration()
var statusCodesToRetry = new List<int>() { 408, 409 };
var requestMethodsToRetry = new List<HttpMethod>() { HttpMethod.Post, HttpMethod.Get };

// Act
var config = _config.ToBuilder()
.Timeout(TimeSpan.FromSeconds(timeout))
.SkipSslCertVerification(skipSslCertVerification)
Expand All @@ -42,7 +52,6 @@ public void Builder_BuildWithPrameters_CoreHttpClientConfiguration()
.RequestMethodsToRetry(requestMethodsToRetry)
.Build();

// Assert
Assert.NotNull(config);
Assert.AreEqual(config.Timeout, TimeSpan.FromSeconds(timeout));
Assert.AreEqual(config.SkipSslCertVerification, skipSslCertVerification);
Expand All @@ -55,9 +64,8 @@ public void Builder_BuildWithPrameters_CoreHttpClientConfiguration()
}

[Test]
public void Builder_BuildWithInvalidPrameters_CoreHttpClientConfiguration()
public void Builder_BuildWithInvalidParameters_CoreHttpClientConfiguration()
{
// Arrange
var timeout = 0;
var numberOfRetries = -1;
var backoffFactor = 0;
Expand All @@ -77,14 +85,12 @@ public void Builder_BuildWithInvalidPrameters_CoreHttpClientConfiguration()
.RequestMethodsToRetry(null)
.Build();

//expected default values
var defaultTimeout = 100;
var defaultNumberOfRetries = 0;
var defaultBackoffFactor = 2;
var defaultRetryInterval = 1;
var defaultMaximumRetryWaitTime = 120;

// Assert
Assert.NotNull(config);
Assert.NotNull(config.HttpClientInstance);
Assert.AreEqual(config.Timeout, TimeSpan.FromSeconds(defaultTimeout));
Expand All @@ -100,10 +106,7 @@ public void Builder_BuildWithInvalidPrameters_CoreHttpClientConfiguration()
[Test]
public void ToString_Default_CoreHttpClientConfiguration()
{
// Act
var actual = _config.ToString();

// Assert
var expected = "HttpClientConfiguration: 00:01:40 , False , 0 , 2 , 1 , 00:02:00 , System.Collections.Immutable.ImmutableList`1[System.Int32] , System.Collections.Immutable.ImmutableList`1[System.Net.Http.HttpMethod] , System.Net.Http.HttpClient , True ";
Assert.AreEqual(expected, actual);
}
Expand Down
54 changes: 26 additions & 28 deletions APIMatic.Core.Test/Http/HttpClientWrapperSSLTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Threading.Tasks;
using APIMatic.Core.Http.Configuration;
using APIMatic.Core.Proxy;
using NUnit.Framework;

namespace APIMatic.Core.Test.Http
Expand All @@ -13,39 +14,37 @@ public class HttpClientWrapperSSLTest : TestBase
{
private readonly string expiredSSLCertUrl = "https://expired.badssl.com/";

[Test]
public async Task TestHttpClientSSLCertificateVerification_ExceptionResponse()
private CoreHttpClientConfiguration CreateClientConfiguration(bool skipSslVerification)
{
var expectedValue = "The SSL connection could not be established, see inner exception.";
var clientConfiguration = new CoreHttpClientConfiguration.Builder()
.Build();
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = skipSslVerification
? (sender, cert, chain, sslPolicyErrors) => true
: (sender, cert, chain, sslPolicyErrors) =>
{
return sslPolicyErrors == SslPolicyErrors.None;
}
};

var config = new GlobalConfiguration.Builder()
.ServerUrls(new Dictionary<Enum, string>
{
{ MockServer.Server1, expiredSSLCertUrl },
}, MockServer.Server1)
.HttpConfiguration(clientConfiguration)
.ApiCallback(ApiCallBack)
.Build();
var proxyConfig = new CoreProxyConfiguration(
address: "http://localhost",
port: 8080,
user: "user",
pass: "pass",
tunnel: true
);

var client = config.HttpClient;

var request = await config.GlobalRequestBuilder()
.Setup(HttpMethod.Get, string.Empty)
return new CoreHttpClientConfiguration.Builder()
.ProxyConfiguration(proxyConfig)
.HttpClientInstance(new HttpClient(handler))
.Build();

// Act
var ex = Assert.ThrowsAsync<HttpRequestException>(() => client.ExecuteAsync(request));
Assert.AreEqual(expectedValue, ex.Message);
}

[Test]
public async Task TestHttpClientSkipSSLCertificateVerification_OKResponse()
public async Task TestHttpClientSSLCertificateVerification_ExceptionResponse()
{
var clientConfiguration = new CoreHttpClientConfiguration.Builder()
.SkipSslCertVerification(true)
.Build();
var clientConfiguration = CreateClientConfiguration(skipSslVerification: false);

var config = new GlobalConfiguration.Builder()
.ServerUrls(new Dictionary<Enum, string>
Expand All @@ -63,9 +62,8 @@ public async Task TestHttpClientSkipSSLCertificateVerification_OKResponse()
.Build();

// Act
var actual = await client.ExecuteAsync(request);

Assert.AreEqual(actual.StatusCode, (int)HttpStatusCode.OK);
var ex = Assert.ThrowsAsync<HttpRequestException>(() => client.ExecuteAsync(request));
Assert.IsTrue(ex.Message.Contains("The SSL connection could not be established"));
}
}
}
19 changes: 14 additions & 5 deletions APIMatic.Core.Test/Http/HttpClientWrapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using APIMatic.Core.Http;
using APIMatic.Core.Http.Configuration;
using APIMatic.Core.Proxy;
using NUnit.Framework;
using RichardSzalay.MockHttp;

Expand All @@ -21,14 +22,24 @@ public class HttpClientWrapperTest : TestBase
[SetUp]
public void SetupHttpClient()
{
var proxyConfig = new CoreProxyConfiguration(
address: "http://localhost",
port: 8080,
user: "user",
pass: "pass",
tunnel: true
);

var clientConfiguration = new CoreHttpClientConfiguration.Builder()
.ProxyConfiguration(proxyConfig)
.HttpClientInstance(new HttpClient(handlerMock), false)
.Build();


_config = new GlobalConfiguration.Builder()
.ServerUrls(new Dictionary<Enum, string>
{
{ MockServer.Server1, "http://my/path:3000/{one}"},
{ MockServer.Server1, "http://my/path:3000/{one}" },
}, MockServer.Server1)
.HttpConfiguration(clientConfiguration)
.ApiCallback(ApiCallBack)
Expand All @@ -43,8 +54,7 @@ public async Task HttpClient_GetCall_200Response()
var request = await _config.GlobalRequestBuilder()
.Setup(HttpMethod.Get, "/httpclient/get/200")
.Parameters(p => p
.Body(b => b.Setup("Get Response")))
.Build();
.Body(b => b.Setup("Get Response"))).Build();

var content = new StringContent(request.Body.ToString(), Encoding.UTF8, "application/json");

Expand All @@ -63,8 +73,7 @@ public async Task TestHttpClientGetCall_400Response()
var request = await _config.GlobalRequestBuilder()
.Setup(HttpMethod.Get, "/httpclient/get/400")
.Parameters(p => p
.Body(b => b.Setup("Get Bad Request")))
.Build();
.Body(b => b.Setup("Get Bad Request"))).Build();

var content = new StringContent(request.Body.ToString(), Encoding.UTF8, "application/json");

Expand Down
57 changes: 57 additions & 0 deletions APIMatic.Core.Test/Proxy/CoreProxyConfigurationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using NUnit.Framework;
using APIMatic.Core.Proxy;

namespace APIMatic.Core.Test.Proxy
{
public class CoreProxyConfigurationTest
{
[Test]
public void Constructor_WithValidInput_ShouldSetAllPropertiesCorrectly()
{
// Act
var proxyConfig = new CoreProxyConfiguration(
"http://proxy.example.com", 8080, "user1", "pass123", true
);

// Assert
Assert.AreEqual("http://proxy.example.com", proxyConfig.Address);
Assert.AreEqual(8080, proxyConfig.Port);
Assert.AreEqual("user1", proxyConfig.User);
Assert.AreEqual("pass123", proxyConfig.Pass);
Assert.IsTrue(proxyConfig.Tunnel);
}

[Test]
public void CopyConstructor_WithValidProxy_ShouldCopyAllProperties()
{
// Arrange
var original = new CoreProxyConfiguration(
"http://proxy.example.com", 8888, "user2", "secret", false
);

// Act
var copy = new CoreProxyConfiguration(original);

// Assert
Assert.AreEqual(original.Address, copy.Address);
Assert.AreEqual(original.Port, copy.Port);
Assert.AreEqual(original.User, copy.User);
Assert.AreEqual(original.Pass, copy.Pass);
Assert.AreEqual(original.Tunnel, copy.Tunnel);
}

[Test]
public void CopyConstructor_WithNullProxy_ShouldSetDefaults()
{
// Act
var copy = new CoreProxyConfiguration(null);

// Assert
Assert.IsNull(copy.Address);
Assert.AreEqual(0, copy.Port);
Assert.IsNull(copy.User);
Assert.IsNull(copy.Pass);
Assert.IsFalse(copy.Tunnel);
}
}
}
7 changes: 6 additions & 1 deletion APIMatic.Core.Test/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http;
using APIMatic.Core.Authentication;
using APIMatic.Core.Http.Configuration;
using APIMatic.Core.Proxy;
using APIMatic.Core.Test.MockTypes.Authentication;
using APIMatic.Core.Types;
using RichardSzalay.MockHttp;
Expand All @@ -22,11 +23,15 @@ protected enum MockServer { Server1, Server2 }
AutoFlush = true
};

protected static readonly CoreProxyConfiguration proxyConfig = new("http://localhost", 8080, "user", "pass", true);

protected static readonly ICoreHttpClientConfiguration _clientConfiguration = new CoreHttpClientConfiguration.Builder()
.ProxyConfiguration(proxyConfig)
.HttpClientInstance(new HttpClient(handlerMock))
.NumberOfRetries(numberOfRetries)
.Build();


private static GlobalConfiguration globalConfiguration;

protected static Lazy<GlobalConfiguration> LazyGlobalConfiguration => new(() => globalConfiguration ??= new GlobalConfiguration.Builder()
Expand All @@ -44,6 +49,7 @@ protected enum MockServer { Server1, Server2 }
.Header(h => h.Setup("additionalHead2", "headVal2"))
.Template(t => t.Setup("one", "v1"))
.Template(t => t.Setup("two", "v2")))

.RuntimeParameters(p => p
.AdditionalHeaders(ah => ah.Setup(new Dictionary<string, object>
{
Expand All @@ -58,6 +64,5 @@ protected enum MockServer { Server1, Server2 }
.ApiCallback(ApiCallBack)
.Build()
);

}
}
Loading
Loading