* StringOutput set proper ContentType

This commit is contained in:
ryanbrandenburg 2016-01-12 16:33:54 -08:00
parent 2810858905
commit a229b20c48
3 changed files with 73 additions and 8 deletions

View File

@ -7,7 +7,6 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Internal; using Microsoft.AspNet.Mvc.Internal;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNet.Mvc.Formatters namespace Microsoft.AspNet.Mvc.Formatters
{ {
@ -34,7 +33,13 @@ namespace Microsoft.AspNet.Mvc.Formatters
// always return it as a text/plain format. // always return it as a text/plain format.
if (context.ObjectType == typeof(string) || context.Object is string) if (context.ObjectType == typeof(string) || context.Object is string)
{ {
context.ContentType = new StringSegment(SupportedMediaTypes[0]); if (!context.ContentType.HasValue)
{
var mediaType = SupportedMediaTypes[0];
var encoding = SupportedEncodings[0];
context.ContentType = new StringSegment(MediaType.ReplaceEncoding(mediaType, encoding));
}
return true; return true;
} }

View File

@ -27,6 +27,48 @@ namespace Microsoft.AspNet.Mvc.Formatters
} }
} }
[Fact]
public void CanWriteResult_SetsAcceptContentType()
{
// Arrange
var formatter = new StringOutputFormatter();
var expectedContentType = new StringSegment("application/json");
var context = new OutputFormatterWriteContext(
new DefaultHttpContext(),
new TestHttpResponseStreamWriterFactory().CreateWriter,
typeof(string),
"Thisisastring");
context.ContentType = expectedContentType;
// Act
var result = formatter.CanWriteResult(context);
// Assert
Assert.True(result);
Assert.Equal(expectedContentType, context.ContentType);
}
[Fact]
public void CanWriteResult_DefaultContentType()
{
// Arrange
var formatter = new StringOutputFormatter();
var context = new OutputFormatterWriteContext(
new DefaultHttpContext(),
new TestHttpResponseStreamWriterFactory().CreateWriter,
typeof(string),
"Thisisastring");
// Act
var result = formatter.CanWriteResult(context);
// Assert
Assert.True(result);
Assert.Equal(new StringSegment("text/plain; charset=utf-8"), context.ContentType);
}
[Theory] [Theory]
[MemberData(nameof(OutputFormatterContextValues))] [MemberData(nameof(OutputFormatterContextValues))]
public void CanWriteResult_ReturnsTrueForStringTypes( public void CanWriteResult_ReturnsTrueForStringTypes(
@ -35,9 +77,7 @@ namespace Microsoft.AspNet.Mvc.Formatters
bool expectedCanWriteResult) bool expectedCanWriteResult)
{ {
// Arrange // Arrange
var expectedContentType = expectedCanWriteResult ? var expectedContentType = new StringSegment("application/json");
new StringSegment("text/plain") :
new StringSegment("application/json");
var formatter = new StringOutputFormatter(); var formatter = new StringOutputFormatter();
var type = useDeclaredTypeAsString ? typeof(string) : typeof(object); var type = useDeclaredTypeAsString ? typeof(string) : typeof(object);
@ -47,7 +87,7 @@ namespace Microsoft.AspNet.Mvc.Formatters
new TestHttpResponseStreamWriterFactory().CreateWriter, new TestHttpResponseStreamWriterFactory().CreateWriter,
type, type,
value); value);
context.ContentType = new StringSegment("application/json"); context.ContentType = expectedContentType;
// Act // Act
var result = formatter.CanWriteResult(context); var result = formatter.CanWriteResult(context);

View File

@ -350,7 +350,27 @@ END:VCARD
[Theory] [Theory]
[InlineData(true)] [InlineData(true)]
[InlineData(false)] [InlineData(false)]
public async Task ObjectResult_WithStringReturnType_WritesTextPlainFormat(bool matchFormatterOnObjectType) public async Task ObjectResult_WithStringReturnType_DefaultToTextPlain(bool matchFormatterOnObjectType)
{
// Arrange
var targetUri = "http://localhost/FallbackOnTypeBasedMatch/ReturnString?matchFormatterOnObjectType=true" +
matchFormatterOnObjectType;
var request = new HttpRequestMessage(HttpMethod.Get, targetUri);
// Act
var response = await Client.SendAsync(request);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("text/plain", response.Content.Headers.ContentType.MediaType);
var actualBody = await response.Content.ReadAsStringAsync();
Assert.Equal("Hello World!", actualBody);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task ObjectResult_WithStringReturnType_SetsMediaTypeToAccept(bool matchFormatterOnObjectType)
{ {
// Arrange // Arrange
var targetUri = "http://localhost/FallbackOnTypeBasedMatch/ReturnString?matchFormatterOnObjectType=" + var targetUri = "http://localhost/FallbackOnTypeBasedMatch/ReturnString?matchFormatterOnObjectType=" +
@ -363,7 +383,7 @@ END:VCARD
// Assert // Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("text/plain", response.Content.Headers.ContentType.MediaType); Assert.Equal("application/json", response.Content.Headers.ContentType.MediaType);
var actualBody = await response.Content.ReadAsStringAsync(); var actualBody = await response.Content.ReadAsStringAsync();
Assert.Equal("Hello World!", actualBody); Assert.Equal("Hello World!", actualBody);
} }