From dea3eb7856a22a8c07e2f4acacd4281e6141a057 Mon Sep 17 00:00:00 2001 From: Artak Mkrtchyan Date: Wed, 31 Jan 2018 17:20:29 -0800 Subject: [PATCH 1/3] Not instantiating the RequestHeaders and not relying on TypedHeaders to parse AcceptCharset --- .../Formatters/TextOutputFormatter.cs | 15 +++++++++-- .../Formatters/TextOutputFormatterTests.cs | 25 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/TextOutputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/TextOutputFormatter.cs index 7b8bf1def3..fb2a07a1fb 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/TextOutputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/TextOutputFormatter.cs @@ -76,8 +76,8 @@ namespace Microsoft.AspNetCore.Mvc.Formatters throw new InvalidOperationException(message); } - var request = context.HttpContext.Request; - var encoding = MatchAcceptCharacterEncoding(request.GetTypedHeaders().AcceptCharset); + var acceptCharsetHeaderValues = GetAcceptCharsetHeaderValues(context); + var encoding = MatchAcceptCharacterEncoding(acceptCharsetHeaderValues); if (encoding != null) { return encoding; @@ -165,6 +165,17 @@ namespace Microsoft.AspNetCore.Mvc.Formatters /// A task which can write the response body. public abstract Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding); + internal static IList GetAcceptCharsetHeaderValues(OutputFormatterWriteContext context) + { + var request = context.HttpContext.Request; + if (StringWithQualityHeaderValue.TryParseList(request.Headers[HeaderNames.AcceptCharset], out IList result)) + { + return result; + } + + return null; + } + private string GetMediaTypeWithCharset(string mediaType, Encoding encoding) { if (string.Equals(encoding.WebName, Encoding.UTF8.WebName, StringComparison.OrdinalIgnoreCase) && diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/TextOutputFormatterTests.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/TextOutputFormatterTests.cs index c81760587e..3bea160842 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/TextOutputFormatterTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/TextOutputFormatterTests.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -233,6 +234,30 @@ namespace Microsoft.AspNetCore.Mvc.Formatters Assert.Equal(StatusCodes.Status406NotAcceptable, context.HttpContext.Response.StatusCode); } + [Fact] + public void GetAcceptCharsetHeaderValues_Succeeds() + { + // Arrange + const string testCharsetValue = "fakeValue"; + + var formatter = new OverrideEncodingFormatter(encoding: null); + var context = new DefaultHttpContext(); + context.Request.Headers[HeaderNames.AcceptCharset] = testCharsetValue; + + var writerContext = new OutputFormatterWriteContext( + context, + new TestHttpResponseStreamWriterFactory().CreateWriter, + objectType: null, + @object: null); + + // Act + var result = TextOutputFormatter.GetAcceptCharsetHeaderValues(writerContext); + + //Assert + Assert.Equal(1, result.Count); + Assert.Equal(testCharsetValue, result.Single().Value); + } + private class TestOutputFormatter : TextOutputFormatter { public TestOutputFormatter() From 7a13eb8b21f20d83a0e8092e1a7b5ae822969eb2 Mon Sep 17 00:00:00 2001 From: Artak Mkrtchyan Date: Thu, 1 Feb 2018 14:44:06 -0800 Subject: [PATCH 2/3] Addressed review feedback --- .../Formatters/TextOutputFormatterTests.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/TextOutputFormatterTests.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/TextOutputFormatterTests.cs index 3bea160842..53420ac38d 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/TextOutputFormatterTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Formatters/TextOutputFormatterTests.cs @@ -235,14 +235,14 @@ namespace Microsoft.AspNetCore.Mvc.Formatters } [Fact] - public void GetAcceptCharsetHeaderValues_Succeeds() + public void GetAcceptCharsetHeaderValues_ReadsHeaderAndParsesValues() { // Arrange - const string testCharsetValue = "fakeValue"; + const string expectedValue = "expected"; var formatter = new OverrideEncodingFormatter(encoding: null); var context = new DefaultHttpContext(); - context.Request.Headers[HeaderNames.AcceptCharset] = testCharsetValue; + context.Request.Headers[HeaderNames.AcceptCharset] = expectedValue; var writerContext = new OutputFormatterWriteContext( context, @@ -254,8 +254,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters var result = TextOutputFormatter.GetAcceptCharsetHeaderValues(writerContext); //Assert - Assert.Equal(1, result.Count); - Assert.Equal(testCharsetValue, result.Single().Value); + Assert.Equal(expectedValue, Assert.Single(result).Value.Value); } private class TestOutputFormatter : TextOutputFormatter From 8b0c6a825f03d5f1a3c3460ca784cc2f52605527 Mon Sep 17 00:00:00 2001 From: Artak Mkrtchyan Date: Thu, 1 Feb 2018 16:15:46 -0800 Subject: [PATCH 3/3] Addressed review comments --- .../Formatters/TextOutputFormatter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/TextOutputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/TextOutputFormatter.cs index fb2a07a1fb..c30901da00 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Formatters/TextOutputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Formatters/TextOutputFormatter.cs @@ -173,7 +173,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters return result; } - return null; + return Array.Empty(); } private string GetMediaTypeWithCharset(string mediaType, Encoding encoding)