Skip to content

Commit

Permalink
Refactor FakeHttpMessageHandler, minor changes to TestRequest and Htt…
Browse files Browse the repository at this point in the history
…pRequestMessageExtensions
  • Loading branch information
aannenko committed Sep 18, 2024
1 parent 9223155 commit 4ec721c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ public FakeHttpMessageHandler(TestRequest testRequest, TestResponse testResponse
{
}

protected override Task<HttpResponseMessage> SendAsync(
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
return SendInternal(request.ToTestRequestAsync().GetAwaiter().GetResult());
}

protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (requestToResponseMap.TryGetValue(request.ToTestRequest(), out var testResponse))
return SendInternal(await request.ToTestRequestAsync().ConfigureAwait(false));
}

private HttpResponseMessage SendInternal(TestRequest testRequest)
{
if (requestToResponseMap.TryGetValue(testRequest, out var testResponse))
{
var response = new HttpResponseMessage
{
Expand All @@ -26,9 +36,11 @@ protected override Task<HttpResponseMessage> SendAsync(
foreach (var (name, value) in testResponse.Headers)
response.Headers.TryAddWithoutValidation(name, value);

return Task.FromResult(response);
return response;
}

return Task.FromResult(new HttpResponseMessage { StatusCode = (HttpStatusCode)418 });
// My hope here is that none of the faked endpoints are expected to return "418 I'm a teapot".
// This is done in order not to occupy an exception type which could be asserted in the tests.
return new() { StatusCode = (HttpStatusCode)418 };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

internal static class HttpRequestMessageExtensions
{
public static TestRequest ToTestRequest(this HttpRequestMessage requestMessage)
public static async Task<TestRequest> ToTestRequestAsync(this HttpRequestMessage request)
{
return new TestRequest(
requestMessage.Method,
requestMessage.RequestUri,
requestMessage.Headers.ToDictionary(static pair => pair.Key, static pair => pair.Value.Single()),
requestMessage.Content?.ReadAsStringAsync().GetAwaiter().GetResult());
return new(
request.Method,
request.RequestUri,
request.Headers.ToDictionary(static pair => pair.Key, static pair => pair.Value.Single()),
request.Content is null ? null : await request.Content.ReadAsStringAsync().ConfigureAwait(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public sealed record TestRequest(
HttpMethod Method,
Uri? RequestUri,
Dictionary<string, string>? Headers = null,
IReadOnlyDictionary<string, string>? Headers = null,
string? Content = null)
{
public bool Equals(TestRequest? other)
Expand Down

0 comments on commit 4ec721c

Please sign in to comment.