mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-23 00:32:50 +08:00
now returns response content headers
This commit is contained in:
parent
c31a1ba598
commit
190e394011
@ -3,6 +3,8 @@ using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Headers
|
||||
{
|
||||
using System;
|
||||
|
||||
public class RemoveOutputHeaders : IRemoveOutputHeaders
|
||||
{
|
||||
/// <summary>
|
||||
@ -10,14 +12,13 @@ namespace Ocelot.Headers
|
||||
/// in a given context such as transfer encoding chunked when ASP.NET is not
|
||||
/// returning the response in this manner
|
||||
/// </summary>
|
||||
private readonly string[] _unsupportedHeaders =
|
||||
private readonly string[] _unsupportedRequestHeaders =
|
||||
{
|
||||
"Transfer-Encoding"
|
||||
};
|
||||
|
||||
public Response Remove(HttpResponseHeaders headers)
|
||||
{
|
||||
foreach (var unsupported in _unsupportedHeaders)
|
||||
foreach (var unsupported in _unsupportedRequestHeaders)
|
||||
{
|
||||
headers.Remove(unsupported);
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Responder
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// Cannot unit test things in this class due to methods not being implemented
|
||||
/// on .net concretes used for testing
|
||||
@ -28,12 +30,17 @@ namespace Ocelot.Responder
|
||||
|
||||
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();
|
||||
|
||||
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 =>
|
||||
{
|
||||
@ -54,6 +61,14 @@ namespace Ocelot.Responder
|
||||
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)
|
||||
{
|
||||
context.Response.OnStarting(x =>
|
||||
|
@ -7,13 +7,13 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class RemoveHeaders
|
||||
public class RemoveHeadersTests
|
||||
{
|
||||
private HttpResponseHeaders _headers;
|
||||
private readonly Ocelot.Headers.RemoveOutputHeaders _removeOutputHeaders;
|
||||
private Response _result;
|
||||
|
||||
public RemoveHeaders()
|
||||
public RemoveHeadersTests()
|
||||
{
|
||||
_removeOutputHeaders = new Ocelot.Headers.RemoveOutputHeaders();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user