[Fixes #1183] Response uses chunked encoding when returning a string

This commit is contained in:
Kiran Challa 2014-09-25 17:26:07 -07:00
parent 3cd5c17da7
commit 1c8582d77a
2 changed files with 16 additions and 7 deletions

View File

@ -51,7 +51,9 @@ namespace Microsoft.AspNet.Mvc
}
var response = context.ActionContext.HttpContext.Response;
using (var writer = new StreamWriter(response.Body, context.SelectedEncoding, 1024, leaveOpen: true))
using (var delegatingStream = new DelegatingStream(response.Body))
using (var writer = new StreamWriter(delegatingStream, context.SelectedEncoding, 1024, leaveOpen: true))
{
await writer.WriteAsync(valueAsString);
}

View File

@ -54,22 +54,29 @@ namespace Microsoft.AspNet.Mvc
public async Task WriteAsync_DoesNotWriteNullStrings()
{
// Arrange
Encoding encoding = Encoding.UTF8;
var memoryStream = new MemoryStream();
var response = new Mock<HttpResponse>();
response.SetupProperty<long?>(o => o.ContentLength);
response.SetupGet(r => r.Body).Returns(memoryStream);
var mockHttpContext = new Mock<HttpContext>();
mockHttpContext.Setup(o => o.Response).Returns(response.Object);
var formatter = new TextPlainFormatter();
var formatterContext = new OutputFormatterContext()
{
Object = null,
DeclaredType = typeof(string),
DeclaredType = typeof(string),
ActionContext = new ActionContext(mockHttpContext.Object, new RouteData(), new ActionDescriptor()),
SelectedEncoding = encoding
};
var tempMemoryStream = new MemoryStream();
var mockHttpContext = new Mock<HttpContext>();
mockHttpContext.SetupGet(o => o.Response.Body)
.Returns(tempMemoryStream);
// Act
await formatter.WriteResponseBodyAsync(formatterContext);
// Assert
Assert.Equal(0, tempMemoryStream.Length);
Assert.Equal(0, memoryStream.Length);
response.VerifySet(r => r.ContentLength = It.IsAny<long?>(), Times.Never());
}
}
}