looking for some solutions? You are welcome.

SOLVED: Send session token on TestServer

Adam:

CUSTOM MIDDLEWARE

My application has middleware which gets the string from a session:

public Task Invoke(HttpContext httpContext)
{
    //...

    string token = httpContext.Session.GetString("token");

    //...
}


TEST

There is an integration test

public class UsersControllerTest
{
    private readonly HttpClient _httpClient;
    private readonly TestServer _testServer;

    public UsersControllerTest()
    {
        var projectDir = @"c:\TestSessionApp";
        _testServer = new TestServer(WebHost.CreateDefaultBuilder()
            .UseEnvironment("Development")
                .UseContentRoot(projectDir)
                .UseConfiguration(new ConfigurationBuilder()
                    .SetBasePath(projectDir)
                    .AddJsonFile("appsettings.Development.json")
                    .Build()
                )
            .UseStartup<Startup>());
        _httpClient = _testServer.CreateClient();
    }

    [Theory]
    [InlineData("GET")]
    public async Task TestGet(string methodType)
    {
        var requestMsg = new HttpRequestMessage(new HttpMethod(methodType), "/api/users");
        //... ?

        var responseMsg = await _httpClient.SendAsync(requestMsg);
    }

How I can send session string by HttpClient? Should I mocking HttpContext in some way? However, as you can see, this is integration test, therefore, I would like to use minimum mock.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent