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

Commit 1c8582d

Browse files
committed
[Fixes #1183] Response uses chunked encoding when returning a string
1 parent 3cd5c17 commit 1c8582d

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/Microsoft.AspNet.Mvc.Core/Formatters/TextPlainFormatter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public override async Task WriteResponseBodyAsync(OutputFormatterContext context
5151
}
5252

5353
var response = context.ActionContext.HttpContext.Response;
54-
using (var writer = new StreamWriter(response.Body, context.SelectedEncoding, 1024, leaveOpen: true))
54+
55+
using (var delegatingStream = new DelegatingStream(response.Body))
56+
using (var writer = new StreamWriter(delegatingStream, context.SelectedEncoding, 1024, leaveOpen: true))
5557
{
5658
await writer.WriteAsync(valueAsString);
5759
}

test/Microsoft.AspNet.Mvc.Core.Test/Formatters/TextPlainFormatterTests.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,29 @@ public void CanWriteResult_ReturnsTrueForStringTypes(object value, bool useDecla
5454
public async Task WriteAsync_DoesNotWriteNullStrings()
5555
{
5656
// Arrange
57+
Encoding encoding = Encoding.UTF8;
58+
var memoryStream = new MemoryStream();
59+
var response = new Mock<HttpResponse>();
60+
response.SetupProperty<long?>(o => o.ContentLength);
61+
response.SetupGet(r => r.Body).Returns(memoryStream);
62+
var mockHttpContext = new Mock<HttpContext>();
63+
mockHttpContext.Setup(o => o.Response).Returns(response.Object);
64+
5765
var formatter = new TextPlainFormatter();
5866
var formatterContext = new OutputFormatterContext()
5967
{
6068
Object = null,
61-
DeclaredType = typeof(string),
69+
DeclaredType = typeof(string),
70+
ActionContext = new ActionContext(mockHttpContext.Object, new RouteData(), new ActionDescriptor()),
71+
SelectedEncoding = encoding
6272
};
6373

64-
var tempMemoryStream = new MemoryStream();
65-
var mockHttpContext = new Mock<HttpContext>();
66-
mockHttpContext.SetupGet(o => o.Response.Body)
67-
.Returns(tempMemoryStream);
6874
// Act
6975
await formatter.WriteResponseBodyAsync(formatterContext);
7076

7177
// Assert
72-
Assert.Equal(0, tempMemoryStream.Length);
78+
Assert.Equal(0, memoryStream.Length);
79+
response.VerifySet(r => r.ContentLength = It.IsAny<long?>(), Times.Never());
7380
}
7481
}
7582
}

0 commit comments

Comments
 (0)