Merge pull request #7322 from aspnet/artakm/fix_5013
TextOutputFormatter reads AcceptCharset header not using TypedHeaders
This commit is contained in:
commit
c365297520
|
|
@ -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
|
|||
/// <returns>A task which can write the response body.</returns>
|
||||
public abstract Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding);
|
||||
|
||||
internal static IList<StringWithQualityHeaderValue> GetAcceptCharsetHeaderValues(OutputFormatterWriteContext context)
|
||||
{
|
||||
var request = context.HttpContext.Request;
|
||||
if (StringWithQualityHeaderValue.TryParseList(request.Headers[HeaderNames.AcceptCharset], out IList<StringWithQualityHeaderValue> result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
return Array.Empty<StringWithQualityHeaderValue>();
|
||||
}
|
||||
|
||||
private string GetMediaTypeWithCharset(string mediaType, Encoding encoding)
|
||||
{
|
||||
if (string.Equals(encoding.WebName, Encoding.UTF8.WebName, StringComparison.OrdinalIgnoreCase) &&
|
||||
|
|
|
|||
|
|
@ -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,29 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
|||
Assert.Equal(StatusCodes.Status406NotAcceptable, context.HttpContext.Response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAcceptCharsetHeaderValues_ReadsHeaderAndParsesValues()
|
||||
{
|
||||
// Arrange
|
||||
const string expectedValue = "expected";
|
||||
|
||||
var formatter = new OverrideEncodingFormatter(encoding: null);
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Headers[HeaderNames.AcceptCharset] = expectedValue;
|
||||
|
||||
var writerContext = new OutputFormatterWriteContext(
|
||||
context,
|
||||
new TestHttpResponseStreamWriterFactory().CreateWriter,
|
||||
objectType: null,
|
||||
@object: null);
|
||||
|
||||
// Act
|
||||
var result = TextOutputFormatter.GetAcceptCharsetHeaderValues(writerContext);
|
||||
|
||||
//Assert
|
||||
Assert.Equal(expectedValue, Assert.Single(result).Value.Value);
|
||||
}
|
||||
|
||||
private class TestOutputFormatter : TextOutputFormatter
|
||||
{
|
||||
public TestOutputFormatter()
|
||||
|
|
|
|||
Loading…
Reference in New Issue