1. <legend id='V4Yp6'><style id='V4Yp6'><dir id='V4Yp6'><q id='V4Yp6'></q></dir></style></legend>

    <small id='V4Yp6'></small><noframes id='V4Yp6'>

      <bdo id='V4Yp6'></bdo><ul id='V4Yp6'></ul>

  2. <tfoot id='V4Yp6'></tfoot>

  3. <i id='V4Yp6'><tr id='V4Yp6'><dt id='V4Yp6'><q id='V4Yp6'><span id='V4Yp6'><b id='V4Yp6'><form id='V4Yp6'><ins id='V4Yp6'></ins><ul id='V4Yp6'></ul><sub id='V4Yp6'></sub></form><legend id='V4Yp6'></legend><bdo id='V4Yp6'><pre id='V4Yp6'><center id='V4Yp6'></center></pre></bdo></b><th id='V4Yp6'></th></span></q></dt></tr></i><div id='V4Yp6'><tfoot id='V4Yp6'></tfoot><dl id='V4Yp6'><fieldset id='V4Yp6'></fieldset></dl></div>

      如何在 ASP.NET 5/MVC 6 的单元测试中访问 HttpContext

      How to access HttpContext inside a unit test in ASP.NET 5 / MVC 6(如何在 ASP.NET 5/MVC 6 的单元测试中访问 HttpContext)
          <tbody id='ro3B3'></tbody>
        <legend id='ro3B3'><style id='ro3B3'><dir id='ro3B3'><q id='ro3B3'></q></dir></style></legend>
        <i id='ro3B3'><tr id='ro3B3'><dt id='ro3B3'><q id='ro3B3'><span id='ro3B3'><b id='ro3B3'><form id='ro3B3'><ins id='ro3B3'></ins><ul id='ro3B3'></ul><sub id='ro3B3'></sub></form><legend id='ro3B3'></legend><bdo id='ro3B3'><pre id='ro3B3'><center id='ro3B3'></center></pre></bdo></b><th id='ro3B3'></th></span></q></dt></tr></i><div id='ro3B3'><tfoot id='ro3B3'></tfoot><dl id='ro3B3'><fieldset id='ro3B3'></fieldset></dl></div>

            <bdo id='ro3B3'></bdo><ul id='ro3B3'></ul>

              <tfoot id='ro3B3'></tfoot>

              <small id='ro3B3'></small><noframes id='ro3B3'>

                本文介绍了如何在 ASP.NET 5/MVC 6 的单元测试中访问 HttpContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我在中间件的 http 上下文中设置了一个值.例如 HttpContext.User.

                Lets say I am setting a value on the http context in my middleware. For example HttpContext.User.

                如何在我的单元测试中测试 http 上下文.这是我正在尝试做的一个例子

                How can test the http context in my unit test. Here is an example of what I am trying to do

                中间件

                public class MyAuthMiddleware
                {
                    private readonly RequestDelegate _next;
                
                    public MyAuthMiddleware(RequestDelegate next)
                    {
                        _next = next;
                    }
                
                    public async Task Invoke(HttpContext context)
                    {
                        context.User = SetUser(); 
                        await next(context);
                    }
                }
                

                测试

                [Fact]
                public async Task UserShouldBeAuthenticated()
                {
                    var server = TestServer.Create((app) => 
                    {
                        app.UseMiddleware<MyAuthMiddleware>();
                    });
                
                    using(server)
                    {
                        var response = await server.CreateClient().GetAsync("/");
                        // After calling the middleware I want to assert that 
                        // the user in the HttpContext was set correctly
                        // but how can I access the HttpContext here?
                    }
                }
                

                推荐答案

                以下是您可以使用的两种方法:

                Following are two approaches you could use:

                // Directly test the middleware itself without setting up the pipeline
                [Fact]
                public async Task Approach1()
                {
                    // Arrange
                    var httpContext = new DefaultHttpContext();
                    var authMiddleware = new MyAuthMiddleware(next: (innerHttpContext) => Task.FromResult(0));
                
                    // Act
                    await authMiddleware.Invoke(httpContext);
                
                    // Assert
                    // Note that the User property on DefaultHttpContext is never null and so do
                    // specific checks for the contents of the principal (ex: claims)
                    Assert.NotNull(httpContext.User);
                    var claims = httpContext.User.Claims;
                    //todo: verify the claims
                }
                
                [Fact]
                public async Task Approach2()
                {
                    // Arrange
                    var server = TestServer.Create((app) =>
                    {
                        app.UseMiddleware<MyAuthMiddleware>();
                
                        app.Run(async (httpContext) =>
                        {
                            if(httpContext.User != null)
                            {
                                await httpContext.Response.WriteAsync("Claims: "
                                    + string.Join(
                                        ",",
                                        httpContext.User.Claims.Select(claim => string.Format("{0}:{1}", claim.Type, claim.Value))));
                            }
                        });
                    });
                
                    using (server)
                    {
                        // Act
                        var response = await server.CreateClient().GetAsync("/");
                
                        // Assert
                        var actual = await response.Content.ReadAsStringAsync();
                        Assert.Equal("Claims: ClaimType1:ClaimType1-value", actual);
                    }
                }
                

                这篇关于如何在 ASP.NET 5/MVC 6 的单元测试中访问 HttpContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                Test if a class has an attribute?(测试一个类是否有属性?)
                asp.net mvc Adding to the AUTHORIZE attribute(asp.net mvc 添加到 AUTHORIZE 属性)
                Crystal Reports: Passing Multiline Parameter Values(Crystal Reports:传递多行参数值)
                Bar code font error with crystal report on web server(Web 服务器上水晶报告的条形码字体错误)
                space between Crystal Reports Cross-Tab Column(Crystal Reports 交叉表列之间的空间)

                • <bdo id='1qLdi'></bdo><ul id='1qLdi'></ul>

                    <legend id='1qLdi'><style id='1qLdi'><dir id='1qLdi'><q id='1qLdi'></q></dir></style></legend>

                          <small id='1qLdi'></small><noframes id='1qLdi'>

                          <tfoot id='1qLdi'></tfoot>
                            <tbody id='1qLdi'></tbody>
                        1. <i id='1qLdi'><tr id='1qLdi'><dt id='1qLdi'><q id='1qLdi'><span id='1qLdi'><b id='1qLdi'><form id='1qLdi'><ins id='1qLdi'></ins><ul id='1qLdi'></ul><sub id='1qLdi'></sub></form><legend id='1qLdi'></legend><bdo id='1qLdi'><pre id='1qLdi'><center id='1qLdi'></center></pre></bdo></b><th id='1qLdi'></th></span></q></dt></tr></i><div id='1qLdi'><tfoot id='1qLdi'></tfoot><dl id='1qLdi'><fieldset id='1qLdi'></fieldset></dl></div>