我的应用程序具有中间件,该中间件从会话中获取字符串:
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
?但是,如您所见,这是集成测试,因此,我想使用最少的模拟。