简体   繁体   English

如何在 FakeHttpContext 中设置 Request.Header 进行单元测试

[英]How to setup Request.Header in FakeHttpContext for Unit Testing

I have a FakeHttpContext I have been trying to modify to include some headers for testing purposes我有一个FakeHttpContext我一直在尝试修改以包含一些用于测试目的的标头

public static HttpContext FakeHttpContext()
{
    var httpRequest = new HttpRequest("", "http://stackoverflow/", "");
    var stringWriter = new StringWriter();
    var httpResponse = new HttpResponse(stringWriter);
    var httpContext = new HttpContext(httpRequest, httpResponse);   

    var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
                                                    new HttpStaticObjectsCollection(), 10, true,
                                                    HttpCookieMode.AutoDetect,
                                                    SessionStateMode.InProc, false);

    httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
                                        BindingFlags.NonPublic | BindingFlags.Instance,
                                        null, CallingConventions.Standard,
                                        new[] { typeof(HttpSessionStateContainer) },
                                        null)
                                .Invoke(new object[] { sessionContainer });

    return httpContext;
 }

This works without the headers but when I add any of these lines of code in between the httpRequest and stringWriter lines.此作品,未经头,但是当我添加任何的代码行放在HttpRequest的StringWriter线之间。

    httpRequest.Headers.Add("blah", "1234");
    httpRequest.Headers["blah"] = "1234";

It throws它抛出

An exception of type 'System.PlatformNotSupportedException' occurred in System.Web.dll but was not handled in user code System.Web.dll 中出现“System.PlatformNotSupportedException”类型的异常,但未在用户代码中处理

  • Why am I getting that exception?为什么我得到那个例外?
  • Is there a possible way to add headers to HttpContext for testing WebApi controllers ?有没有可能的方法将标头添加到HttpContext以测试WebApi 控制器

I just discovered that with HttpRequestMessage class , you can easily add headers for testing your WebAPI controllers without having to create any fake HttpContext .我刚刚发现使用HttpRequestMessage 类,您可以轻松添加用于测试WebAPI 控制器的标头,而无需创建任何虚假的HttpContext

var request = new HttpRequestMessage(HttpMethod.Get, "http://stackoverflow");
request.Headers.Add("deviceId","1234");
_myController.Request = request;

I'm using .Net Core 3.1 and the Moq framework.我正在使用 .Net Core 3.1 和 Moq 框架。 I created a DefaultHttpContext, set the headers there, finally assigned it to the controller ControllerContext constructor.我创建了一个 DefaultHttpContext,在那里设置标题,最后将它分配给控制器 ControllerContext 构造函数。

var httpContext = new DefaultHttpContext();
httpContext.Request.Headers["X-Example"] = "test-header";
var controller = new ExampleController(mockClient.Object, mockLogger.Object) 
{ 
    ControllerContext = new ControllerContext()
    {
        HttpContext = httpContext
    }
};

Source: https://www.thecodebuzz.com/unit-test-mock-httprequest-in-asp-net-core-controller/来源: https : //www.thecodebuzz.com/unit-test-mock-httprequest-in-asp-net-core-controller/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM