Add builder for HttpClient
This commit is contained in:
parent
176c401cf1
commit
524b418969
2 changed files with 40 additions and 17 deletions
34
Website.Tests/HttpClientBuilder.cs
Normal file
34
Website.Tests/HttpClientBuilder.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue