mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 17:28:16 +08:00
* #298 initial hacking around better aggregation * #298 bit more hacking around * #298 abstraction over httpresponsemessage * #298 tidying up * #298 docs * #298 missed this
This commit is contained in:
@ -1,29 +1,29 @@
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Ocelot.Headers;
|
||||
using System.Net.Http;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using System.Linq;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Moq;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Infrastructure;
|
||||
using Ocelot.UnitTests.Responder;
|
||||
using System;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Middleware.Multiplexer;
|
||||
using Shouldly;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class AddHeadersToResponseTests
|
||||
{
|
||||
private IAddHeadersToResponse _adder;
|
||||
private Mock<IPlaceholders> _placeholders;
|
||||
private HttpResponseMessage _response;
|
||||
private readonly IAddHeadersToResponse _adder;
|
||||
private readonly Mock<IPlaceholders> _placeholders;
|
||||
private DownstreamResponse _response;
|
||||
private List<AddHeader> _addHeaders;
|
||||
private Mock<IOcelotLoggerFactory> _factory;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
private readonly Mock<IOcelotLogger> _logger;
|
||||
|
||||
public AddHeadersToResponseTests()
|
||||
{
|
||||
@ -111,7 +111,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
|
||||
private void ThenTheHeaderIsNotAdded(string key)
|
||||
{
|
||||
_response.Headers.TryGetValues(key, out var values).ShouldBeFalse();
|
||||
_response.Headers.Any(x => x.Key == key).ShouldBeFalse();
|
||||
}
|
||||
|
||||
private void GivenTheTraceIdIs(string traceId)
|
||||
@ -126,8 +126,8 @@ namespace Ocelot.UnitTests.Headers
|
||||
|
||||
private void ThenTheHeaderIsReturned(string key, string value)
|
||||
{
|
||||
var values = _response.Headers.GetValues(key);
|
||||
values.First().ShouldBe(value);
|
||||
var values = _response.Headers.First(x => x.Key == key);
|
||||
values.Values.First().ShouldBe(value);
|
||||
}
|
||||
|
||||
private void WhenIAdd()
|
||||
@ -137,7 +137,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
|
||||
private void GivenAResponseMessage()
|
||||
{
|
||||
_response = new HttpResponseMessage();
|
||||
_response = new DownstreamResponse(new HttpResponseMessage());
|
||||
}
|
||||
|
||||
private void GivenTheAddHeaders(List<AddHeader> addHeaders)
|
||||
|
@ -1,33 +1,33 @@
|
||||
using Xunit;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Headers.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Headers;
|
||||
using System.Net.Http;
|
||||
using Ocelot.Authorisation.Middleware;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
using Xunit;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Headers.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Headers;
|
||||
using System.Net.Http;
|
||||
using Ocelot.Authorisation.Middleware;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Middleware.Multiplexer;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Request.Middleware;
|
||||
|
||||
public class HttpHeadersTransformationMiddlewareTests
|
||||
{
|
||||
private Mock<IHttpContextRequestHeaderReplacer> _preReplacer;
|
||||
private Mock<IHttpResponseHeaderReplacer> _postReplacer;
|
||||
private readonly Mock<IHttpContextRequestHeaderReplacer> _preReplacer;
|
||||
private readonly Mock<IHttpResponseHeaderReplacer> _postReplacer;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
private HttpHeadersTransformationMiddleware _middleware;
|
||||
private DownstreamContext _downstreamContext;
|
||||
private readonly HttpHeadersTransformationMiddleware _middleware;
|
||||
private readonly DownstreamContext _downstreamContext;
|
||||
private OcelotRequestDelegate _next;
|
||||
private Mock<IAddHeadersToResponse> _addHeaders;
|
||||
private readonly Mock<IAddHeadersToResponse> _addHeaders;
|
||||
|
||||
public HttpHeadersTransformationMiddlewareTests()
|
||||
{
|
||||
@ -74,7 +74,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
|
||||
private void GivenTheHttpResponseMessageIs()
|
||||
{
|
||||
_downstreamContext.DownstreamResponse = new HttpResponseMessage();
|
||||
_downstreamContext.DownstreamResponse = new DownstreamResponse(new HttpResponseMessage());
|
||||
}
|
||||
|
||||
private void GivenTheReRouteHasPreFindAndReplaceSetUp()
|
||||
@ -98,7 +98,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
|
||||
private void ThenTheIHttpResponseHeaderReplacerIsCalledCorrectly()
|
||||
{
|
||||
_postReplacer.Verify(x => x.Replace(It.IsAny<HttpResponseMessage>(), It.IsAny<List<HeaderFindAndReplace>>(), It.IsAny<DownstreamRequest>()), Times.Once);
|
||||
_postReplacer.Verify(x => x.Replace(It.IsAny<DownstreamResponse>(), It.IsAny<List<HeaderFindAndReplace>>(), It.IsAny<DownstreamRequest>()), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingRequest()
|
||||
|
@ -7,19 +7,21 @@ using Ocelot.Configuration;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Responses;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using Moq;
|
||||
using Ocelot.Infrastructure;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Middleware.Multiplexer;
|
||||
using Ocelot.Request.Middleware;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class HttpResponseHeaderReplacerTests
|
||||
{
|
||||
private HttpResponseMessage _response;
|
||||
private DownstreamResponse _response;
|
||||
private Placeholders _placeholders;
|
||||
private HttpResponseHeaderReplacer _replacer;
|
||||
private readonly HttpResponseHeaderReplacer _replacer;
|
||||
private List<HeaderFindAndReplace> _headerFindAndReplaces;
|
||||
private Response _result;
|
||||
private DownstreamRequest _request;
|
||||
@ -37,11 +39,13 @@ namespace Ocelot.UnitTests.Headers
|
||||
[Fact]
|
||||
public void should_replace_headers()
|
||||
{
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("test", "test");
|
||||
var response = new DownstreamResponse(new StringContent(string.Empty), HttpStatusCode.Accepted,
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>()
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("test", new List<string> {"test"})
|
||||
});
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("test", "test", "chiken", 0));
|
||||
var fAndRs = new List<HeaderFindAndReplace> {new HeaderFindAndReplace("test", "test", "chiken", 0)};
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
@ -53,8 +57,11 @@ namespace Ocelot.UnitTests.Headers
|
||||
[Fact]
|
||||
public void should_not_replace_headers()
|
||||
{
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("test", "test");
|
||||
var response = new DownstreamResponse(new StringContent(string.Empty), HttpStatusCode.Accepted,
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>()
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("test", new List<string> {"test"})
|
||||
});
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
|
||||
@ -68,16 +75,21 @@ namespace Ocelot.UnitTests.Headers
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/";
|
||||
const string downstreamUrl = "http://downstream.com/";
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(downstreamUrl)};
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
var response = new DownstreamResponse(new StringContent(string.Empty), HttpStatusCode.Accepted,
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>()
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("Location", new List<string> {downstreamUrl})
|
||||
});
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
|
||||
var fAndRs = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0)
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
@ -90,16 +102,21 @@ namespace Ocelot.UnitTests.Headers
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url_with_port()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/";
|
||||
const string downstreamUrl = "http://downstream.com/";
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(downstreamUrl)};
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
var response = new DownstreamResponse(new StringContent(string.Empty), HttpStatusCode.Accepted,
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>()
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("Location", new List<string> {downstreamUrl})
|
||||
});
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:123/", 0));
|
||||
var fAndRs = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:123/", 0)
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
@ -112,16 +129,21 @@ namespace Ocelot.UnitTests.Headers
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url_and_path()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/test/product";
|
||||
const string downstreamUrl = "http://downstream.com/test/product";
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(downstreamUrl)};
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
var response = new DownstreamResponse(new StringContent(string.Empty), HttpStatusCode.Accepted,
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>()
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("Location", new List<string> {downstreamUrl})
|
||||
});
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
|
||||
var fAndRs = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0)
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
@ -134,16 +156,21 @@ namespace Ocelot.UnitTests.Headers
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url_with_path_and_port()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/test/product";
|
||||
const string downstreamUrl = "http://downstream.com/test/product";
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(downstreamUrl)};
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
var response = new DownstreamResponse(new StringContent(string.Empty), HttpStatusCode.Accepted,
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>()
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("Location", new List<string> {downstreamUrl})
|
||||
});
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:123/", 0));
|
||||
var fAndRs = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:123/", 0)
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
@ -156,16 +183,21 @@ namespace Ocelot.UnitTests.Headers
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_and_port_with_ocelot_base_url()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com:123/test/product";
|
||||
const string downstreamUrl = "http://downstream.com:123/test/product";
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(downstreamUrl)};
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
var response = new DownstreamResponse(new StringContent(string.Empty), HttpStatusCode.Accepted,
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>()
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("Location", new List<string> {downstreamUrl})
|
||||
});
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
|
||||
var fAndRs = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0)
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
@ -178,16 +210,21 @@ namespace Ocelot.UnitTests.Headers
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_and_port_with_ocelot_base_url_and_port()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com:123/test/product";
|
||||
const string downstreamUrl = "http://downstream.com:123/test/product";
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(downstreamUrl)};
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
var response = new DownstreamResponse(new StringContent(string.Empty), HttpStatusCode.Accepted,
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>()
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("Location", new List<string> {downstreamUrl})
|
||||
});
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:321/", 0));
|
||||
var fAndRs = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:321/", 0)
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
@ -207,8 +244,8 @@ namespace Ocelot.UnitTests.Headers
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _headerFindAndReplaces)
|
||||
{
|
||||
_response.Headers.TryGetValues(f.Key, out var values);
|
||||
values.ToList()[f.Index].ShouldBe("test");
|
||||
var values = _response.Headers.First(x => x.Key == f.Key);
|
||||
values.Values.ToList()[f.Index].ShouldBe("test");
|
||||
}
|
||||
}
|
||||
|
||||
@ -217,7 +254,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
_headerFindAndReplaces = fAndRs;
|
||||
}
|
||||
|
||||
private void GivenTheHttpResponse(HttpResponseMessage response)
|
||||
private void GivenTheHttpResponse(DownstreamResponse response)
|
||||
{
|
||||
_response = response;
|
||||
}
|
||||
@ -229,17 +266,17 @@ namespace Ocelot.UnitTests.Headers
|
||||
|
||||
private void ThenTheHeaderShouldBe(string key, string value)
|
||||
{
|
||||
var test = _response.Headers.GetValues(key);
|
||||
test.First().ShouldBe(value);
|
||||
var test = _response.Headers.First(x => x.Key == key);
|
||||
test.Values.First().ShouldBe(value);
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreReplaced()
|
||||
private void ThenTheHeadersAreReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _headerFindAndReplaces)
|
||||
{
|
||||
_response.Headers.TryGetValues(f.Key, out var values);
|
||||
values.ToList()[f.Index].ShouldBe(f.Replace);
|
||||
var values = _response.Headers.First(x => x.Key == f.Key);
|
||||
values.Values.ToList()[f.Index].ShouldBe(f.Replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
@ -9,7 +9,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class RemoveHeadersTests
|
||||
{
|
||||
private HttpResponseHeaders _headers;
|
||||
private List<Header> _headers;
|
||||
private readonly Ocelot.Headers.RemoveOutputHeaders _removeOutputHeaders;
|
||||
private Response _result;
|
||||
|
||||
@ -21,18 +21,18 @@ namespace Ocelot.UnitTests.Headers
|
||||
[Fact]
|
||||
public void should_remove_header()
|
||||
{
|
||||
var httpResponse = new HttpResponseMessage()
|
||||
var headers = new List<Header>()
|
||||
{
|
||||
Headers = {{ "Transfer-Encoding", "chunked"}}
|
||||
new Header("Transfer-Encoding", new List<string> {"chunked"})
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenAHttpContext(httpResponse.Headers))
|
||||
this.Given(x => x.GivenAHttpContext(headers))
|
||||
.When(x => x.WhenIRemoveTheHeaders())
|
||||
.Then(x => x.TheHeaderIsNoLongerInTheContext())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAHttpContext(HttpResponseHeaders headers)
|
||||
private void GivenAHttpContext(List<Header> headers)
|
||||
{
|
||||
_headers = headers;
|
||||
}
|
||||
|
Reference in New Issue
Block a user