Put in test fallback for manual error code. Output http mock for later verification.
This commit is contained in:
parent
022de696e3
commit
e389b2404a
2 changed files with 26 additions and 11 deletions
|
@ -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<TestObject>("/404")).Should().Throw<ApiCallException>()
|
||||
.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<TestObject>("/test", "value", new {query1 = 1, query2 = 2})).Should().BeNull();
|
||||
await client.Post<object>("/test", "value", new {query1 = 1, query2 = 2});
|
||||
mockHttpMessageHandler.VerifyNoOutstandingExpectation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<string, string> _queries = new Dictionary<string, string>();
|
||||
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 _);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue