diff --git a/FakeHttpMessageHandler.Tests/FakeHttpMessageHandlerTest.cs b/FakeHttpMessageHandler.Tests/FakeHttpMessageHandlerTest.cs index 729aebd..134a271 100644 --- a/FakeHttpMessageHandler.Tests/FakeHttpMessageHandlerTest.cs +++ b/FakeHttpMessageHandler.Tests/FakeHttpMessageHandlerTest.cs @@ -4,6 +4,7 @@ using Moq; using Newtonsoft.Json; using System; +using System.Net; using System.Net.Http; using System.Threading.Tasks; @@ -26,6 +27,7 @@ public async Task FakeHttpMessageHandler_Verify_ReturnsInstanceOfT() var responseText = await responce.Content.ReadAsStringAsync(); var clientOutput = JsonConvert.DeserializeObject(responseText); + responce.StatusCode.Should().Be(HttpStatusCode.OK); clientOutput.Should().NotBeNull(); clientOutput.FakeProperty.Should().NotBeNullOrWhiteSpace(); handler.CallsCount.Should().Be(1); @@ -56,6 +58,26 @@ public async Task FakeHttpMessageHandler_Verify_ReturnsOutputOverride() } } + [TestMethod] + public async Task FakeHttpMessageHandler_Verify_ReturnStatusCodeOverride() + { + var output = new FakeOutput + { + FakeProperty = "MyFakeProperty" + }; + + var handler = new FakeHttpMessageHandler(output, httpStatusCode: HttpStatusCode.BadRequest); + + + using (var httpClient = new HttpClient(handler, true)) + { + var responce = await httpClient.GetAsync(_requestUri); + responce.StatusCode.Should().Be(HttpStatusCode.BadRequest); + + handler.CallsCount.Should().Be(1); + } + } + [TestMethod] public async Task FakeHttpMessageHandler_Verify_ReturnsStringOutput() { diff --git a/FakeHttpMessageHandler/FakeHttpMessageHandler.cs b/FakeHttpMessageHandler/FakeHttpMessageHandler.cs index 31329d8..85c927f 100644 --- a/FakeHttpMessageHandler/FakeHttpMessageHandler.cs +++ b/FakeHttpMessageHandler/FakeHttpMessageHandler.cs @@ -18,7 +18,7 @@ public class FakeHttpMessageHandler : HttpMessageHandler where T : class private readonly Func _createResponseObjectFunction; private readonly Func> _createResponseObjectAsyncFunction; private readonly Func _serializerFunction; - + private readonly HttpStatusCode _httpStatusCode; /// /// FakeHttpMessageHandler seamlessly fakes http request sent from System.Net.Http.HttpClient for purpose of unit testing code that previously could only be integration tested. @@ -26,10 +26,12 @@ public class FakeHttpMessageHandler : HttpMessageHandler where T : class /// /// /// - public FakeHttpMessageHandler(T overrideResponseContent = null, Func serializerFunction = null) + /// + public FakeHttpMessageHandler(T overrideResponseContent = null, Func serializerFunction = null, HttpStatusCode httpStatusCode = HttpStatusCode.OK) { _overrideResponseContent = overrideResponseContent; - _serializerFunction = serializerFunction ?? new Func((T output) => JsonConvert.SerializeObject(output)); + _serializerFunction = serializerFunction ?? new Func((T output) => JsonConvert.SerializeObject(output)); + _httpStatusCode = httpStatusCode; } /// @@ -38,10 +40,12 @@ public FakeHttpMessageHandler(T overrideResponseContent = null, Func /// /// /// - public FakeHttpMessageHandler(Func createResponseObjectFunction, Func serializerFunction = null) + /// + public FakeHttpMessageHandler(Func createResponseObjectFunction, Func serializerFunction = null, HttpStatusCode httpStatusCode = HttpStatusCode.OK) { _createResponseObjectFunction = createResponseObjectFunction ?? throw new ArgumentNullException(nameof(createResponseObjectFunction)); _serializerFunction = serializerFunction ?? new Func((T output) => JsonConvert.SerializeObject(output)); + _httpStatusCode = httpStatusCode; } /// @@ -50,10 +54,12 @@ public FakeHttpMessageHandler(Func createResponseObjectFunction, Func /// /// - public FakeHttpMessageHandler(Func> createResponseObjectAsyncFunction, Func serializerFunction = null) + /// + public FakeHttpMessageHandler(Func> createResponseObjectAsyncFunction, Func serializerFunction = null, HttpStatusCode httpStatusCode = HttpStatusCode.OK) { _createResponseObjectAsyncFunction = createResponseObjectAsyncFunction ?? throw new ArgumentNullException(nameof(createResponseObjectAsyncFunction)); _serializerFunction = serializerFunction ?? new Func((T output) => JsonConvert.SerializeObject(output)); + _httpStatusCode = httpStatusCode; } /// @@ -78,7 +84,7 @@ protected override async Task SendAsync(HttpRequestMessage returnObject = _overrideResponseContent ?? Activator.CreateInstance(); } var returnObjectString = returnObject is string ? returnObject as string : _serializerFunction(returnObject); - HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK) + HttpResponseMessage responseMessage = new HttpResponseMessage(_httpStatusCode) { Content = new ByteArrayContent(Encoding.ASCII.GetBytes(returnObjectString)) }; diff --git a/FakeHttpMessageHandler/FakeHttpMessageHandler.csproj b/FakeHttpMessageHandler/FakeHttpMessageHandler.csproj index e5f372a..2f1a79f 100644 --- a/FakeHttpMessageHandler/FakeHttpMessageHandler.csproj +++ b/FakeHttpMessageHandler/FakeHttpMessageHandler.csproj @@ -10,10 +10,18 @@ httpclient unittest unit testing test http client http message handler fake xunit web api url rest Copyright ©2018 Zheludov false + true + true + snupkg - + + + + + + all runtime; build; native; contentfiles; analyzers