Ignore response content if null (fix #785) (#786)

This commit is contained in:
Chris Swinchatt 2019-02-20 14:16:55 +00:00 committed by Marcelo Castagna
parent a9cfc04aa9
commit 26ae9948d5
2 changed files with 26 additions and 4 deletions

View File

@ -32,6 +32,15 @@ namespace Ocelot.Responder
AddHeaderIfDoesntExist(context, httpResponseHeader); AddHeaderIfDoesntExist(context, httpResponseHeader);
} }
SetStatusCode(context, (int)response.StatusCode);
context.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = response.ReasonPhrase;
if (response.Content is null)
{
return;
}
foreach (var httpResponseHeader in response.Content.Headers) foreach (var httpResponseHeader in response.Content.Headers)
{ {
AddHeaderIfDoesntExist(context, new Header(httpResponseHeader.Key, httpResponseHeader.Value)); AddHeaderIfDoesntExist(context, new Header(httpResponseHeader.Key, httpResponseHeader.Value));
@ -44,10 +53,6 @@ namespace Ocelot.Responder
AddHeaderIfDoesntExist(context, new Header("Content-Length", new []{ response.Content.Headers.ContentLength.ToString() }) ); AddHeaderIfDoesntExist(context, new Header("Content-Length", new []{ response.Content.Headers.ContentLength.ToString() }) );
} }
SetStatusCode(context, (int)response.StatusCode);
context.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = response.ReasonPhrase;
using(content) using(content)
{ {
if (response.StatusCode != HttpStatusCode.NotModified && context.Response.ContentLength != 0) if (response.StatusCode != HttpStatusCode.NotModified && context.Response.ContentLength != 0)

View File

@ -39,6 +39,23 @@ namespace Ocelot.UnitTests.Responder
header.ShouldBeEmpty(); header.ShouldBeEmpty();
} }
[Fact]
public void should_ignore_content_if_null()
{
var httpContext = new DefaultHttpContext();
var response = new DownstreamResponse(null, HttpStatusCode.OK,
new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason");
Should.NotThrow(() =>
{
_responder
.SetResponseOnHttpContext(httpContext, response)
.GetAwaiter()
.GetResult()
;
});
}
[Fact] [Fact]
public void should_have_content_length() public void should_have_content_length()
{ {