Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions FakeHttpMessageHandler.Tests/FakeHttpMessageHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Moq;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

Expand All @@ -26,6 +27,7 @@ public async Task FakeHttpMessageHandler_Verify_ReturnsInstanceOfT()
var responseText = await responce.Content.ReadAsStringAsync();
var clientOutput = JsonConvert.DeserializeObject<FakeOutput>(responseText);

responce.StatusCode.Should().Be(HttpStatusCode.OK);
clientOutput.Should().NotBeNull();
clientOutput.FakeProperty.Should().NotBeNullOrWhiteSpace();
handler.CallsCount.Should().Be(1);
Expand Down Expand Up @@ -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<FakeOutput>(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()
{
Expand Down
18 changes: 12 additions & 6 deletions FakeHttpMessageHandler/FakeHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ public class FakeHttpMessageHandler<T> : HttpMessageHandler where T : class
private readonly Func<T> _createResponseObjectFunction;
private readonly Func<Task<T>> _createResponseObjectAsyncFunction;
private readonly Func<T, string> _serializerFunction;

private readonly HttpStatusCode _httpStatusCode;

/// <summary>
/// FakeHttpMessageHandler seamlessly fakes http request sent from System.Net.Http.HttpClient for purpose of unit testing code that previously could only be integration tested.
/// Passing <paramref name="overrideResponseContent"/> will override response of a message handler to JSON serialized representation of <paramref name="overrideResponseContent"/>
/// </summary>
/// <param name="overrideResponseContent"></param>
/// <param name="serializerFunction"></param>
public FakeHttpMessageHandler(T overrideResponseContent = null, Func<T, string> serializerFunction = null)
/// <param name="httpStatusCode"></param>
public FakeHttpMessageHandler(T overrideResponseContent = null, Func<T, string> serializerFunction = null, HttpStatusCode httpStatusCode = HttpStatusCode.OK)
{
_overrideResponseContent = overrideResponseContent;
_serializerFunction = serializerFunction ?? new Func<T, string>((T output) => JsonConvert.SerializeObject(output));
_serializerFunction = serializerFunction ?? new Func<T, string>((T output) => JsonConvert.SerializeObject(output));
_httpStatusCode = httpStatusCode;
}

/// <summary>
Expand All @@ -38,10 +40,12 @@ public FakeHttpMessageHandler(T overrideResponseContent = null, Func<T, string>
/// </summary>
/// <param name="createResponseObjectFunction"></param>
/// <param name="serializerFunction"></param>
public FakeHttpMessageHandler(Func<T> createResponseObjectFunction, Func<T, string> serializerFunction = null)
/// <param name="httpStatusCode"></param>
public FakeHttpMessageHandler(Func<T> createResponseObjectFunction, Func<T, string> serializerFunction = null, HttpStatusCode httpStatusCode = HttpStatusCode.OK)
{
_createResponseObjectFunction = createResponseObjectFunction ?? throw new ArgumentNullException(nameof(createResponseObjectFunction));
_serializerFunction = serializerFunction ?? new Func<T, string>((T output) => JsonConvert.SerializeObject(output));
_httpStatusCode = httpStatusCode;
}

/// <summary>
Expand All @@ -50,10 +54,12 @@ public FakeHttpMessageHandler(Func<T> createResponseObjectFunction, Func<T, stri
/// </summary>
/// <param name="createResponseObjectAsyncFunction"></param>
/// <param name="serializerFunction"></param>
public FakeHttpMessageHandler(Func<Task<T>> createResponseObjectAsyncFunction, Func<T, string> serializerFunction = null)
/// <param name="httpStatusCode"></param>
public FakeHttpMessageHandler(Func<Task<T>> createResponseObjectAsyncFunction, Func<T, string> serializerFunction = null, HttpStatusCode httpStatusCode = HttpStatusCode.OK)
{
_createResponseObjectAsyncFunction = createResponseObjectAsyncFunction ?? throw new ArgumentNullException(nameof(createResponseObjectAsyncFunction));
_serializerFunction = serializerFunction ?? new Func<T, string>((T output) => JsonConvert.SerializeObject(output));
_httpStatusCode = httpStatusCode;
}

/// <summary>
Expand All @@ -78,7 +84,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
returnObject = _overrideResponseContent ?? Activator.CreateInstance<T>();
}
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))
};
Expand Down
10 changes: 9 additions & 1 deletion FakeHttpMessageHandler/FakeHttpMessageHandler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@
<PackageTags>httpclient unittest unit testing test http client http message handler fake xunit web api url rest</PackageTags>
<Copyright>Copyright ©2018 Zheludov</Copyright>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GitVersionTask" Version="4.0.0">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="GitVersionTask" Version="5.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down