title: Test controller action that makes an external API call
body: <p>I have an API controller with an action function. This function makes an external call for another API to get some data. this external call is made by simply creating a client with a URL. I want to create a test using WebApplicationFactory to test this action function.
  I would like to know how to configure this external call. To say if the server calls this URL return this response.</p>
  
  <p>May be it should be somewhere in overriding ConfigureWebHost to tell the server that if you call this URL (The external API url) return this response.</p>
  
  <p>Here is the controller action I want to test.</p>
  
  <pre><code>namespace MyAppAPI.Controllers
  {
      public class MyController : ControllerBase
      {
          [HttpPost("MyAction")]
          public async Task MyAction([FromBody] int inputParam)
          {
              var externalApiURL = "http://www.external.com?param=inputParam";
              var client = new HttpClient();
              var externalResponse = await client.GetAsync(externalApiURL);
              //more work with the externalResponse
          }
      }
  }
  </code></pre>
  
  <p>Here is the Test class I want to use</p>
  
  <pre><code>public class MyAppAPITests : IClassFixture&lt;WebApplicationFactory&lt;MyAppAPI.Startup&gt;&gt;
  {
       private readonly WebApplicationFactory&lt;MyAppAPI.Startup&gt; _factory;
  
       public MyAppAPITests(WebApplicationFactory&lt;MyAppAPI.Startup&gt; factory)
       {
            _factory = factory;
       }
  
       [Fact]
       public async Task Test_MyActionReturnsExpectedResponse()
       {
            //Arrange Code
  
            //Act
            //Here I would like to have something like this or a similar fashion
            _factory.ConfigureReponseForURL("http://www.external.com?param=inputParam",
                     response =&gt; {
                           response.Response = "ExpectedResponse";
                     });
  
            //Assert Code
       }
  }
  </code></pre>
  
  <p>The code in Test_MyActionReturnsExpectedResponse does not exist anywhere, it is just what I would like to have either by inheriting WebApplicationFactory or by configuring it. I would like to know how that can be achieved. i.e. configuring a response when an API controller makes an external call.
  Thank you for your help.</p>