title: Mock IHttpContextAccessor in Integration Tests
body: <p>I would like to test one method in controller which is retrieving value from <code>IHttpContextAccessor.HttpContext.Connection.RemoteIpAddress</code>.</p>
  <p>Is it possible to customize <a href="https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-5.0#aspnet-core-integration-tests" rel="nofollow noreferrer">WebApplicationFactory</a> so one can set fixed ip address?</p>
  <p>The workaround would be to wrap the expression above into service so it can be mocked.</p>
  <p>Thanks</p>
  <p><em>Test.cs</em></p>
  <pre class="lang-cs prettyprint-override"><code>var ipAddress = IPAddress.Parse(&quot;192.168.1.123&quot;);
  var factory = new WebApplicationFactory&lt;Startup&gt;();
  // ?
  
  using var client = factory.CreateClient();
  var response = await client.GetAsync(new Uri(&quot;https://localhost/test&quot;), UriKind.Relative));
  var ipAddress = await response.Content.ReadAsStringAsync();
  </code></pre>
  <p><em>TestController.cs</em></p>
  <pre><code>  private readonly IHttpContextAccessor _httpContextAccessor;
  
    public TestController(IHttpContextAccessor httpContextAccessor)
          {
  
              _httpContextAccessor = httpContextAccessor;
          }
  
    [HttpGet(&quot;test&quot;)]
    public async Task&lt;IActionResult&gt; GetTest()
    {
  
        var ipAddressObj = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress;
  
        var ipAddress = ipAddressObj?.ToString();
  
        return Ok(ipAddress);
     }
  </code></pre>