asp.net - 使用 moq 模拟 HttpMessageHandler - 如何获取请求的内容?
问题描述
在决定我要发回什么样的响应进行测试之前,有没有办法获取 http 请求的内容?多个测试将使用此类,并且每个测试将有多个 http 请求。此代码无法编译,因为 lambda 不是异步的,并且其中有一个 await。我是 async-await 的新手,所以我不确定如何解决这个问题。我曾短暂考虑过拥有多个 TestHttpClientFactories,但这意味着重复的代码,所以如果可能的话,决定反对它。任何帮助表示赞赏。
public class TestHttpClientFactory : IHttpClientFactory
{
public HttpClient CreateClient(string name)
{
var messageHandlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
messageHandlerMock.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync((HttpRequestMessage request, CancellationToken token) =>
{
HttpResponseMessage response = new HttpResponseMessage();
var requestMessageContent = await request.Content.ReadAsStringAsync();
// decide what to put in the response after looking at the contents of the request
return response;
})
.Verifiable();
var httpClient = new HttpClient(messageHandlerMock.Object);
return httpClient;
}
}
解决方案
To take advantage of the async delegate use the Returns
method instead
public class TestHttpClientFactory : IHttpClientFactory {
public HttpClient CreateClient(string name) {
var messageHandlerMock = new Mock<HttpMessageHandler>(MockBehavior.Strict);
messageHandlerMock.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.Returns(async (HttpRequestMessage request, CancellationToken token) => {
string requestMessageContent = await request.Content.ReadAsStringAsync();
HttpResponseMessage response = new HttpResponseMessage();
//...decide what to put in the response after looking at the contents of the request
return response;
})
.Verifiable();
var httpClient = new HttpClient(messageHandlerMock.Object);
return httpClient;
}
}
Or consider creating your own handler that exposes a delegate to handle the desired behavior.
For example
public class DelegatingHandlerStub : DelegatingHandler {
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _handlerFunc;
public DelegatingHandlerStub() {
_handlerFunc = (request, cancellationToken) => Task.FromResult(request.CreateResponse(HttpStatusCode.OK));
}
public DelegatingHandlerStub(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handlerFunc) {
_handlerFunc = handlerFunc;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
return _handlerFunc(request, cancellationToken);
}
}
And used in the factory like this
public class TestHttpClientFactory : IHttpClientFactory {
public HttpClient CreateClient(string name) {
var messageHandlerMock = new DelegatingHandlerStub(async (HttpRequestMessage request, CancellationToken token) => {
string requestMessageContent = await request.Content.ReadAsStringAsync();
HttpResponseMessage response = new HttpResponseMessage();
//...decide what to put in the response after looking at the contents of the request
return response;
});
var httpClient = new HttpClient(messageHandlerMock);
return httpClient;
}
}
推荐阅读
- facebook-prophet - 使用 Prophet 从观察中去除乘法季节性和趋势?
- amazon-dynamodb - 生存时间是否会导致 DynamoDB 全局表中的竞争条件?
- python - Mido midi - 如果我在键盘上弹奏,找到两倍的 Note_On 事件,而不是在钢琴卷中绘制音符......?
- javascript - Vue - 在浏览器在线或离线时设置不同的图标
- sql - 如何在 sql 中使用 Substring 命令将一个字段拆分为 10 个新字段
- javascript - JS innerHTML 脚本解析
- linux - 创建一个显示较少删除行的补丁
- python - 运行超时子进程和管道结果后挂起父进程
- environment-variables - 错误替换:将 env 中的密钥带到 yml 文件中
- sql - 如何从 AM/PM 格式的 VARCHAR 转换为 UTC?