Ocelot/test/Ocelot.UnitTests/Headers/HttpResponseHeaderReplacerTests.cs
Tom Pallister 463a7bdab4
Feature/websockets (#273)
* #212 - hacked websockets proxy together

* faffing around

* #212 hacking away :(

* #212 websockets proxy middleware working

* #212 map when for webockets working

* #212 some test refactor

* #212 temp commit

* #212 websockets proxy working, tests passing...need to do some tidying and write docs

* #212 more code coverage

* #212 docs for websockets

* #212 updated readme

* #212 tidying up after websockets refactoring

* #212 tidying up after websockets refactoring

* #212 tidying up after websockets refactoring

* stuck a warning in about logging levels into docs!
2018-03-23 18:01:02 +00:00

247 lines
9.6 KiB
C#

using Xunit;
using Shouldly;
using TestStack.BDDfy;
using System.Net.Http;
using Ocelot.Headers;
using Ocelot.Configuration;
using System.Collections.Generic;
using Ocelot.Responses;
using System.Linq;
using Moq;
using Ocelot.Infrastructure;
using Ocelot.Middleware;
using Ocelot.Infrastructure.RequestData;
using Ocelot.Request.Middleware;
namespace Ocelot.UnitTests.Headers
{
public class HttpResponseHeaderReplacerTests
{
private HttpResponseMessage _response;
private Placeholders _placeholders;
private HttpResponseHeaderReplacer _replacer;
private List<HeaderFindAndReplace> _headerFindAndReplaces;
private Response _result;
private DownstreamRequest _request;
private Mock<IBaseUrlFinder> _finder;
private Mock<IRequestScopedDataRepository> _repo;
public HttpResponseHeaderReplacerTests()
{
_repo = new Mock<IRequestScopedDataRepository>();
_finder = new Mock<IBaseUrlFinder>();
_placeholders = new Placeholders(_finder.Object, _repo.Object);
_replacer = new HttpResponseHeaderReplacer(_placeholders);
}
[Fact]
public void should_replace_headers()
{
var response = new HttpResponseMessage();
response.Headers.Add("test", "test");
var fAndRs = new List<HeaderFindAndReplace>();
fAndRs.Add(new HeaderFindAndReplace("test", "test", "chiken", 0));
this.Given(x => GivenTheHttpResponse(response))
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
.When(x => WhenICallTheReplacer())
.Then(x => ThenTheHeadersAreReplaced())
.BDDfy();
}
[Fact]
public void should_not_replace_headers()
{
var response = new HttpResponseMessage();
response.Headers.Add("test", "test");
var fAndRs = new List<HeaderFindAndReplace>();
this.Given(x => GivenTheHttpResponse(response))
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
.When(x => WhenICallTheReplacer())
.Then(x => ThenTheHeadersAreNotReplaced())
.BDDfy();
}
[Fact]
public void should_replace_downstream_base_url_with_ocelot_base_url()
{
var downstreamUrl = "http://downstream.com/";
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
request.RequestUri = new System.Uri(downstreamUrl);
var response = new HttpResponseMessage();
response.Headers.Add("Location", downstreamUrl);
var fAndRs = new List<HeaderFindAndReplace>();
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
this.Given(x => GivenTheHttpResponse(response))
.And(x => GivenTheRequestIs(request))
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
.When(x => WhenICallTheReplacer())
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com/"))
.BDDfy();
}
[Fact]
public void should_replace_downstream_base_url_with_ocelot_base_url_with_port()
{
var downstreamUrl = "http://downstream.com/";
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
request.RequestUri = new System.Uri(downstreamUrl);
var response = new HttpResponseMessage();
response.Headers.Add("Location", downstreamUrl);
var fAndRs = new List<HeaderFindAndReplace>();
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:123/", 0));
this.Given(x => GivenTheHttpResponse(response))
.And(x => GivenTheRequestIs(request))
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
.When(x => WhenICallTheReplacer())
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com:123/"))
.BDDfy();
}
[Fact]
public void should_replace_downstream_base_url_with_ocelot_base_url_and_path()
{
var downstreamUrl = "http://downstream.com/test/product";
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
request.RequestUri = new System.Uri(downstreamUrl);
var response = new HttpResponseMessage();
response.Headers.Add("Location", downstreamUrl);
var fAndRs = new List<HeaderFindAndReplace>();
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
this.Given(x => GivenTheHttpResponse(response))
.And(x => GivenTheRequestIs(request))
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
.When(x => WhenICallTheReplacer())
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com/test/product"))
.BDDfy();
}
[Fact]
public void should_replace_downstream_base_url_with_ocelot_base_url_with_path_and_port()
{
var downstreamUrl = "http://downstream.com/test/product";
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
request.RequestUri = new System.Uri(downstreamUrl);
var response = new HttpResponseMessage();
response.Headers.Add("Location", downstreamUrl);
var fAndRs = new List<HeaderFindAndReplace>();
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:123/", 0));
this.Given(x => GivenTheHttpResponse(response))
.And(x => GivenTheRequestIs(request))
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
.When(x => WhenICallTheReplacer())
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com:123/test/product"))
.BDDfy();
}
[Fact]
public void should_replace_downstream_base_url_and_port_with_ocelot_base_url()
{
var downstreamUrl = "http://downstream.com:123/test/product";
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
request.RequestUri = new System.Uri(downstreamUrl);
var response = new HttpResponseMessage();
response.Headers.Add("Location", downstreamUrl);
var fAndRs = new List<HeaderFindAndReplace>();
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
this.Given(x => GivenTheHttpResponse(response))
.And(x => GivenTheRequestIs(request))
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
.When(x => WhenICallTheReplacer())
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com/test/product"))
.BDDfy();
}
[Fact]
public void should_replace_downstream_base_url_and_port_with_ocelot_base_url_and_port()
{
var downstreamUrl = "http://downstream.com:123/test/product";
var request = new HttpRequestMessage(HttpMethod.Get, "http://test.com");
request.RequestUri = new System.Uri(downstreamUrl);
var response = new HttpResponseMessage();
response.Headers.Add("Location", downstreamUrl);
var fAndRs = new List<HeaderFindAndReplace>();
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:321/", 0));
this.Given(x => GivenTheHttpResponse(response))
.And(x => GivenTheRequestIs(request))
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
.When(x => WhenICallTheReplacer())
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com:321/test/product"))
.BDDfy();
}
private void GivenTheRequestIs(HttpRequestMessage request)
{
_request = new DownstreamRequest(request);
}
private void ThenTheHeadersAreNotReplaced()
{
_result.ShouldBeOfType<OkResponse>();
foreach (var f in _headerFindAndReplaces)
{
_response.Headers.TryGetValues(f.Key, out var values);
values.ToList()[f.Index].ShouldBe("test");
}
}
private void GivenTheFollowingHeaderReplacements(List<HeaderFindAndReplace> fAndRs)
{
_headerFindAndReplaces = fAndRs;
}
private void GivenTheHttpResponse(HttpResponseMessage response)
{
_response = response;
}
private void WhenICallTheReplacer()
{
_result = _replacer.Replace(_response, _headerFindAndReplaces, _request);
}
private void ThenTheHeaderShouldBe(string key, string value)
{
var test = _response.Headers.GetValues(key);
test.First().ShouldBe(value);
}
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);
}
}
}
}