diff --git a/Website.Tests/Data/ApiClientTests.cs b/Website.Tests/Data/ApiClientTests.cs index 05e042e..82da2b3 100644 --- a/Website.Tests/Data/ApiClientTests.cs +++ b/Website.Tests/Data/ApiClientTests.cs @@ -1,4 +1,5 @@ -using System.Net.Http; +using System.Net; +using System.Net.Http; using System.Threading.Tasks; using FluentAssertions; using Website.Data; @@ -50,17 +51,18 @@ namespace Website.Tests.Data { } [Fact] - public void Get_WithUrlThatReturns404_ThrowsException() { + public void Get_WithUrlThatReturnsError_ThrowsException() { var httpClient = new HttpClientBuilder() .WithMethod(HttpMethod.Get) .WithUrl("/test") .WithResponse("") + .WithErrorStatus(HttpStatusCode.NotFound) .Build(); var client = new TestApiClient(httpClient); client.Invoking(apiClient => apiClient.Get("/404")).Should().Throw() - .WithMessage("Error calling API http://example.com/404: 404, No matching mock handler for \"GET http://example.com/404\""); + .WithMessage("Error calling API http://example.com/404: 404, Not Found"); } [Fact] @@ -69,6 +71,7 @@ namespace Website.Tests.Data { .WithMethod(HttpMethod.Post) .WithUrl("/test") .WithResponse(@"{""TestProperty"":1}") + .WithPostBody("\"value\"") .Build(); var expected = new TestObject {TestProperty = 1}; @@ -102,11 +105,12 @@ namespace Website.Tests.Data { .WithQueryString("query1", "1") .WithQueryString("query2", "2") .WithPostBody("\"value\"") - .Build(); + .Build(out var mockHttpMessageHandler); var client = new TestApiClient(httpClient); - (await client.Post("/test", "value", new {query1 = 1, query2 = 2})).Should().BeNull(); + await client.Post("/test", "value", new {query1 = 1, query2 = 2}); + mockHttpMessageHandler.VerifyNoOutstandingExpectation(); } } } diff --git a/Website.Tests/HttpClientBuilder.cs b/Website.Tests/HttpClientBuilder.cs index 4d2998d..cb1f623 100644 --- a/Website.Tests/HttpClientBuilder.cs +++ b/Website.Tests/HttpClientBuilder.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Net.Http; using RichardSzalay.MockHttp; @@ -9,6 +10,7 @@ namespace Website.Tests { private string _url, _response, _body; private HttpMethod _method; private readonly Dictionary _queries = new Dictionary(); + private HttpStatusCode _fallbackCode = HttpStatusCode.OK; public HttpClientBuilder WithUrl(string url) { _url = url; @@ -35,19 +37,28 @@ namespace Website.Tests { return this; } - public HttpClient Build() { - var httpMessageHandler = new MockHttpMessageHandler(); + public HttpClientBuilder WithErrorStatus(HttpStatusCode statusCode) { + _fallbackCode = statusCode; + return this; + } - var mockedRequest = httpMessageHandler.Expect(_method, _url).Respond("application/json", _response ?? string.Empty); + public HttpClient Build(out MockHttpMessageHandler mockHttpMessageHandler) { + mockHttpMessageHandler = new MockHttpMessageHandler(); + + mockHttpMessageHandler.Fallback.Respond(_fallbackCode, message => message.Content = new StringContent(string.Empty)); + + var mockedRequest = mockHttpMessageHandler.Expect(_method, _url).Respond("application/json", _response ?? string.Empty); if (_queries.Any()) mockedRequest.WithExactQueryString(_queries); - if (!string.IsNullOrEmpty(_body)) - mockedRequest.WithContent(_body); + if (!string.IsNullOrEmpty(_body) || _method == HttpMethod.Post) + mockedRequest.WithContent(_body ?? string.Empty); - var httpClient = httpMessageHandler.ToHttpClient(); + var httpClient = mockHttpMessageHandler.ToHttpClient(); httpClient.BaseAddress = new Uri("http://example.com"); return httpClient; } + + public HttpClient Build() => Build(out _); } }