Skip to content

Commit

Permalink
Initial implementation of ability to supply custom HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
basdijkstra committed Aug 14, 2023
1 parent 16a2326 commit 4074077
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
6 changes: 4 additions & 2 deletions RestAssured.Net/Dsl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace RestAssured
{
using RestAssured.Configuration;
using RestAssured.Request;
using System.Net.Http;

/// <summary>
/// Entry point to the RestAssured code and writing tests for HTTP-based APIs.
Expand All @@ -31,10 +32,11 @@ public class Dsl
/// <summary>
/// Used to start writing a new test.
/// </summary>
/// <param name="httpClient">The <see cref="HttpClient"/> to use when submitting the request.</param>
/// <returns>A <see cref="ExecutableRequest"/> object containing all relevant request properties.</returns>
public static ExecutableRequest Given()
public static ExecutableRequest Given(HttpClient? httpClient = null)
{
return new ExecutableRequest(RestAssuredConfig);
return new ExecutableRequest(RestAssuredConfig, httpClient);
}
}
}
8 changes: 6 additions & 2 deletions RestAssured.Net/Request/ExecutableRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace RestAssured.Request
/// </summary>
public class ExecutableRequest : IDisposable
{
private readonly HttpClient? httpClient;
private HttpRequestMessage request = new HttpRequestMessage();
private CookieCollection cookieCollection = new CookieCollection();
private RequestSpecification? requestSpecification;
Expand Down Expand Up @@ -74,12 +75,15 @@ public class ExecutableRequest : IDisposable
/// Initializes a new instance of the <see cref="ExecutableRequest"/> class.
/// </summary>
/// <param name="config">The <see cref="RestAssuredConfiguration"/> to use for all requests.</param>
internal ExecutableRequest(RestAssuredConfiguration config)
/// <param name="httpClient">The <see cref="HttpClient"/> to use when sending requests.</param>
internal ExecutableRequest(RestAssuredConfiguration config, HttpClient? httpClient)
{
this.disableSslCertificateValidation = config.DisableSslCertificateValidation;

this.RequestLoggingLevel = config.RequestLogLevel;
this.ResponseLoggingLevel = config.ResponseLogLevel;

this.httpClient = httpClient;
}

/// <summary>
Expand Down Expand Up @@ -594,7 +598,7 @@ private VerifiableResponse Send(HttpMethod httpMethod, string endpoint)
bool disableSslChecks = this.disableSslCertificateValidation || (this.requestSpecification?.DisableSslCertificateValidation ?? false);

// Create the HTTP request processor that sends the request and set its properties
HttpRequestProcessor httpRequestProcessor = new HttpRequestProcessor(this.proxy ?? this.requestSpecification?.Proxy, disableSslChecks);
HttpRequestProcessor httpRequestProcessor = new HttpRequestProcessor(this.httpClient, this.proxy ?? this.requestSpecification?.Proxy, disableSslChecks);

// Timeout set in test has precedence over timeout set in request specification
// If both are null, use default timeout for HttpClient (= 100.000 milliseconds).
Expand Down
23 changes: 12 additions & 11 deletions RestAssured.Net/Request/HttpRequestProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace RestAssured.Request
/// </summary>
internal class HttpRequestProcessor : IDisposable
{
private readonly HttpClientHandler handler;
private readonly HttpClient client;
private readonly HttpClientHandler httpClientHandler;
private readonly HttpClient httpClient;
private CookieContainer cookieContainer = new CookieContainer();
private bool disposed = false;

Expand All @@ -58,21 +58,22 @@ public virtual void Dispose(bool disposing)
return;
}

this.client.Dispose();
this.handler.Dispose();
this.httpClient.Dispose();
this.httpClientHandler.Dispose();
this.disposed = true;
}

/// <summary>
/// Initializes a new instance of the <see cref="HttpRequestProcessor"/> class.
/// </summary>
/// <param name="httpClient">The <see cref="HttpClient"/> to use when sending requests.</param>
/// <param name="proxy">The <see cref="IWebProxy"/> to set on the <see cref="HttpClientHandler"/> used with the <see cref="HttpClient"/>.</param>
/// <param name="disableSslCertificateValidation">If set to true, SSL certificate validation is disabled.</param>
internal HttpRequestProcessor(IWebProxy? proxy, bool disableSslCertificateValidation)
internal HttpRequestProcessor(HttpClient? httpClient, IWebProxy? proxy, bool disableSslCertificateValidation)
{
if (disableSslCertificateValidation)
{
this.handler = new HttpClientHandler
this.httpClientHandler = new HttpClientHandler
{
CookieContainer = this.cookieContainer,
Proxy = proxy,
Expand All @@ -81,15 +82,15 @@ internal HttpRequestProcessor(IWebProxy? proxy, bool disableSslCertificateValida
}
else
{
this.handler = new HttpClientHandler
this.httpClientHandler = new HttpClientHandler
{
CookieContainer = this.cookieContainer,
Proxy = proxy,
ServerCertificateCustomValidationCallback = ServerCertificateCustomValidation,
};
}

this.client = new HttpClient(this.handler);
this.httpClient = httpClient ?? new HttpClient(this.httpClientHandler);
}

/// <summary>
Expand All @@ -98,7 +99,7 @@ internal HttpRequestProcessor(IWebProxy? proxy, bool disableSslCertificateValida
/// <param name="timeout">The timeout to set on the HTTP client.</param>
internal void SetTimeout(TimeSpan timeout)
{
this.client.Timeout = timeout;
this.httpClient.Timeout = timeout;
}

/// <summary>
Expand Down Expand Up @@ -127,10 +128,10 @@ internal async Task<VerifiableResponse> Send(HttpRequestMessage request, CookieC
Stopwatch stopwatch = new Stopwatch();

stopwatch.Start();
HttpResponseMessage response = await this.client.SendAsync(request);
HttpResponseMessage response = await this.httpClient.SendAsync(request);
stopwatch.Stop();

return new VerifiableResponse(response, this.handler.CookieContainer, stopwatch.Elapsed);
return new VerifiableResponse(response, this.httpClientHandler.CookieContainer, stopwatch.Elapsed);
}
catch (HttpRequestException hre)
{
Expand Down
2 changes: 1 addition & 1 deletion RestAssured.Net/RestAssured.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>4.1.0-beta.2</Version>
<Version>4.1.0-beta.3</Version>
<Authors>Bas Dijkstra</Authors>
<Company>On Test Automation</Company>
<Description>C# port of the popular REST Assured library for writing tests for HTTP APIs.</Description>
Expand Down

0 comments on commit 4074077

Please sign in to comment.