|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | +using System.Net; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.AspNetCore.Builder; |
| 9 | +using Microsoft.AspNetCore.Hosting; |
| 10 | +using Microsoft.AspNetCore.Http; |
| 11 | +using Microsoft.AspNetCore.TestHost; |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace Microsoft.AspNetCore.Diagnostics |
| 16 | +{ |
| 17 | + public class StatusCodeMiddlewareTest |
| 18 | + { |
| 19 | + [Fact] |
| 20 | + public async Task Redirect_StatusPage() |
| 21 | + { |
| 22 | + var expectedStatusCode = 432; |
| 23 | + var destination = "/location"; |
| 24 | + var builder = new WebHostBuilder() |
| 25 | + .Configure(app => |
| 26 | + { |
| 27 | + app.UseStatusCodePagesWithRedirects("/errorPage?id={0}"); |
| 28 | + |
| 29 | + app.Map(destination, (innerAppBuilder) => |
| 30 | + { |
| 31 | + innerAppBuilder.Run((httpContext) => |
| 32 | + { |
| 33 | + httpContext.Response.StatusCode = expectedStatusCode; |
| 34 | + return Task.FromResult(1); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + app.Map("/errorPage", (innerAppBuilder) => |
| 39 | + { |
| 40 | + innerAppBuilder.Run(async (httpContext) => |
| 41 | + { |
| 42 | + await httpContext.Response.WriteAsync(httpContext.Request.QueryString.Value); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + app.Run((context) => |
| 47 | + { |
| 48 | + throw new InvalidOperationException($"Invalid input provided. {context.Request.Path}"); |
| 49 | + }); |
| 50 | + }); |
| 51 | + var expectedQueryString = $"?id={expectedStatusCode}"; |
| 52 | + var expectedUri = $"/errorPage{expectedQueryString}"; |
| 53 | + using var server = new TestServer(builder); |
| 54 | + var client = server.CreateClient(); |
| 55 | + var response = await client.GetAsync(destination); |
| 56 | + Assert.Equal(HttpStatusCode.Found, response.StatusCode); |
| 57 | + Assert.Equal(expectedUri, response.Headers.First(s => s.Key == "Location").Value.First()); |
| 58 | + |
| 59 | + response = await client.GetAsync(expectedUri); |
| 60 | + var content = await response.Content.ReadAsStringAsync(); |
| 61 | + Assert.Equal(expectedQueryString, content); |
| 62 | + Assert.Equal(expectedQueryString, response.RequestMessage.RequestUri.Query); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async Task Reexecute_CanRetrieveInformationAboutOriginalRequest() |
| 67 | + { |
| 68 | + var expectedStatusCode = 432; |
| 69 | + var destination = "/location"; |
| 70 | + var builder = new WebHostBuilder() |
| 71 | + .Configure(app => |
| 72 | + { |
| 73 | + app.Use(async (context, next) => |
| 74 | + { |
| 75 | + var beforeNext = context.Request.QueryString; |
| 76 | + await next(); |
| 77 | + var afterNext = context.Request.QueryString; |
| 78 | + |
| 79 | + Assert.Equal(beforeNext, afterNext); |
| 80 | + }); |
| 81 | + app.UseStatusCodePagesWithReExecute(pathFormat: "/errorPage", queryFormat: "?id={0}"); |
| 82 | + |
| 83 | + app.Map(destination, (innerAppBuilder) => |
| 84 | + { |
| 85 | + innerAppBuilder.Run((httpContext) => |
| 86 | + { |
| 87 | + httpContext.Response.StatusCode = expectedStatusCode; |
| 88 | + return Task.FromResult(1); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + app.Map("/errorPage", (innerAppBuilder) => |
| 93 | + { |
| 94 | + innerAppBuilder.Run(async (httpContext) => |
| 95 | + { |
| 96 | + var statusCodeReExecuteFeature = httpContext.Features.Get<IStatusCodeReExecuteFeature>(); |
| 97 | + await httpContext.Response.WriteAsync( |
| 98 | + httpContext.Request.QueryString.Value |
| 99 | + + ", " |
| 100 | + + statusCodeReExecuteFeature.OriginalPath |
| 101 | + + ", " |
| 102 | + + statusCodeReExecuteFeature.OriginalQueryString); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + app.Run((context) => |
| 107 | + { |
| 108 | + throw new InvalidOperationException("Invalid input provided."); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + using var server = new TestServer(builder); |
| 113 | + var client = server.CreateClient(); |
| 114 | + var response = await client.GetAsync(destination + "?name=James"); |
| 115 | + var content = await response.Content.ReadAsStringAsync(); |
| 116 | + Assert.Equal($"?id={expectedStatusCode}, /location, ?name=James", content); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public async Task Reexecute_ClearsEndpointAndRouteData() |
| 121 | + { |
| 122 | + var expectedStatusCode = 432; |
| 123 | + var destination = "/location"; |
| 124 | + var builder = new WebHostBuilder() |
| 125 | + .Configure(app => |
| 126 | + { |
| 127 | + app.UseStatusCodePagesWithReExecute(pathFormat: "/errorPage", queryFormat: "?id={0}"); |
| 128 | + |
| 129 | + app.Use((context, next) => |
| 130 | + { |
| 131 | + Assert.Empty(context.Request.RouteValues); |
| 132 | + Assert.Null(context.GetEndpoint()); |
| 133 | + return next(); |
| 134 | + }); |
| 135 | + |
| 136 | + app.Map(destination, (innerAppBuilder) => |
| 137 | + { |
| 138 | + innerAppBuilder.Run((httpContext) => |
| 139 | + { |
| 140 | + httpContext.SetEndpoint(new Endpoint((_) => Task.CompletedTask, new EndpointMetadataCollection(), "Test")); |
| 141 | + httpContext.Request.RouteValues["John"] = "Doe"; |
| 142 | + httpContext.Response.StatusCode = expectedStatusCode; |
| 143 | + return Task.CompletedTask; |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + app.Map("/errorPage", (innerAppBuilder) => |
| 148 | + { |
| 149 | + innerAppBuilder.Run(async (httpContext) => |
| 150 | + { |
| 151 | + var statusCodeReExecuteFeature = httpContext.Features.Get<IStatusCodeReExecuteFeature>(); |
| 152 | + await httpContext.Response.WriteAsync( |
| 153 | + httpContext.Request.QueryString.Value |
| 154 | + + ", " |
| 155 | + + statusCodeReExecuteFeature.OriginalPath |
| 156 | + + ", " |
| 157 | + + statusCodeReExecuteFeature.OriginalQueryString); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + app.Run((context) => |
| 162 | + { |
| 163 | + throw new InvalidOperationException("Invalid input provided."); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + using var server = new TestServer(builder); |
| 168 | + var client = server.CreateClient(); |
| 169 | + var response = await client.GetAsync(destination + "?name=James"); |
| 170 | + var content = await response.Content.ReadAsStringAsync(); |
| 171 | + Assert.Equal($"?id={expectedStatusCode}, /location, ?name=James", content); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments