Not instantiating the RequestHeaders and not relying on TypedHeaders to parse AcceptCharset
This commit is contained in:
parent
930664de6e
commit
dea3eb7856
|
|
@ -76,8 +76,8 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
throw new InvalidOperationException(message);
|
throw new InvalidOperationException(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
var request = context.HttpContext.Request;
|
var acceptCharsetHeaderValues = GetAcceptCharsetHeaderValues(context);
|
||||||
var encoding = MatchAcceptCharacterEncoding(request.GetTypedHeaders().AcceptCharset);
|
var encoding = MatchAcceptCharacterEncoding(acceptCharsetHeaderValues);
|
||||||
if (encoding != null)
|
if (encoding != null)
|
||||||
{
|
{
|
||||||
return encoding;
|
return encoding;
|
||||||
|
|
@ -165,6 +165,17 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
/// <returns>A task which can write the response body.</returns>
|
/// <returns>A task which can write the response body.</returns>
|
||||||
public abstract Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding);
|
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 null;
|
||||||
|
}
|
||||||
|
|
||||||
private string GetMediaTypeWithCharset(string mediaType, Encoding encoding)
|
private string GetMediaTypeWithCharset(string mediaType, Encoding encoding)
|
||||||
{
|
{
|
||||||
if (string.Equals(encoding.WebName, Encoding.UTF8.WebName, StringComparison.OrdinalIgnoreCase) &&
|
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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
@ -233,6 +234,30 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
||||||
Assert.Equal(StatusCodes.Status406NotAcceptable, context.HttpContext.Response.StatusCode);
|
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
|
private class TestOutputFormatter : TextOutputFormatter
|
||||||
{
|
{
|
||||||
public TestOutputFormatter()
|
public TestOutputFormatter()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue