Expand builder and tests for query strings and post body
This commit is contained in:
parent
524b418969
commit
022de696e3
2 changed files with 66 additions and 7 deletions
|
@ -1,11 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using RichardSzalay.MockHttp;
|
||||
|
||||
namespace Website.Tests {
|
||||
public class HttpClientBuilder {
|
||||
private string _url, _response;
|
||||
private string _url, _response, _body;
|
||||
private HttpMethod _method;
|
||||
private readonly Dictionary<string, string> _queries = new Dictionary<string, string>();
|
||||
|
||||
public HttpClientBuilder WithUrl(string url) {
|
||||
_url = url;
|
||||
|
@ -22,9 +25,25 @@ namespace Website.Tests {
|
|||
return this;
|
||||
}
|
||||
|
||||
public HttpClientBuilder WithQueryString(string key, string value) {
|
||||
_queries.Add(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpClientBuilder WithPostBody(string body) {
|
||||
_body = body;
|
||||
return this;
|
||||
}
|
||||
|
||||
public HttpClient Build() {
|
||||
var httpMessageHandler = new MockHttpMessageHandler();
|
||||
httpMessageHandler.When(_method, _url).Respond("application/json", _response);
|
||||
|
||||
var mockedRequest = httpMessageHandler.Expect(_method, _url).Respond("application/json", _response ?? string.Empty);
|
||||
|
||||
if (_queries.Any())
|
||||
mockedRequest.WithExactQueryString(_queries);
|
||||
if (!string.IsNullOrEmpty(_body))
|
||||
mockedRequest.WithContent(_body);
|
||||
|
||||
var httpClient = httpMessageHandler.ToHttpClient();
|
||||
httpClient.BaseAddress = new Uri("http://example.com");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue