title: How to do integration testing on an external API with ASP.NET Core
body: <p>I'm trying to do some integration tests on an external API. Most of the guides I find online are about testing the ASP.NET web api, but there's not much to find about external API's. I want to test a GET request on this API and confirm if it passes by checking if the status code is OK. However this test is not passing and im wondering if i'm doing this correctly. Currently it's giving me a status code 404(Not found).</p>
  <p>I'm using <code>xUnit</code> together with <code>Microsoft.AspNetCore.TestHost</code> How would you suggest me to test external API's?</p>
  <pre><code>private readonly HttpClient _client;
  
  public DevicesApiTests()
  {
      var server = new TestServer(new WebHostBuilder()
          .UseEnvironment(&quot;Development&quot;)
          .UseStartup&lt;Startup&gt;());
      _client = server.CreateClient();
  }
  
  [Theory]
  [InlineData(&quot;GET&quot;)]
  public async Task GetAllDevicesFromPRTG(string method)
  {
      //Arrange
      var request = new HttpRequestMessage(new HttpMethod(method), &quot;https://prtg.nl/api/content=Group,Device,Status&quot;);
  
      //Act
      var response = await _client.SendAsync(request);
  
      // Assert
      response.EnsureSuccessStatusCode();
      Assert.Equal(HttpStatusCode.OK, response.StatusCode);
  }
  </code></pre>
  <p><strong>Edit</strong></p>
  <p>The API call which im trying to test looks as follows, and is working properly</p>
  <pre><code>private readonly DbContext _dbContext;
  private readonly IDevicesRepository _devicesRepository;
  
  public DevicesAPIController(DbContext dbContext, IDevicesRepository devicesRepository)
  {
      _dbContext = dbContext;
      _devicesRepository = devicesRepository;
  }
  
  [HttpPost(&quot;PostLiveDevicesToDatabase&quot;)]
  public async Task&lt;IActionResult&gt; PostLiveDevicesToDatabase()
  {
      try
      {
  
          using (var httpClient = new HttpClient())
          {
              httpClient.DefaultRequestHeaders.Clear();
              httpClient.DefaultRequestHeaders.Accept.Add(
                  new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
  
              using (var response = await httpClient
                  .GetAsync(
                      &quot;https://prtg.nl/api/content=Group,Device,Status&quot;)
              )
              {
                  string apiResponse = await response.Content.ReadAsStringAsync();
                  var dataDeserialized = JsonConvert.DeserializeObject&lt;Devices&gt;(apiResponse);
  
                  devicesList.AddRange(dataDeserialized.devices);
  
                  foreach (DevicesData device in devicesList)
                  {
  
                      _dbContext.Devices.Add(device);
                      devicesAdded.Add(device);
  
                      _dbContext.SaveChanges();
                  }
              }
          }
      }
      catch
      {
          return BadRequest();
      }
  }
  </code></pre>