Repository: dotnet/aspnetcore
File: src/Mvc/test/Mvc.FunctionalTests/RealServerBackedIntegrationTests.cs (C#)

Code before:

public class RealServerBackedIntegrationTests : IClassFixture<KestrelBasedWapFactory>
{
    public KestrelBasedWapFactory Factory { get; }

    public RealServerBackedIntegrationTests(KestrelBasedWapFactory factory)
    {
        Factory = factory;
    }

    [Fact]
    public async Task RetrievesDataFromRealServer()
    {
        // Arrange
        var expectedMediaType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");

        // Act
        var client = Factory.CreateClient();
        var response = await client.GetAsync("/");
        var responseContent = await response.Content.ReadAsStringAsync();

        // Assert
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);

        Assert.Equal(5000, client.BaseAddress.Port);

        Assert.Contains("first", responseContent);
        Assert.Contains("second", responseContent);
        Assert.Contains("wall", responseContent);
        Assert.Contains("floor", responseContent);
    }
}


Review comment (suggestion):
This is probably enough to verify that the `TestServer` isn't used unless we happen decide to change its default base address.

We could make this even more reliable by hitting an endpiont that references `httpContext.Features.Get<IConnectionSocketFeature>().Socket` and returns `((IPEndpoint)socket.LocalEndpoint).Port` as part of the response body and then verifying the body contains `"Port": 5000` as expected.

Edit: Better yet, we should just call `new HttpClient() { BaseAddress = factoryClient.BaseAddress }` and verity that works.

Code after:
namespace Microsoft.AspNetCore.Mvc.FunctionalTests;

public class RealServerBackedIntegrationTests : IClassFixture<KestrelBasedWebApplicationFactory>
{
    public KestrelBasedWebApplicationFactory Factory { get; }

    public RealServerBackedIntegrationTests(KestrelBasedWebApplicationFactory factory)
    {
        Factory = factory;
    }

    [Fact]
    public async Task RetrievesDataFromRealServer()
    {
        // Arrange
        var expectedMediaType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");

        // Act
        var client = Factory.CreateClient();
        var response = await client.GetAsync("/");
        var responseContent = await response.Content.ReadAsStringAsync();

        // Assert
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);

        Assert.Contains("first", responseContent);
        Assert.Contains("second", responseContent);
        Assert.Contains("wall", responseContent);
        Assert.Contains("floor", responseContent);
    }

    [Fact]
    public async Task ServerReachableViaGenericHttpClient()
    {
        // Arrange
        var baseAddress = new Uri("http://localhost:5000");

        // Act
        using var factoryClient = Factory.CreateClient();
        using var client = new HttpClient() { BaseAddress = factoryClient.BaseAddress };

        using var response = await client.GetAsync("/");

        // Assert
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
    }
}