[Fixes #1331] Dispose HttpResponseMessage once its written out in HttpResponseMessageOutputFormatter
This commit is contained in:
parent
4ec6da1ed3
commit
dd587f743b
|
|
@ -40,29 +40,32 @@ namespace Microsoft.AspNet.Mvc.WebApiCompatShim
|
||||||
throw new InvalidOperationException(message);
|
throw new InvalidOperationException(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
response.StatusCode = (int)responseMessage.StatusCode;
|
using (responseMessage)
|
||||||
|
|
||||||
var responseFeature = context.ActionContext.HttpContext.GetFeature<IHttpResponseFeature>();
|
|
||||||
if (responseFeature != null)
|
|
||||||
{
|
{
|
||||||
responseFeature.ReasonPhrase = responseMessage.ReasonPhrase;
|
response.StatusCode = (int)responseMessage.StatusCode;
|
||||||
}
|
|
||||||
|
|
||||||
var responseHeaders = responseMessage.Headers;
|
var responseFeature = context.ActionContext.HttpContext.GetFeature<IHttpResponseFeature>();
|
||||||
foreach (var header in responseHeaders)
|
if (responseFeature != null)
|
||||||
{
|
{
|
||||||
response.Headers.AppendValues(header.Key, header.Value.ToArray());
|
responseFeature.ReasonPhrase = responseMessage.ReasonPhrase;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseMessage.Content != null)
|
var responseHeaders = responseMessage.Headers;
|
||||||
{
|
foreach (var header in responseHeaders)
|
||||||
var contentHeaders = responseMessage.Content.Headers;
|
|
||||||
foreach (var header in contentHeaders)
|
|
||||||
{
|
{
|
||||||
response.Headers.AppendValues(header.Key, header.Value.ToArray());
|
response.Headers.AppendValues(header.Key, header.Value.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
await responseMessage.Content.CopyToAsync(response.Body);
|
if (responseMessage.Content != null)
|
||||||
|
{
|
||||||
|
var contentHeaders = responseMessage.Content.Headers;
|
||||||
|
foreach (var header in contentHeaders)
|
||||||
|
{
|
||||||
|
response.Headers.AppendValues(header.Key, header.Value.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
await responseMessage.Content.CopyToAsync(response.Body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
// 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.IO;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNet.Mvc.WebApiCompatShim;
|
||||||
|
using Microsoft.AspNet.PipelineCore;
|
||||||
|
using Moq;
|
||||||
|
using Moq.Protected;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc.WebApiCompatShimTest
|
||||||
|
{
|
||||||
|
public class HttpResponseMessageOutputFormatterTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Disposed_CalledOn_HttpResponseMessage()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var formatter = new HttpResponseMessageOutputFormatter();
|
||||||
|
var streamContent = new Mock<StreamContent>(new MemoryStream());
|
||||||
|
streamContent.Protected().Setup("Dispose", true).Verifiable();
|
||||||
|
var httpResponseMessage = new HttpResponseMessage();
|
||||||
|
httpResponseMessage.Content = streamContent.Object;
|
||||||
|
var outputFormatterContext = GetOutputFormatterContext(httpResponseMessage, typeof(HttpResponseMessage));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await formatter.WriteAsync(outputFormatterContext);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
streamContent.Protected().Verify("Dispose", Times.Once(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private OutputFormatterContext GetOutputFormatterContext(object outputValue, Type outputType)
|
||||||
|
{
|
||||||
|
return new OutputFormatterContext
|
||||||
|
{
|
||||||
|
Object = outputValue,
|
||||||
|
DeclaredType = outputType,
|
||||||
|
ActionContext = new ActionContext(new DefaultHttpContext(), routeData: null, actionDescriptor: null)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue