How to create mock for httpclient getasync method?
Asked Answered
V

1

12

I am using Moq to create mocks for my unit tests but I am stuck when I have to create mock for getasync method of httpclient. Previously I was using SendAsync method and for that I could use the below code:

   var mockResponse =
            new HttpResponseMessage(HttpStatusCode.OK) {Content = new StringContent(expectedResponse)};
        mockResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        var mockHandler = new Mock<DelegatingHandler>();
        mockHandler
            .Protected()
            .Setup<Task<HttpResponseMessage>>(
                "SendAsync",
                ItExpr.Is<HttpRequestMessage>(
                    message => message.Headers.Contains("Authorization")
                               && message.Headers.Authorization.Parameter.Equals(accessToken)
                               && message.Headers.Authorization.Scheme.Equals("Bearer")
                               && message.RequestUri.AbsoluteUri.Contains(baseUrl)
                ),
                ItExpr.IsAny<CancellationToken>())
            .Returns(Task.FromResult(mockResponse));

Now I have a method:

  private async Task<List<Model>> GetData()
  {
        string url = url;
        _httpClient.BaseAddress = new Uri(url);
        _httpClient.DefaultRequestHeaders.Add(Headers.AuthorizationHeader, "Bearer" + "token");
        var response = await _httpClient.GetAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsAsync<List<Model>>();
  }

Now can I create mock for this method (getasync)? Any help?

Verla answered 31/10, 2018 at 10:56 Comment(1)
internally GetAsync will eventually call SendAsync. Loosen the ItExpr and you should be able to get it to behave as expected.Biological
B
21

Internally GetAsync will eventually call SendAsync.

public Task<HttpResponseMessage> GetAsync(Uri requestUri, HttpCompletionOption completionOption,
    CancellationToken cancellationToken)
{
    return SendAsync(new HttpRequestMessage(HttpMethod.Get, requestUri), completionOption, cancellationToken);
}

Source code

Loosen the ItExpr expectation and you should be able to get it to behave as expected.

Using the originally provided example

mockHandler
    .Protected()
    .Setup<Task<HttpResponseMessage>>(
        "SendAsync",
        ItExpr.IsAny<HttpRequestMessage>(),
        ItExpr.IsAny<CancellationToken>()
    )
    .ReturnsAsync(mockResponse);
Biological answered 31/10, 2018 at 11:3 Comment(5)
And same is the case for PostAsync and PostJsonAsync?Verla
@Verla Yes. If you check the source code you will see that SendAsync is the main method for making requests. all the others eventually call that methodBiological
and more thing, I know it's not related to this question and should be another question but do we have any way to create mock of static methods (extension methods)Verla
@Verla Moq is unable to mock static/extension methods.Biological
@Verla review the source code of the extension, if available and try to craft a safe path through its invocation.Biological

© 2022 - 2024 — McMap. All rights reserved.