在TestServer上发送会话令牌

时间:2019-03-09 08:00:31

标签: c# testing asp.net-core xunit asp.net-core-2.1

自定义中间件

我的应用程序具有中间件,该中间件从会话中获取字符串:

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

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

    //...
}

测试

有一个集成测试

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);
    }

如何通过HttpClient发送会话字符串?是否应该以某种方式模拟HttpContext?但是,如您所见,这是集成测试,因此,我想使用最少的模拟。

0 个答案:

没有答案