当前位置: 首页>>代码示例>>C#>>正文


C# TestServer.CreateRequest方法代码示例

本文整理汇总了C#中Microsoft.AspNet.TestHost.TestServer.CreateRequest方法的典型用法代码示例。如果您正苦于以下问题:C# TestServer.CreateRequest方法的具体用法?C# TestServer.CreateRequest怎么用?C# TestServer.CreateRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.AspNet.TestHost.TestServer的用法示例。


在下文中一共展示了TestServer.CreateRequest方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CorsRequest_MatchPolicy_SetsResponseHeaders

        public async Task CorsRequest_MatchPolicy_SetsResponseHeaders()
        {
            // Arrange
            var appBuilder = new WebApplicationBuilder()
                .Configure(app =>
                {
                    app.UseCors(builder =>
                        builder.WithOrigins("http://localhost:5001")
                               .WithMethods("PUT")
                               .WithHeaders("Header1")
                               .WithExposedHeaders("AllowedHeader"));
                    app.Run(async context =>
                    {
                        await context.Response.WriteAsync("Cross origin response");
                    });
                })
                .ConfigureServices(services => services.AddCors());

            using (var server = new TestServer(appBuilder))
            {
                // Act
                // Actual request.
                var response = await server.CreateRequest("/")
                    .AddHeader(CorsConstants.Origin, "http://localhost:5001")
                    .SendAsync("PUT");

                // Assert
                response.EnsureSuccessStatusCode();
                Assert.Equal(2, response.Headers.Count());
                Assert.Equal("Cross origin response", await response.Content.ReadAsStringAsync());
                Assert.Equal("http://localhost:5001", response.Headers.GetValues(CorsConstants.AccessControlAllowOrigin).FirstOrDefault());
                Assert.Equal("AllowedHeader", response.Headers.GetValues(CorsConstants.AccessControlExposeHeaders).FirstOrDefault());
            }
        }
开发者ID:leloulight,项目名称:CORS,代码行数:34,代码来源:CorsMiddlewareTests.cs

示例2: AddRequestHeader

 public void AddRequestHeader()
 {
     var builder = new WebApplicationBuilder().Configure(app => { });
     var server = new TestServer(builder);
     server.CreateRequest("/")
         .AddHeader("Host", "MyHost:90")
         .And(request =>
         {
             Assert.Equal("MyHost:90", request.Headers.Host.ToString());
         });
 }
开发者ID:leloulight,项目名称:Hosting,代码行数:11,代码来源:RequestBuilderTests.cs

示例3: AddContentHeaders

 public void AddContentHeaders()
 {
     var builder = new WebApplicationBuilder().Configure(app => { });
     var server = new TestServer(builder);
     server.CreateRequest("/")
         .AddHeader("Content-Type", "Test/Value")
         .And(request =>
         {
             Assert.NotNull(request.Content);
             Assert.Equal("Test/Value", request.Content.Headers.ContentType.ToString());
         });
 }
开发者ID:leloulight,项目名称:Hosting,代码行数:12,代码来源:RequestBuilderTests.cs

示例4: PreFlight_MatchesPolicy_SetsResponseHeaders

        public async Task PreFlight_MatchesPolicy_SetsResponseHeaders()
        {
            // Arrange
            var policy = new CorsPolicy();
            policy.Origins.Add("http://localhost:5001");
            policy.Methods.Add("PUT");
            policy.Headers.Add("Header1");
            policy.ExposedHeaders.Add("AllowedHeader");

            var appBuilder = new WebApplicationBuilder()
                .Configure(app =>
                {
                    app.UseCors("customPolicy");
                    app.Run(async context =>
                    {
                        await context.Response.WriteAsync("Cross origin response");
                    });
                })
                .ConfigureServices(services =>
                {
                    services.AddCors(options =>
                    {
                        options.AddPolicy("customPolicy", policy);
                    });
                });

            using (var server = new TestServer(appBuilder))
            {
                // Act
                // Preflight request.
                var response = await server.CreateRequest("/")
                    .AddHeader(CorsConstants.Origin, "http://localhost:5001")
                    .AddHeader(CorsConstants.AccessControlRequestMethod, "PUT")
                    .SendAsync(CorsConstants.PreflightHttpMethod);

                // Assert
                response.EnsureSuccessStatusCode();
                Assert.Equal(2, response.Headers.Count());
                Assert.Equal("http://localhost:5001", response.Headers.GetValues(CorsConstants.AccessControlAllowOrigin).FirstOrDefault());
                Assert.Equal("PUT", response.Headers.GetValues(CorsConstants.AccessControlAllowMethods).FirstOrDefault());
            }
        }
开发者ID:leloulight,项目名称:CORS,代码行数:42,代码来源:CorsMiddlewareTests.cs

示例5: CorsRequest_DoesNotMatchPolicy_DoesNotSetHeaders

        public async Task CorsRequest_DoesNotMatchPolicy_DoesNotSetHeaders()
        {
            // Arrange
            var appBuilder = new WebApplicationBuilder()
                .Configure(app =>
                {
                    app.UseCors(builder =>
                        builder.WithOrigins("http://localhost:5001")
                               .WithMethods("PUT")
                               .WithHeaders("Header1")
                               .WithExposedHeaders("AllowedHeader"));
                    app.Run(async context =>
                    {
                        await context.Response.WriteAsync("Cross origin response");
                    });
                })
                .ConfigureServices(services => services.AddCors());

            using (var server = new TestServer(appBuilder))
            {
                // Act
                // Actual request.
                var response = await server.CreateRequest("/")
                    .AddHeader(CorsConstants.Origin, "http://localhost:5002")
                    .SendAsync("PUT");

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Empty(response.Headers);
            }
        }
开发者ID:leloulight,项目名称:CORS,代码行数:31,代码来源:CorsMiddlewareTests.cs


注:本文中的Microsoft.AspNet.TestHost.TestServer.CreateRequest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。