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
|
@ -21,7 +21,11 @@ namespace Website.Tests.Data {
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Get_WithUrl_ReturnsResult() {
|
public async Task Get_WithUrl_ReturnsResult() {
|
||||||
var httpClient = new HttpClientBuilder().WithMethod(HttpMethod.Get).WithUrl("/test").WithResponse(@"{""TestProperty"":1}").Build();
|
var httpClient = new HttpClientBuilder()
|
||||||
|
.WithMethod(HttpMethod.Get)
|
||||||
|
.WithUrl("/test")
|
||||||
|
.WithResponse(@"{""TestProperty"":1}")
|
||||||
|
.Build();
|
||||||
|
|
||||||
var expected = new TestObject {TestProperty = 1};
|
var expected = new TestObject {TestProperty = 1};
|
||||||
|
|
||||||
|
@ -31,7 +35,13 @@ namespace Website.Tests.Data {
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Get_WithUrlAndQuery_ReturnsResult() {
|
public async Task Get_WithUrlAndQuery_ReturnsResult() {
|
||||||
var httpClient = new HttpClientBuilder().WithMethod(HttpMethod.Get).WithUrl("/test?query1=1&query2=2").WithResponse(@"{""TestProperty"":1}").Build();
|
var httpClient = new HttpClientBuilder()
|
||||||
|
.WithMethod(HttpMethod.Get)
|
||||||
|
.WithUrl("/test")
|
||||||
|
.WithQueryString("query1", "1")
|
||||||
|
.WithQueryString("query2", "2")
|
||||||
|
.WithResponse(@"{""TestProperty"":1}")
|
||||||
|
.Build();
|
||||||
|
|
||||||
var expected = new TestObject {TestProperty = 1};
|
var expected = new TestObject {TestProperty = 1};
|
||||||
|
|
||||||
|
@ -41,7 +51,11 @@ namespace Website.Tests.Data {
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Get_WithUrlThatReturns404_ThrowsException() {
|
public void Get_WithUrlThatReturns404_ThrowsException() {
|
||||||
var httpClient = new HttpClientBuilder().WithMethod(HttpMethod.Get).WithUrl("/test").WithResponse("").Build();
|
var httpClient = new HttpClientBuilder()
|
||||||
|
.WithMethod(HttpMethod.Get)
|
||||||
|
.WithUrl("/test")
|
||||||
|
.WithResponse("")
|
||||||
|
.Build();
|
||||||
|
|
||||||
var client = new TestApiClient(httpClient);
|
var client = new TestApiClient(httpClient);
|
||||||
|
|
||||||
|
@ -51,7 +65,11 @@ namespace Website.Tests.Data {
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Post_WithUrlAndValue_ReturnsResult() {
|
public async Task Post_WithUrlAndValue_ReturnsResult() {
|
||||||
var httpClient = new HttpClientBuilder().WithMethod(HttpMethod.Post).WithUrl("/test").WithResponse(@"{""TestProperty"":1}").Build();
|
var httpClient = new HttpClientBuilder()
|
||||||
|
.WithMethod(HttpMethod.Post)
|
||||||
|
.WithUrl("/test")
|
||||||
|
.WithResponse(@"{""TestProperty"":1}")
|
||||||
|
.Build();
|
||||||
|
|
||||||
var expected = new TestObject {TestProperty = 1};
|
var expected = new TestObject {TestProperty = 1};
|
||||||
|
|
||||||
|
@ -61,12 +79,34 @@ namespace Website.Tests.Data {
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Post_WithUrlAndValueAndQuery_ReturnsResult() {
|
public async Task Post_WithUrlAndValueAndQuery_ReturnsResult() {
|
||||||
var httpClient = new HttpClientBuilder().WithMethod(HttpMethod.Post).WithUrl("/test?query1=1&query2=2").WithResponse(@"{""TestProperty"":1}").Build();
|
var httpClient = new HttpClientBuilder()
|
||||||
|
.WithMethod(HttpMethod.Post)
|
||||||
|
.WithUrl("/test")
|
||||||
|
.WithQueryString("query1", "1")
|
||||||
|
.WithQueryString("query2", "2")
|
||||||
|
.WithPostBody("\"value\"")
|
||||||
|
.WithResponse(@"{""TestProperty"":1}")
|
||||||
|
.Build();
|
||||||
|
|
||||||
var expected = new TestObject {TestProperty = 1};
|
var expected = new TestObject {TestProperty = 1};
|
||||||
|
|
||||||
var client = new TestApiClient(httpClient);
|
var client = new TestApiClient(httpClient);
|
||||||
(await client.Post<TestObject>("/test", "value", new {query1 = 1, query2 = 2})).Should().BeEquivalentTo(expected);
|
(await client.Post<TestObject>("/test", "value", new {query1 = 1, query2 = 2})).Should().BeEquivalentTo(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Post_WithUrlAndValueAndQuery_ReturnsNoResult() {
|
||||||
|
var httpClient = new HttpClientBuilder()
|
||||||
|
.WithMethod(HttpMethod.Post)
|
||||||
|
.WithUrl("/test")
|
||||||
|
.WithQueryString("query1", "1")
|
||||||
|
.WithQueryString("query2", "2")
|
||||||
|
.WithPostBody("\"value\"")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
|
||||||
|
var client = new TestApiClient(httpClient);
|
||||||
|
(await client.Post<TestObject>("/test", "value", new {query1 = 1, query2 = 2})).Should().BeNull();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using RichardSzalay.MockHttp;
|
using RichardSzalay.MockHttp;
|
||||||
|
|
||||||
namespace Website.Tests {
|
namespace Website.Tests {
|
||||||
public class HttpClientBuilder {
|
public class HttpClientBuilder {
|
||||||
private string _url, _response;
|
private string _url, _response, _body;
|
||||||
private HttpMethod _method;
|
private HttpMethod _method;
|
||||||
|
private readonly Dictionary<string, string> _queries = new Dictionary<string, string>();
|
||||||
|
|
||||||
public HttpClientBuilder WithUrl(string url) {
|
public HttpClientBuilder WithUrl(string url) {
|
||||||
_url = url;
|
_url = url;
|
||||||
|
@ -22,9 +25,25 @@ namespace Website.Tests {
|
||||||
return this;
|
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() {
|
public HttpClient Build() {
|
||||||
var httpMessageHandler = new MockHttpMessageHandler();
|
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();
|
var httpClient = httpMessageHandler.ToHttpClient();
|
||||||
httpClient.BaseAddress = new Uri("http://example.com");
|
httpClient.BaseAddress = new Uri("http://example.com");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue