Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
* StringOutput set proper ContentType
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanbrandenburg committed Jan 19, 2016
1 parent 2810858 commit a229b20
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNet.Mvc.Formatters
{
Expand All @@ -34,7 +33,13 @@ public override bool CanWriteResult(OutputFormatterCanWriteContext context)
// always return it as a text/plain format.
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,48 @@ public static IEnumerable<object[]> OutputFormatterContextValues
}
}

[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]
[MemberData(nameof(OutputFormatterContextValues))]
public void CanWriteResult_ReturnsTrueForStringTypes(
Expand All @@ -35,9 +77,7 @@ public void CanWriteResult_ReturnsTrueForStringTypes(
bool expectedCanWriteResult)
{
// Arrange
var expectedContentType = expectedCanWriteResult ?
new StringSegment("text/plain") :
new StringSegment("application/json");
var expectedContentType = new StringSegment("application/json");

var formatter = new StringOutputFormatter();
var type = useDeclaredTypeAsString ? typeof(string) : typeof(object);
Expand All @@ -47,7 +87,7 @@ public void CanWriteResult_ReturnsTrueForStringTypes(
new TestHttpResponseStreamWriterFactory().CreateWriter,
type,
value);
context.ContentType = new StringSegment("application/json");
context.ContentType = expectedContentType;

// Act
var result = formatter.CanWriteResult(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,27 @@ public async Task NoMatchOn_RequestContentType_FallsBackOnTypeBasedMatch_MatchFo
[Theory]
[InlineData(true)]
[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
var targetUri = "http://localhost/FallbackOnTypeBasedMatch/ReturnString?matchFormatterOnObjectType=" +
Expand All @@ -363,7 +383,7 @@ public async Task ObjectResult_WithStringReturnType_WritesTextPlainFormat(bool m

// Assert
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();
Assert.Equal("Hello World!", actualBody);
}
Expand Down

0 comments on commit a229b20

Please sign in to comment.