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

Commit 31f1cbb

Browse files
author
Yishai Galatzer
committed
CR feedback
1 parent 78093c2 commit 31f1cbb

File tree

5 files changed

+20
-22
lines changed

5 files changed

+20
-22
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(
4444
public bool CanWriteResult([NotNull] OutputFormatterContext context, MediaTypeHeaderValue contentType)
4545
{
4646
// Ignore the passed in content type, if the object is a Stream.
47-
// always return it as a text/plain format.
4847
if (context.Object is Stream)
4948
{
5049
context.SelectedContentType = contentType;
@@ -57,16 +56,17 @@ public bool CanWriteResult([NotNull] OutputFormatterContext context, MediaTypeHe
5756
/// <inheritdoc />
5857
public async Task WriteAsync([NotNull] OutputFormatterContext context)
5958
{
60-
var valueAsStream = ((Stream)context.Object);
59+
using (var valueAsStream = ((Stream)context.Object))
60+
{
61+
var response = context.ActionContext.HttpContext.Response;
6162

62-
var response = context.ActionContext.HttpContext.Response;
63+
if (context.SelectedContentType != null)
64+
{
65+
response.ContentType = context.SelectedContentType.ToString();
66+
}
6367

64-
if (context.SelectedContentType != null)
65-
{
66-
response.ContentType = context.SelectedContentType.ToString();
68+
await valueAsStream.CopyToAsync(response.Body);
6769
}
68-
69-
await valueAsStream.CopyToAsync(response.Body);
7070
}
7171
}
7272
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
using System;
55
using System.IO;
6-
using Microsoft.AspNet.Mvc;
76
using Microsoft.Net.Http.Headers;
87
using Xunit;
98

109
namespace Microsoft.AspNet.Mvc
1110
{
1211
public class StreamOutputFormatterTest
1312
{
13+
[Theory]
1414
[InlineData(typeof(Stream), typeof(FileStream), "text/plain", "text/plain")]
1515
[InlineData(typeof(object), typeof(FileStream), "text/plain", "text/plain")]
1616
[InlineData(typeof(object), typeof(MemoryStream), "text/plain", "text/plain")]
@@ -19,9 +19,10 @@ public class StreamOutputFormatterTest
1919
[InlineData(typeof(object), null, "text/plain", null)]
2020
[InlineData(typeof(IActionResult), null, "text/plain", null)]
2121
[InlineData(typeof(IActionResult), typeof(IActionResult), "text/plain", null)]
22-
[Theory]
23-
public void GetSupportedContentTypes_ReturnsAppropriateValues(Type declaredType, Type runtimeType,
24-
string contentType, string expected)
22+
public void GetSupportedContentTypes_ReturnsAppropriateValues(Type declaredType,
23+
Type runtimeType,
24+
string contentType,
25+
string expected)
2526
{
2627
// Arrange
2728
var formatter = new StreamOutputFormatter();
@@ -42,10 +43,10 @@ public void GetSupportedContentTypes_ReturnsAppropriateValues(Type declaredType,
4243
}
4344
}
4445

46+
[Theory]
4547
[InlineData(typeof(object))]
4648
[InlineData(typeof(SimplePOCO))]
4749
[InlineData(null)]
48-
[Theory]
4950
public void CanWriteResult_OnlyActsOnStreams(Type type)
5051
{
5152
// Arrange

test/Microsoft.AspNet.Mvc.FunctionalTests/StreamOutputFormatterTest.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Net;
6-
using System.Net.Http.Headers;
75
using System.Threading.Tasks;
86
using FormatterWebSite;
97
using Microsoft.AspNet.Builder;
@@ -17,12 +15,12 @@ public class StreamOutputFormatterTest
1715
private readonly IServiceProvider _provider = TestHelper.CreateServices(nameof(FormatterWebSite));
1816
private readonly Action<IApplicationBuilder> _app = new Startup().Configure;
1917

18+
[Theory]
2019
[InlineData("SimpleMemoryStream", null)]
2120
[InlineData("MemoryStreamWithContentType", "text/html")]
2221
[InlineData("MemoryStreamWithContentTypeFromProduces", "text/plain")]
2322
[InlineData("MemoryStreamWithContentTypeFromProducesWithMultipleValues", "text/html")]
2423
[InlineData("MemoryStreamOverridesContentTypeWithProduces", "text/plain")]
25-
[Theory]
2624
public async Task StreamOutputFormatter_ReturnsAppropriateContentAndContentType(string actionName, string contentType)
2725
{
2826
// Arrange

test/Microsoft.AspNet.Mvc.Test/MvcOptionSetupTest.cs renamed to test/Microsoft.AspNet.Mvc.Test/MvcOptionsSetupTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Microsoft.AspNet.Mvc
1212
{
13-
public class MvcOptionSetupTest
13+
public class MvcOptionsSetupTest
1414
{
1515
[Fact]
1616
public void Setup_SetsUpViewEngines()
@@ -86,8 +86,8 @@ public void Setup_SetsUpOutputFormatters()
8686
Assert.Equal(4, mvcOptions.OutputFormatters.Count);
8787
Assert.IsType<HttpNoContentOutputFormatter>(mvcOptions.OutputFormatters[0].Instance);
8888
Assert.IsType<StringOutputFormatter>(mvcOptions.OutputFormatters[1].Instance);
89-
Assert.IsType<StreamOutputFormatter>(mvcOptions.OutputFormatters[1].Instance);
90-
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[2].Instance);
89+
Assert.IsType<StreamOutputFormatter>(mvcOptions.OutputFormatters[2].Instance);
90+
Assert.IsType<JsonOutputFormatter>(mvcOptions.OutputFormatters[3].Instance);
9191
}
9292

9393
[Fact]

test/WebSites/FormatterWebSite/Controllers/StreamController.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.IO;
55
using Microsoft.AspNet.Mvc;
6-
using Microsoft.Net.Http.Headers;
76

87
namespace FormatterWebSite
98
{
@@ -40,7 +39,7 @@ public Stream MemoryStreamWithContentTypeFromProducesWithMultipleValues()
4039
[Produces("text/plain")]
4140
public Stream MemoryStreamOverridesContentTypeWithProduces()
4241
{
43-
// Produces will set a ContentType on the implicit ObjecResult and
42+
// Produces will set a ContentType on the implicit ObjectResult and
4443
// ContentType on response are overriden by content types from ObjectResult.
4544
Response.ContentType = "text/html";
4645

@@ -55,7 +54,7 @@ private static Stream CreateDefaultStream()
5554
writer.Flush();
5655
stream.Seek(0, SeekOrigin.Begin);
5756

58-
return stream;
57+
return stream;
5958
}
6059
}
6160
}

0 commit comments

Comments
 (0)