parent
90f2ece84d
commit
9ac37fbc7a
|
|
@ -1,8 +1,8 @@
|
||||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.Net.Http.Headers;
|
using Microsoft.Net.Http.Headers;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
|
|
@ -10,9 +10,9 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Always writes a string value to the response, regardless of requested content type.
|
/// Always writes a string value to the response, regardless of requested content type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class TextPlainFormatter : OutputFormatter
|
public class StringOutputFormatter : OutputFormatter
|
||||||
{
|
{
|
||||||
public TextPlainFormatter()
|
public StringOutputFormatter()
|
||||||
{
|
{
|
||||||
SupportedEncodings.Add(Encodings.UTF8EncodingWithoutBOM);
|
SupportedEncodings.Add(Encodings.UTF8EncodingWithoutBOM);
|
||||||
SupportedEncodings.Add(Encodings.UTF16EncodingLittleEndian);
|
SupportedEncodings.Add(Encodings.UTF16EncodingLittleEndian);
|
||||||
|
|
@ -39,19 +39,14 @@ namespace Microsoft.AspNet.Mvc
|
||||||
public override async Task WriteResponseBodyAsync(OutputFormatterContext context)
|
public override async Task WriteResponseBodyAsync(OutputFormatterContext context)
|
||||||
{
|
{
|
||||||
var valueAsString = (string)context.Object;
|
var valueAsString = (string)context.Object;
|
||||||
if (valueAsString == null)
|
if (string.IsNullOrEmpty(valueAsString))
|
||||||
{
|
{
|
||||||
// if the value is null don't write anything.
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = context.ActionContext.HttpContext.Response;
|
var response = context.ActionContext.HttpContext.Response;
|
||||||
|
|
||||||
using (var delegatingStream = new DelegatingStream(response.Body))
|
await response.WriteAsync(valueAsString, context.SelectedEncoding);
|
||||||
using (var writer = new StreamWriter(delegatingStream, context.SelectedEncoding, 1024, leaveOpen: true))
|
|
||||||
{
|
|
||||||
await writer.WriteAsync(valueAsString);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -42,7 +42,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
|
|
||||||
// Set up default output formatters.
|
// Set up default output formatters.
|
||||||
options.OutputFormatters.Add(new HttpNoContentOutputFormatter());
|
options.OutputFormatters.Add(new HttpNoContentOutputFormatter());
|
||||||
options.OutputFormatters.Add(new TextPlainFormatter());
|
options.OutputFormatters.Add(new StringOutputFormatter());
|
||||||
options.OutputFormatters.Add(new JsonOutputFormatter());
|
options.OutputFormatters.Add(new JsonOutputFormatter());
|
||||||
|
|
||||||
// Set up default input formatters.
|
// Set up default input formatters.
|
||||||
|
|
|
||||||
|
|
@ -119,7 +119,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
return new List<IOutputFormatter>()
|
return new List<IOutputFormatter>()
|
||||||
{
|
{
|
||||||
new TextPlainFormatter(),
|
new StringOutputFormatter(),
|
||||||
new JsonOutputFormatter()
|
new JsonOutputFormatter()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
return new List<IOutputFormatter>()
|
return new List<IOutputFormatter>()
|
||||||
{
|
{
|
||||||
new TextPlainFormatter(),
|
new StringOutputFormatter(),
|
||||||
new JsonOutputFormatter()
|
new JsonOutputFormatter()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
return new List<IOutputFormatter>()
|
return new List<IOutputFormatter>()
|
||||||
{
|
{
|
||||||
new TextPlainFormatter(),
|
new StringOutputFormatter(),
|
||||||
new JsonOutputFormatter()
|
new JsonOutputFormatter()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -490,7 +490,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
var objectResult = new ObjectResult(new Person() { Name = "John" });
|
var objectResult = new ObjectResult(new Person() { Name = "John" });
|
||||||
var outputFormatters = new IOutputFormatter[] {
|
var outputFormatters = new IOutputFormatter[] {
|
||||||
new HttpNoContentOutputFormatter(),
|
new HttpNoContentOutputFormatter(),
|
||||||
new TextPlainFormatter(),
|
new StringOutputFormatter(),
|
||||||
new JsonOutputFormatter(),
|
new JsonOutputFormatter(),
|
||||||
new XmlDataContractSerializerOutputFormatter(XmlSerializerOutputFormatter.GetDefaultXmlWriterSettings())
|
new XmlDataContractSerializerOutputFormatter(XmlSerializerOutputFormatter.GetDefaultXmlWriterSettings())
|
||||||
};
|
};
|
||||||
|
|
@ -531,7 +531,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
var objectResult = new ObjectResult(new Person() { Name = "John" });
|
var objectResult = new ObjectResult(new Person() { Name = "John" });
|
||||||
var outputFormatters = new IOutputFormatter[] {
|
var outputFormatters = new IOutputFormatter[] {
|
||||||
new HttpNoContentOutputFormatter(),
|
new HttpNoContentOutputFormatter(),
|
||||||
new TextPlainFormatter(),
|
new StringOutputFormatter(),
|
||||||
new JsonOutputFormatter(),
|
new JsonOutputFormatter(),
|
||||||
new XmlDataContractSerializerOutputFormatter(XmlSerializerOutputFormatter.GetDefaultXmlWriterSettings())
|
new XmlDataContractSerializerOutputFormatter(XmlSerializerOutputFormatter.GetDefaultXmlWriterSettings())
|
||||||
};
|
};
|
||||||
|
|
@ -566,7 +566,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
objectResult.ContentTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
|
objectResult.ContentTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
|
||||||
var outputFormatters = new IOutputFormatter[] {
|
var outputFormatters = new IOutputFormatter[] {
|
||||||
new HttpNoContentOutputFormatter(),
|
new HttpNoContentOutputFormatter(),
|
||||||
new TextPlainFormatter(),
|
new StringOutputFormatter(),
|
||||||
new JsonOutputFormatter(),
|
new JsonOutputFormatter(),
|
||||||
new XmlDataContractSerializerOutputFormatter(XmlSerializerOutputFormatter.GetDefaultXmlWriterSettings())
|
new XmlDataContractSerializerOutputFormatter(XmlSerializerOutputFormatter.GetDefaultXmlWriterSettings())
|
||||||
};
|
};
|
||||||
|
|
@ -592,7 +592,7 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
|
||||||
string requestAcceptCharsetHeader = "",
|
string requestAcceptCharsetHeader = "",
|
||||||
bool respectBrowserAcceptHeader = false)
|
bool respectBrowserAcceptHeader = false)
|
||||||
{
|
{
|
||||||
var formatters = new IOutputFormatter[] { new TextPlainFormatter(), new JsonOutputFormatter() };
|
var formatters = new IOutputFormatter[] { new StringOutputFormatter(), new JsonOutputFormatter() };
|
||||||
|
|
||||||
return CreateMockActionContext(
|
return CreateMockActionContext(
|
||||||
formatters,
|
formatters,
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
public void CanWriteResult_ReturnsTrueForStringTypes(object value, bool useDeclaredTypeAsString, bool expectedCanWriteResult)
|
public void CanWriteResult_ReturnsTrueForStringTypes(object value, bool useDeclaredTypeAsString, bool expectedCanWriteResult)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var formatter = new TextPlainFormatter();
|
var formatter = new StringOutputFormatter();
|
||||||
var typeToUse = useDeclaredTypeAsString ? typeof(string) : typeof(object);
|
var typeToUse = useDeclaredTypeAsString ? typeof(string) : typeof(object);
|
||||||
var formatterContext = new OutputFormatterContext()
|
var formatterContext = new OutputFormatterContext()
|
||||||
{
|
{
|
||||||
|
|
@ -59,7 +59,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
var mockHttpContext = new Mock<HttpContext>();
|
var mockHttpContext = new Mock<HttpContext>();
|
||||||
mockHttpContext.Setup(o => o.Response).Returns(response.Object);
|
mockHttpContext.Setup(o => o.Response).Returns(response.Object);
|
||||||
|
|
||||||
var formatter = new TextPlainFormatter();
|
var formatter = new StringOutputFormatter();
|
||||||
var formatterContext = new OutputFormatterContext()
|
var formatterContext = new OutputFormatterContext()
|
||||||
{
|
{
|
||||||
Object = null,
|
Object = null,
|
||||||
|
|
@ -22,7 +22,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
[InlineData("ReturnTaskOfObject_StringValue")]
|
[InlineData("ReturnTaskOfObject_StringValue")]
|
||||||
[InlineData("ReturnString")]
|
[InlineData("ReturnString")]
|
||||||
[InlineData("ReturnObject_StringValue")]
|
[InlineData("ReturnObject_StringValue")]
|
||||||
public async Task TextPlainFormatter_ForStringValues_GetsSelectedReturnsTextPlainContentType(string actionName)
|
public async Task StringOutputFormatter_ForStringValues_GetsSelectedReturnsTextPlainContentType(string actionName)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var server = TestServer.Create(_provider, _app);
|
var server = TestServer.Create(_provider, _app);
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,9 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
{
|
{
|
||||||
public class RazorPageTest
|
public class RazorPageTest
|
||||||
{
|
{
|
||||||
|
#pragma warning disable 1998
|
||||||
private readonly RenderAsyncDelegate _nullRenderAsyncDelegate = async writer => { };
|
private readonly RenderAsyncDelegate _nullRenderAsyncDelegate = async writer => { };
|
||||||
|
#pragma warning restore 1998
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task WritingScopesRedirectContentWrittenToViewContextWriter()
|
public async Task WritingScopesRedirectContentWrittenToViewContextWriter()
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,10 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
public class RazorViewTest
|
public class RazorViewTest
|
||||||
{
|
{
|
||||||
private const string LayoutPath = "~/Shared/_Layout.cshtml";
|
private const string LayoutPath = "~/Shared/_Layout.cshtml";
|
||||||
|
|
||||||
|
#pragma warning disable 1998
|
||||||
private readonly RenderAsyncDelegate _nullRenderAsyncDelegate = async writer => { };
|
private readonly RenderAsyncDelegate _nullRenderAsyncDelegate = async writer => { };
|
||||||
|
#pragma warning restore 1998
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task RenderAsync_AsPartial_DoesNotBufferOutput()
|
public async Task RenderAsync_AsPartial_DoesNotBufferOutput()
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(3, mvcOptions.OutputFormatters.Count);
|
Assert.Equal(3, mvcOptions.OutputFormatters.Count);
|
||||||
Assert.IsType<HttpNoContentOutputFormatter>(mvcOptions.OutputFormatters[0].Instance);
|
Assert.IsType<HttpNoContentOutputFormatter>(mvcOptions.OutputFormatters[0].Instance);
|
||||||
Assert.IsType<TextPlainFormatter>(mvcOptions.OutputFormatters[1].Instance);
|
Assert.IsType<StringOutputFormatter>(mvcOptions.OutputFormatters[1].Instance);
|
||||||
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[2].Instance);
|
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[2].Instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue