Add builder for HttpClient

This commit is contained in:
Robert Marshall 2020-04-10 22:24:24 +01:00
parent 176c401cf1
commit 524b418969
2 changed files with 40 additions and 17 deletions

View file

@ -0,0 +1,34 @@
using System;
using System.Net.Http;
using RichardSzalay.MockHttp;
namespace Website.Tests {
public class HttpClientBuilder {
private string _url, _response;
private HttpMethod _method;
public HttpClientBuilder WithUrl(string url) {
_url = url;
return this;
}
public HttpClientBuilder WithResponse(string response) {
_response = response;
return this;
}
public HttpClientBuilder WithMethod(HttpMethod method) {
_method = method;
return this;
}
public HttpClient Build() {
var httpMessageHandler = new MockHttpMessageHandler();
httpMessageHandler.When(_method, _url).Respond("application/json", _response);
var httpClient = httpMessageHandler.ToHttpClient();
httpClient.BaseAddress = new Uri("http://example.com");
return httpClient;
}
}
}