Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: OnRequestCompleted callback #83

Open
johnknoop opened this issue Nov 6, 2020 · 2 comments
Open

Feature request: OnRequestCompleted callback #83

johnknoop opened this issue Nov 6, 2020 · 2 comments

Comments

@johnknoop
Copy link

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");
})
@niccou
Copy link

niccou commented Dec 16, 2020

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.

@richardszalay
Copy link
Owner

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants