From f6f5e575fb5302df74d4b52d5159a1bf8e4d6c3c Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 22 Mar 2018 13:11:29 -0700 Subject: [PATCH] Test more things --- .../Server/IISHttpContext.cs | 7 ++++--- .../IISIntegration.FunctionalTests.csproj | 1 + .../Inprocess/ResponseHeaderTests.cs | 14 +++++++++++++- test/IISTestSite/Startup.cs | 10 ++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs index e7fe04d9d4..fb7f7cbb2f 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs @@ -332,9 +332,10 @@ namespace Microsoft.AspNetCore.Server.IISIntegration public unsafe void SendResponseHeaders(bool appCompleted) { // Verifies we have sent the statuscode before writing a header - var reasonPhrase = string.IsNullOrWhiteSpace(ReasonPhrase) ? ReasonPhrases.GetReasonPhrase(StatusCode) : ReasonPhrase; - var reasonPhraseBytes = Encoding.UTF8.GetBytes(reasonPhrase); - + var reasonPhrase = string.IsNullOrEmpty(ReasonPhrase) ? ReasonPhrases.GetReasonPhrase(StatusCode) : ReasonPhrase; + var reasonPhraseBytes = new byte [reasonPhrase.Length + 1]; + Encoding.ASCII.GetBytes(reasonPhrase, 0, reasonPhrase.Length, reasonPhraseBytes, 0); + fixed (byte* pReasonPhrase = reasonPhraseBytes) { Debug.Assert((IntPtr)pReasonPhrase != IntPtr.Zero); diff --git a/test/IISIntegration.FunctionalTests/IISIntegration.FunctionalTests.csproj b/test/IISIntegration.FunctionalTests/IISIntegration.FunctionalTests.csproj index 77f3646a70..c09eeee095 100644 --- a/test/IISIntegration.FunctionalTests/IISIntegration.FunctionalTests.csproj +++ b/test/IISIntegration.FunctionalTests/IISIntegration.FunctionalTests.csproj @@ -10,6 +10,7 @@ + diff --git a/test/IISIntegration.FunctionalTests/Inprocess/ResponseHeaderTests.cs b/test/IISIntegration.FunctionalTests/Inprocess/ResponseHeaderTests.cs index 8cee6643a4..e54920e59d 100644 --- a/test/IISIntegration.FunctionalTests/Inprocess/ResponseHeaderTests.cs +++ b/test/IISIntegration.FunctionalTests/Inprocess/ResponseHeaderTests.cs @@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests Assert.Equal("1", headerValues.First()); Assert.Equal("2", headerValues.Last()); } - + [ConditionalFact] public async Task ErrorCodeIsSetForExceoptionDuringRequest() { @@ -48,5 +48,17 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); Assert.Equal("Internal Server Error", response.ReasonPhrase); } + + [ConditionalTheory] + [InlineData(1, "custom", "custom")] + [InlineData(500, "", "Internal Server Error")] + [InlineData(999, "", "")] + public async Task CustomErrorCodeWorks(int code, string reason, string expectedReason) + { + var response = await _fixture.Client.GetAsync($"SetCustomErorCode?code={code}&reason={reason}"); + Assert.Equal((HttpStatusCode)code, response.StatusCode); + Assert.Equal(expectedReason, response.ReasonPhrase); + Assert.Equal("Body", await response.Content.ReadAsStringAsync()); + } } } diff --git a/test/IISTestSite/Startup.cs b/test/IISTestSite/Startup.cs index a74524587a..28460cdaf5 100644 --- a/test/IISTestSite/Startup.cs +++ b/test/IISTestSite/Startup.cs @@ -218,6 +218,16 @@ namespace IISTestSite app.Run(ctx => { throw new Exception(); }); } + private void SetCustomErorCode(IApplicationBuilder app) + { + app.Run(async ctx => { + var feature = ctx.Features.Get(); + feature.ReasonPhrase = ctx.Request.Query["reason"]; + feature.StatusCode = int.Parse(ctx.Request.Query["code"]); + await ctx.Response.WriteAsync("Body"); + }); + } + private void HelloWorld(IApplicationBuilder app) { app.Run(async ctx =>