在ASP.NET WebApi 2中测试OWIN中间件2:如何设置cookie



我正在尝试在ASP.NET WebApi 2应用程序中测试OWIN中间件组件。中间件应该查看传入请求的cookie,在将请求交给下一个组件之前修改请求上的一些内容,并可能在发出请求时设置cookie。

让我感到困惑的是,OwinRequest.Cookies属性的类型是RequestCookieCollection,它似乎不允许修改,而属性本身是只读的,这意味着我不能使用RequestCookieCollection(IDictionary<string,string>)构造函数初始化已经有cookie的集合,并在请求时设置它。

我想做这样的事情:

var context = new OwinContext();
// option 1:
context.Request.Cookies.Append("cookie-name", "cookie-value");
// option 2:
var cookies = new RequestCookieCollection(new Dictionary<string, string>{ { "cookie-name", "cookie-value" } });
context.Request.Cookies = cookies;
await myMiddleware.Invoke(context);
// Assert stuff about context.Response

但由于上述原因,这并不奏效。

我曾希望不必完全模拟IOwinContext,因为用功能良好的RequestResponse对象来设置它非常方便(除其他外,我需要在实现中查看Request.User.Identity.IsAuthenticated(。

使用以下内容作为单元测试的示例中间件的主题

using RequestDelegate = Func<IOwinContext, Task>;
class MyMiddleware {
private readonly RequestDelegate next;
public MyMiddleware(RequestDelegate next) {
this.next = next;
}
public async Task Invoke(IOwinContext context) {
//The middleware is supposed to look at the cookies of the incoming request, 
var request = context.Request;
var authenticated = request.User.Identity.IsAuthenticated;
var cookies = request.Cookies;
if (cookies["cookie-name"] != null) {
// 1. check for existence of a cookie on the incoming request. 
//if the cookie exists, use its value to set a header, 
//so that the pipline after this component thinks the header was always present.
request.Headers.Append("header-name", "header-value");
}
//2. call the next component.
await next.Invoke(context);
//3. on the way out, check for some conditions, and possibly set a cookie value.
if (authenticated) {
context.Response.Cookies.Append("cookie-name", "cookie-value");
}
}
}

并且使用所提供的关于期望行为的细节,

很难用完整CCD_ 8所需的所有管道对控制器进行单元测试。

您已经说明了访问某些成员的许多限制。

然而,Owin提供了许多抽象,允许对隔离的单元测试模拟/存根/伪造所需的行为。

以下示例基于上面提供的主题,并使用Moqmocking框架以及具体实现来正确设置和执行主题中间件的独立单元测试。

[TestClass]
public class OwinMiddlewareCookiesTest {
[Test]
public async Task MyMiddleware_Should_Set_RequestHeader_And_ResponseHeader() {
//Arrange
var cookieStore = new Dictionary<string, string> { { "cookie-name", "cookie-value" } };
var cookies = new RequestCookieCollection(cookieStore);
var request = Mock.Of<IOwinRequest>();
var requestMock = Mock.Get(request);
requestMock.Setup(_ => _.Cookies).Returns(cookies);
requestMock.Setup(_ => _.User.Identity.IsAuthenticated).Returns(true);
requestMock.Setup(_ => _.Headers.Append(It.IsAny<string>(), It.IsAny<string>()));
var response = new OwinResponse();
var context = Mock.Of<OwinContext>();
var contextMock = Mock.Get(context);
contextMock.CallBase = true;
contextMock.Setup(_ => _.Request).Returns(request);
contextMock.Setup(_ => _.Response).Returns(response);
RequestDelegate next = _ => Task.FromResult((object)null);
var myMiddleware = new MyMiddleware(next);
//Act
await myMiddleware.Invoke(context);
//Assert
requestMock.Verify(_ => _.Headers.Append("header-name", "header-value"));
response.Headers.ContainsKey("Set-Cookie");
}
}

只需要模拟必要的依赖关系,测试就可以完成,并验证预期的行为。

由于请求实现的Cookies属性不能被重写,因此必须使用抽象。然而,这在模拟期望的行为方面提供了更大的灵活性。

相关内容

最新更新