集成测试结果一直返回状态码301
Integration tests result keeps returning status code 301
我正在尝试对项目进行集成测试,但同样的错误不断出现。用邮递员手动做同样的事情很好。
我已经尝试更改端口号并启用 on/off SSL,但仍然出现同样的问题。
用户API测试:
[TestClass]
public class UserAPITest
{
private readonly HttpClient _client;
private readonly IConfiguration _configuration;
public UserAPITest()
{
_client = TestsHelper.Client;
_configuration = TestsHelper.Configurations;
}
[TestInitialize]
public void TestInitialize()
{
}
[TestMethod]
[DataRow(nameof(HttpMethod.Post), "api/User/refreshToken")]
public async Task Initiation_Authorized_ShouldReturnNewToken(string httpMethod, string url)
{
_client.DefaultRequestHeaders.Authorization = TestsHelper.GetTestUserBearerHeader();
var request = new HttpRequestMessage(new HttpMethod(httpMethod), url);
var response = await _client.SendAsync(request);
Assert.IsNotNull(response.Content); // value during debug added below
Assert.AreEqual(HttpStatusCode.OK,response.StatusCode); // test fails here
// check for token inside response
// Assert.IsTrue(Regex.IsMatch(response.RequestMessage.Content.ToString(), "^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$"));
}
}
测试助手:
private static HttpClient SetClient()
{
HttpClient client;
var host = new WebHostBuilder()
.UseEnvironment("Development")
.UseStartup<Startup>()
;
var server = new TestServer(host);
client = new HttpClient {BaseAddress = new Uri(Configurations["Tests:ApiClientUrl"], UriKind.RelativeOrAbsolute)};
return client;
}
public static string GetTestUserToken()
{
var token = "someToken"; // there is an actual token here but I have removed it
return token;
}
public static AuthenticationHeaderValue GetTestUserBearerHeader()
{
var token = GetTestUserToken();
var bearerToken = new AuthenticationHeaderValue("Bearer", token);
return bearerToken;
}
预期结果: 测试通过(returns 令牌)或失败(returns 401 未授权)
实际结果:测试returns301源已被移动
响应值:
{StatusCode: 301, ReasonPhrase: 'Moved Permanently', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
Cache-Control: no-cache
Pragma: no-cache
Proxy-Connection: close
Connection: close
Content-Type: text/html; charset=utf-8
Content-Length: 668
}}
您需要将测试用例修改为
_client.DefaultRequestHeaders.Authorization = TestsHelper.GetTestUserBearerHeader();
var request = new HttpRequestMessage(new HttpMethod(httpMethod), url);
var response = await _client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
Assert.IsNotNull(content);
Assert.AreEqual(HttpStatusCode.OK,response.StatusCode);
更新
问题同时出现在 TestServer 和 Client
var server = new TestServer(Program.CreateWebHostBuilder(new string[] { }));
client = server.CreateClient();
client.BaseAddress = new Uri(Configurations["Tests:ApiClientUrl"]);
构建测试 http 客户端时使用:
server.CreateClient()
而不是 client = new HttpClient {BaseAddress = new Uri(Configurations["Tests:ApiClientUrl"], UriKind.RelativeOrAbsolute)};
您正在做的是行不通的,因为您正在构建的测试服务器实际上并没有在 http 级别公开端点。您使用测试服务器和测试客户端来测试您的 webapi/http 集成,同时测试基础设施将内存中的整个 http 协议栈短路。
https://andrewchaa.me.uk/programming/2019/03/01/unit-testing-with-ASP-NET-Core.html
我正在尝试对项目进行集成测试,但同样的错误不断出现。用邮递员手动做同样的事情很好。
我已经尝试更改端口号并启用 on/off SSL,但仍然出现同样的问题。
用户API测试:
[TestClass]
public class UserAPITest
{
private readonly HttpClient _client;
private readonly IConfiguration _configuration;
public UserAPITest()
{
_client = TestsHelper.Client;
_configuration = TestsHelper.Configurations;
}
[TestInitialize]
public void TestInitialize()
{
}
[TestMethod]
[DataRow(nameof(HttpMethod.Post), "api/User/refreshToken")]
public async Task Initiation_Authorized_ShouldReturnNewToken(string httpMethod, string url)
{
_client.DefaultRequestHeaders.Authorization = TestsHelper.GetTestUserBearerHeader();
var request = new HttpRequestMessage(new HttpMethod(httpMethod), url);
var response = await _client.SendAsync(request);
Assert.IsNotNull(response.Content); // value during debug added below
Assert.AreEqual(HttpStatusCode.OK,response.StatusCode); // test fails here
// check for token inside response
// Assert.IsTrue(Regex.IsMatch(response.RequestMessage.Content.ToString(), "^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$"));
}
}
测试助手:
private static HttpClient SetClient()
{
HttpClient client;
var host = new WebHostBuilder()
.UseEnvironment("Development")
.UseStartup<Startup>()
;
var server = new TestServer(host);
client = new HttpClient {BaseAddress = new Uri(Configurations["Tests:ApiClientUrl"], UriKind.RelativeOrAbsolute)};
return client;
}
public static string GetTestUserToken()
{
var token = "someToken"; // there is an actual token here but I have removed it
return token;
}
public static AuthenticationHeaderValue GetTestUserBearerHeader()
{
var token = GetTestUserToken();
var bearerToken = new AuthenticationHeaderValue("Bearer", token);
return bearerToken;
}
预期结果: 测试通过(returns 令牌)或失败(returns 401 未授权)
实际结果:测试returns301源已被移动
响应值:
{StatusCode: 301, ReasonPhrase: 'Moved Permanently', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
Cache-Control: no-cache
Pragma: no-cache
Proxy-Connection: close
Connection: close
Content-Type: text/html; charset=utf-8
Content-Length: 668
}}
您需要将测试用例修改为
_client.DefaultRequestHeaders.Authorization = TestsHelper.GetTestUserBearerHeader();
var request = new HttpRequestMessage(new HttpMethod(httpMethod), url);
var response = await _client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
Assert.IsNotNull(content);
Assert.AreEqual(HttpStatusCode.OK,response.StatusCode);
更新 问题同时出现在 TestServer 和 Client
var server = new TestServer(Program.CreateWebHostBuilder(new string[] { }));
client = server.CreateClient();
client.BaseAddress = new Uri(Configurations["Tests:ApiClientUrl"]);
构建测试 http 客户端时使用:
server.CreateClient()
而不是 client = new HttpClient {BaseAddress = new Uri(Configurations["Tests:ApiClientUrl"], UriKind.RelativeOrAbsolute)};
您正在做的是行不通的,因为您正在构建的测试服务器实际上并没有在 http 级别公开端点。您使用测试服务器和测试客户端来测试您的 webapi/http 集成,同时测试基础设施将内存中的整个 http 协议栈短路。
https://andrewchaa.me.uk/programming/2019/03/01/unit-testing-with-ASP-NET-Core.html