有什么方法可以 get/test Mock HttpMessageHandler 中的实际请求主体吗?
Are there any ways to get/test the actual request body in Mock HttpMessageHandler?
public async AddName(){
var requestUri = "someuri";
var payload = new AddNameRequest()
{
NameParts = new string[] { "Name001" },
Formula = string.Empty
};
// Act
var responseMessage = await this.httpClient.PostAsJsonAsync(requestUri, payload);
}
并对其进行单元测试:
public async AddName_ExpectedBeh(){
//Create URI
//Create responseMessage
var mockHandler = new Mock<HttpMessageHandler>();
mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(r => r.Method == responseMessage.RequestMessage.Method && r.RequestUri.ToString().EndsWith(responseMessage.RequestMessage.RequestUri.ToString())),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(responseMessage);
var httpClient = new HttpClient(mockHandler.Object);
//Pass HttpClient to Main Class
//Do Request
//Assert the response
}
之前单元测试的问题是无法测试payload(request body)。我看到 mockHandler 可以读取 HttpRequestMessage,但我无法在 mockHandler.Setup() 之外读取它。那么有什么方法可以 get/test HttpRequestMessage 吗?我该怎么做?
例如,我想检查请求正文是否包含“Name001”。
负载在设置方法中可用。你可以测试它的方法是你可以添加一个方法,你可以在其中测试你想要的东西。例如
public async Task<bool> IsRequestValid(HttpRequestMessage requestMessage)
{
string stringContent = await requestMessage.Content.ReadAsStringAsync();
// Example of some validation...
return stringContent.Contains("myProperty");
}
如果此方法 return 为真,则 mockHandler 将 return 所需的消息,否则它将 return 默认(空)。这样您就可以知道您的请求是正确的。或者,您可以通过此方法抛出异常或 Assert.False().
您可以使用 && IsRequestValid(r.Content)
.
在现有的设置表达式中附加此检查
public async AddName(){
var requestUri = "someuri";
var payload = new AddNameRequest()
{
NameParts = new string[] { "Name001" },
Formula = string.Empty
};
// Act
var responseMessage = await this.httpClient.PostAsJsonAsync(requestUri, payload);
}
并对其进行单元测试:
public async AddName_ExpectedBeh(){
//Create URI
//Create responseMessage
var mockHandler = new Mock<HttpMessageHandler>();
mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(r => r.Method == responseMessage.RequestMessage.Method && r.RequestUri.ToString().EndsWith(responseMessage.RequestMessage.RequestUri.ToString())),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(responseMessage);
var httpClient = new HttpClient(mockHandler.Object);
//Pass HttpClient to Main Class
//Do Request
//Assert the response
}
之前单元测试的问题是无法测试payload(request body)。我看到 mockHandler 可以读取 HttpRequestMessage,但我无法在 mockHandler.Setup() 之外读取它。那么有什么方法可以 get/test HttpRequestMessage 吗?我该怎么做? 例如,我想检查请求正文是否包含“Name001”。
负载在设置方法中可用。你可以测试它的方法是你可以添加一个方法,你可以在其中测试你想要的东西。例如
public async Task<bool> IsRequestValid(HttpRequestMessage requestMessage)
{
string stringContent = await requestMessage.Content.ReadAsStringAsync();
// Example of some validation...
return stringContent.Contains("myProperty");
}
如果此方法 return 为真,则 mockHandler 将 return 所需的消息,否则它将 return 默认(空)。这样您就可以知道您的请求是正确的。或者,您可以通过此方法抛出异常或 Assert.False().
您可以使用 && IsRequestValid(r.Content)
.