gpt4 book ai didi

asp.net-mvc - 这仍然是使用需要 HttpRequest 和 HttpResponse 的 API 进行单元测试的推荐方法吗?

转载 作者:行者123 更新时间:2023-12-01 06:29:27 24 4
gpt4 key购买 nike

引用this question特别是 this answer ,已投票但未标记。我不喜欢标记的答案,因为我需要对 Controller 进行单元测试。

在 Application_BeginRequest 期间将它们放入 HttpRequest 和 HttpResponse 中有点难闻,但我正在处理一个 ComponentSpace SAML 2 library将这些类型作为方法参数。有没有更好的方法以单元可测试的方式访问 MVC 中的这些内容?

这是我现在正在使用的。可以将 .Items 重构为一个扩展方法来封装 cast 和 magic string,但这是主要思想:

Global.asax:

protected void Application_BeginRequest()
{
// put the raw request in context items for use by ComponentSpace SAML lib
Context.Items["HttpRequest"] = Context.Request;
Context.Items["HttpResponse"] = Context.Response;
}

在服务方法中:

// HttpContext is an instance of HttpContextBase
ServiceProvider.SendAuthnRequestByHTTPPost(
HttpContext.Items["HttpResponse"] as HttpResponse,
authnRequest.Destination, authnRequestXml, relayState);

ServiceProvider.ReceiveSAMLResponseByHTTPPost(
HttpContext.Items["HttpRequest"] as HttpRequest,
out samlResponseXml, out relayState);

最佳答案

我不明白您的开始请求中需要以下行

 Context.Items["HttpRequest"] = Context.Request;

如果你的需求只是下面这行

 HttpContext.Items["HttpRequest"] as HttpRequest,

您可以通过执行以下操作获得相同的效果

HttpContext.Current.Request

关于asp.net-mvc - 这仍然是使用需要 HttpRequest 和 HttpResponse 的 API 进行单元测试的推荐方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8899370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com