* Moving Utf8EncodingWithoutBOM to a shared type
This commit is contained in:
parent
065ee36fdd
commit
19ab3a4fc6
|
|
@ -4,6 +4,7 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Microsoft.AspNet.Mvc.Internal;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
|
|
@ -11,12 +12,10 @@ namespace Microsoft.AspNet.Mvc
|
|||
public class JsonResult : ActionResult
|
||||
{
|
||||
private const int BufferSize = 1024;
|
||||
private static readonly Encoding UTF8EncodingWithoutBOM
|
||||
= new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
|
||||
private readonly object _returnValue;
|
||||
|
||||
private JsonSerializerSettings _jsonSerializerSettings;
|
||||
private Encoding _encoding = UTF8EncodingWithoutBOM;
|
||||
private Encoding _encoding = UTF8EncodingWithoutBOM.Encoding;
|
||||
|
||||
public JsonResult(object returnValue)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,15 +4,16 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc.Core;
|
||||
using Microsoft.AspNet.Mvc.Internal;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
public class ViewResult : ActionResult
|
||||
{
|
||||
private const int BufferSize = 1024;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IViewEngine _viewEngine;
|
||||
|
||||
|
|
@ -35,7 +36,8 @@ namespace Microsoft.AspNet.Mvc
|
|||
{
|
||||
context.HttpContext.Response.ContentType = "text/html; charset=utf-8";
|
||||
var wrappedStream = new StreamWrapper(context.HttpContext.Response.Body);
|
||||
using (var writer = new StreamWriter(wrappedStream, new UTF8Encoding(false), 1024, leaveOpen: true))
|
||||
var encoding = UTF8EncodingWithoutBOM.Encoding;
|
||||
using (var writer = new StreamWriter(wrappedStream, encoding, BufferSize, leaveOpen: true))
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -81,7 +83,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
_wrappedStream = stream;
|
||||
}
|
||||
|
||||
public bool BlockWrites { get; set;}
|
||||
public bool BlockWrites { get; set; }
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
using System.Text;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Internal
|
||||
{
|
||||
public static class UTF8EncodingWithoutBOM
|
||||
{
|
||||
public static readonly Encoding Encoding
|
||||
= new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
|
||||
}
|
||||
}
|
||||
|
|
@ -133,6 +133,7 @@
|
|||
<Compile Include="IControllerDescriptorFactory.cs" />
|
||||
<Compile Include="IControllerFactory.cs" />
|
||||
<Compile Include="Injector.cs" />
|
||||
<Compile Include="Internal\EncodingHelper.cs" />
|
||||
<Compile Include="Internal\PropertyHelper.cs" />
|
||||
<Compile Include="Internal\TypeHelper.cs" />
|
||||
<Compile Include="IParameterDescriptorFactory.cs" />
|
||||
|
|
@ -233,4 +234,4 @@
|
|||
<Compile Include="ViewDataDictionaryOfT.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
</Project>
|
||||
|
|
@ -53,6 +53,7 @@
|
|||
<Compile Include="TestController.cs" />
|
||||
<Compile Include="TypeHelperTest.cs" />
|
||||
<Compile Include="UrlHelperTest.cs" />
|
||||
<Compile Include="ViewResultTest.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Core.Test
|
||||
{
|
||||
public class ViewResultTest
|
||||
{
|
||||
[Fact]
|
||||
public async Task ExecuteResultAsync_WritesOutputWithoutBOM()
|
||||
{
|
||||
// Arrange
|
||||
var expected = new byte[] { 97, 98, 99, 100 };
|
||||
var memoryStream = new MemoryStream();
|
||||
var response = new Mock<HttpResponse>();
|
||||
response.SetupGet(r => r.Body)
|
||||
.Returns(memoryStream);
|
||||
var context = new Mock<HttpContext>();
|
||||
context.SetupGet(c => c.Response)
|
||||
.Returns(response.Object);
|
||||
var routeDictionary = new Dictionary<string, object>();
|
||||
var actionContext = new ActionContext(context.Object,
|
||||
Mock.Of<IRouter>(),
|
||||
routeDictionary,
|
||||
new ActionDescriptor());
|
||||
var view = new Mock<IView>();
|
||||
view.Setup(v => v.RenderAsync(It.IsAny<ViewContext>()))
|
||||
.Callback((ViewContext v) =>
|
||||
{
|
||||
v.Writer.Write("abcd");
|
||||
})
|
||||
.Returns(Task.FromResult(0));
|
||||
var serviceProvider = Mock.Of<IServiceProvider>();
|
||||
var viewEngine = new Mock<IViewEngine>();
|
||||
viewEngine.Setup(v => v.FindView(routeDictionary, It.IsAny<string>()))
|
||||
.Returns(ViewEngineResult.Found("MyView", view.Object));
|
||||
var viewResult = new ViewResult(serviceProvider, viewEngine.Object);
|
||||
|
||||
// Act
|
||||
await viewResult.ExecuteResultAsync(actionContext);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expected, memoryStream.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue