richardszalay/mockhttp

Feature request: OnRequestCompleted callback

johnknoop opened this issue · 2 comments

Some of my end-to-end tests result in quite a large number of HTTP requests. When a request is made for which there is no MockedRequest setup (or the order is wrong), I always have a hard time correlating which one. Aside from the feature suggestion in #82, it would be nice to also be able to hook into a callback that is called whenever a request completes. Then the test output when give me a good audit log of what has happened and where it goes wrong.

httpMock.OnRequestCompleted((HttpRequestMessage request, HttpResponseMessage response) => {
    logger.Debug($"HTTP request to {request.Uri.AbsoluteUri} completed successfully");
})

Hi @johnknoop ,

I had the same case today. I used this way to handle the call:

internal void SetupPutRouteResponse(string route, Func<HttpRequestMessage, Task<HttpResponseMessage>> requestHandler)
=> MessageHandler.When(HttpMethod.Put, route).Respond(requestHandler);

and the request handler

private async Task<HttpResponseMessage> HandlePutRequest(HttpRequestMessage req)
{
    var content = await req.Content.ReadAsStringAsync().ConfigureAwait(false);
    _logger.LogDebug(content);
    return new HttpResponseMessage(HttpStatusCode.OK);
}

Perhaps it will help you.

This is a good idea! (Apologies for the very late reply)

I'll look at this the next time I'm updating the library