title: Mock HttpClient with multiple requests
body: <p>I'm trying to unit test a function that makes a call to an API. I have successfully done this using a mock <code>HttpMessageHandler</code> as below which allows me to fake a response from the API:</p>
  
  <pre><code>private static Mock&lt;HttpMessageHandler&gt; GetMockHttpMessageHandler(string mockResponse)
  {
      var mockMessageHandler = new Mock&lt;HttpMessageHandler&gt;();
      mockMessageHandler.Protected()
          .Setup&lt;Task&lt;HttpResponseMessage&gt;&gt;("SendAsync", ItExpr.IsAny&lt;HttpRequestMessage&gt;(), ItExpr.IsAny&lt;CancellationToken&gt;())
          .Returns(Task.FromResult(new HttpResponseMessage
          {
              StatusCode = HttpStatusCode.OK,
              Content = new StringContent(mockResponse)
          }));
      return mockMessageHandler;
  }
  </code></pre>
  
  <p>So far, so good. I've been able to test half of my function, the second half makes another api call - both responses are then wrapped up into an object used by the system. The problem is, the second api needs to have a different mock response.</p>
  
  <p>I thought I'd be able to change <code>ItExpr.IsAny&lt;HttpRequestMessage&gt;()</code> in the code above to <code>new HttpRequestMessage(HttpMethod.Post, "http://LiveUrl.com/AuthenticateUserCredential")</code> and then have multiple <code>Setup/Returns</code> with the response changing based on the URI, but I tried it as below (with only one <code>Setup/Return</code> to test I didn't break the first half test)</p>
  
  <pre><code>private static Mock&lt;HttpMessageHandler&gt; GetMockHttpMessageHandler(string mockResponse)
  {
      var mockMessageHandler = new Mock&lt;HttpMessageHandler&gt;();
      mockMessageHandler.Protected()
          .Setup&lt;Task&lt;HttpResponseMessage&gt;&gt;("SendAsync", new HttpRequestMessage(HttpMethod.Post, "http://LiveUrl.com/AuthenticateUserCredential"), ItExpr.IsAny&lt;CancellationToken&gt;())
          .Returns(Task.FromResult(new HttpResponseMessage
          {
              StatusCode = HttpStatusCode.OK,
              Content = new StringContent(mockResponse)
          }));
  
      return mockMessageHandler;
  }
  </code></pre>
  
  <p>Now the above breaks the first api call - I get the response below:</p>
  
  <blockquote>
    <p>handler did not return a response message</p>
  </blockquote>
  
  <p>Now I'm stuck - is what I'm trying to do even possible?</p>