now returns response content headers

This commit is contained in:
tom.pallister 2016-11-02 20:22:07 +00:00
parent c31a1ba598
commit 190e394011
4 changed files with 24 additions and 8 deletions

View File

@ -3,6 +3,8 @@ using Ocelot.Responses;
namespace Ocelot.Headers namespace Ocelot.Headers
{ {
using System;
public class RemoveOutputHeaders : IRemoveOutputHeaders public class RemoveOutputHeaders : IRemoveOutputHeaders
{ {
/// <summary> /// <summary>
@ -10,14 +12,13 @@ namespace Ocelot.Headers
/// in a given context such as transfer encoding chunked when ASP.NET is not /// in a given context such as transfer encoding chunked when ASP.NET is not
/// returning the response in this manner /// returning the response in this manner
/// </summary> /// </summary>
private readonly string[] _unsupportedHeaders = private readonly string[] _unsupportedRequestHeaders =
{ {
"Transfer-Encoding" "Transfer-Encoding"
}; };
public Response Remove(HttpResponseHeaders headers) public Response Remove(HttpResponseHeaders headers)
{ {
foreach (var unsupported in _unsupportedHeaders) foreach (var unsupported in _unsupportedRequestHeaders)
{ {
headers.Remove(unsupported); headers.Remove(unsupported);
} }

View File

@ -9,6 +9,8 @@ using Ocelot.Responses;
namespace Ocelot.Responder namespace Ocelot.Responder
{ {
using System.Collections.Generic;
/// <summary> /// <summary>
/// Cannot unit test things in this class due to methods not being implemented /// Cannot unit test things in this class due to methods not being implemented
/// on .net concretes used for testing /// on .net concretes used for testing
@ -28,12 +30,17 @@ namespace Ocelot.Responder
foreach (var httpResponseHeader in response.Headers) foreach (var httpResponseHeader in response.Headers)
{ {
context.Response.Headers.Add(httpResponseHeader.Key, new StringValues(httpResponseHeader.Value.ToArray())); AddHeaderIfDoesntExist(context, httpResponseHeader);
}
foreach (var httpResponseHeader in response.Content.Headers)
{
AddHeaderIfDoesntExist(context, httpResponseHeader);
} }
var content = await response.Content.ReadAsStreamAsync(); var content = await response.Content.ReadAsStreamAsync();
context.Response.Headers.Add("Content-Length", new[] { content.Length.ToString() }); AddHeaderIfDoesntExist(context, new KeyValuePair<string, IEnumerable<string>>("Content-Length", new []{ content.Length.ToString() }) );
context.Response.OnStarting(state => context.Response.OnStarting(state =>
{ {
@ -54,6 +61,14 @@ namespace Ocelot.Responder
return new OkResponse(); return new OkResponse();
} }
private static void AddHeaderIfDoesntExist(HttpContext context, KeyValuePair<string, IEnumerable<string>> httpResponseHeader)
{
if (!context.Response.Headers.ContainsKey(httpResponseHeader.Key))
{
context.Response.Headers.Add(httpResponseHeader.Key, new StringValues(httpResponseHeader.Value.ToArray()));
}
}
public async Task<Response> SetErrorResponseOnContext(HttpContext context, int statusCode) public async Task<Response> SetErrorResponseOnContext(HttpContext context, int statusCode)
{ {
context.Response.OnStarting(x => context.Response.OnStarting(x =>

View File

@ -7,13 +7,13 @@ using Xunit;
namespace Ocelot.UnitTests.Headers namespace Ocelot.UnitTests.Headers
{ {
public class RemoveHeaders public class RemoveHeadersTests
{ {
private HttpResponseHeaders _headers; private HttpResponseHeaders _headers;
private readonly Ocelot.Headers.RemoveOutputHeaders _removeOutputHeaders; private readonly Ocelot.Headers.RemoveOutputHeaders _removeOutputHeaders;
private Response _result; private Response _result;
public RemoveHeaders() public RemoveHeadersTests()
{ {
_removeOutputHeaders = new Ocelot.Headers.RemoveOutputHeaders(); _removeOutputHeaders = new Ocelot.Headers.RemoveOutputHeaders();
} }