mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-08-04 20:22:27 +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:
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user