Add builder for HttpClient
This commit is contained in:
parent
176c401cf1
commit
524b418969
2 changed files with 40 additions and 17 deletions
|
@ -1,8 +1,6 @@
|
||||||
using System;
|
using System.Net.Http;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using RichardSzalay.MockHttp;
|
|
||||||
using Website.Data;
|
using Website.Data;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
@ -21,18 +19,9 @@ namespace Website.Tests.Data {
|
||||||
public int TestProperty { get; set; }
|
public int TestProperty { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HttpClient MakeHttpClient(HttpMethod method, string url, string response) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Get_WithUrl_ReturnsResult() {
|
public async Task Get_WithUrl_ReturnsResult() {
|
||||||
var httpClient = MakeHttpClient(HttpMethod.Get, "/test", @"{""TestProperty"":1}");
|
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};
|
||||||
|
|
||||||
|
@ -42,7 +31,7 @@ namespace Website.Tests.Data {
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Get_WithUrlAndQuery_ReturnsResult() {
|
public async Task Get_WithUrlAndQuery_ReturnsResult() {
|
||||||
var httpClient = MakeHttpClient(HttpMethod.Get, "/test?query1=1&query2=2", @"{""TestProperty"":1}");
|
var httpClient = new HttpClientBuilder().WithMethod(HttpMethod.Get).WithUrl("/test?query1=1&query2=2").WithResponse(@"{""TestProperty"":1}").Build();
|
||||||
|
|
||||||
var expected = new TestObject {TestProperty = 1};
|
var expected = new TestObject {TestProperty = 1};
|
||||||
|
|
||||||
|
@ -52,7 +41,7 @@ namespace Website.Tests.Data {
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Get_WithUrlThatReturns404_ThrowsException() {
|
public void Get_WithUrlThatReturns404_ThrowsException() {
|
||||||
var httpClient = MakeHttpClient(HttpMethod.Get, "/test", "");
|
var httpClient = new HttpClientBuilder().WithMethod(HttpMethod.Get).WithUrl("/test").WithResponse("").Build();
|
||||||
|
|
||||||
var client = new TestApiClient(httpClient);
|
var client = new TestApiClient(httpClient);
|
||||||
|
|
||||||
|
@ -62,7 +51,7 @@ namespace Website.Tests.Data {
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Post_WithUrlAndValue_ReturnsResult() {
|
public async Task Post_WithUrlAndValue_ReturnsResult() {
|
||||||
var httpClient = MakeHttpClient(HttpMethod.Post, "/test", @"{""TestProperty"":1}");
|
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};
|
||||||
|
|
||||||
|
@ -72,7 +61,7 @@ namespace Website.Tests.Data {
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Post_WithUrlAndValueAndQuery_ReturnsResult() {
|
public async Task Post_WithUrlAndValueAndQuery_ReturnsResult() {
|
||||||
var httpClient = MakeHttpClient(HttpMethod.Post, "/test?query1=1&query2=2", @"{""TestProperty"":1}");
|
var httpClient = new HttpClientBuilder().WithMethod(HttpMethod.Post).WithUrl("/test?query1=1&query2=2").WithResponse(@"{""TestProperty"":1}").Build();
|
||||||
|
|
||||||
var expected = new TestObject {TestProperty = 1};
|
var expected = new TestObject {TestProperty = 1};
|
||||||
|
|
||||||
|
|
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