mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 14:08:15 +08:00
Feature/proxy reason phrase (#618)
* #599 started work to proxy reason phrase * #599 test for aggregator
This commit is contained in:
@ -53,7 +53,7 @@
|
||||
{ "content-type", new List<string> { "application/json" } }
|
||||
};
|
||||
|
||||
var cachedResponse = new CachedResponse(HttpStatusCode.OK, headers, "", contentHeaders);
|
||||
var cachedResponse = new CachedResponse(HttpStatusCode.OK, headers, "", contentHeaders, "some reason");
|
||||
this.Given(x => x.GivenThereIsACachedResponse(cachedResponse))
|
||||
.And(x => x.GivenTheDownstreamRouteIs())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
@ -69,7 +69,7 @@
|
||||
{ "Expires", new List<string> { "-1" } }
|
||||
};
|
||||
|
||||
var cachedResponse = new CachedResponse(HttpStatusCode.OK, new Dictionary<string, IEnumerable<string>>(), "", contentHeaders);
|
||||
var cachedResponse = new CachedResponse(HttpStatusCode.OK, new Dictionary<string, IEnumerable<string>>(), "", contentHeaders, "some reason");
|
||||
this.Given(x => x.GivenThereIsACachedResponse(cachedResponse))
|
||||
.And(x => x.GivenTheDownstreamRouteIs())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
|
@ -1,283 +1,283 @@
|
||||
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 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 DownstreamResponse _response;
|
||||
private Placeholders _placeholders;
|
||||
private readonly 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 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> {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 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>();
|
||||
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com/";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com/";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com/test/product";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com/test/product";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com:123/test/product";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com:123/test/product";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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)
|
||||
{
|
||||
var values = _response.Headers.First(x => x.Key == f.Key);
|
||||
values.Values.ToList()[f.Index].ShouldBe("test");
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenTheFollowingHeaderReplacements(List<HeaderFindAndReplace> fAndRs)
|
||||
{
|
||||
_headerFindAndReplaces = fAndRs;
|
||||
}
|
||||
|
||||
private void GivenTheHttpResponse(DownstreamResponse response)
|
||||
{
|
||||
_response = response;
|
||||
}
|
||||
|
||||
private void WhenICallTheReplacer()
|
||||
{
|
||||
_result = _replacer.Replace(_response, _headerFindAndReplaces, _request);
|
||||
}
|
||||
|
||||
private void ThenTheHeaderShouldBe(string key, string value)
|
||||
{
|
||||
var test = _response.Headers.First(x => x.Key == key);
|
||||
test.Values.First().ShouldBe(value);
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _headerFindAndReplaces)
|
||||
{
|
||||
var values = _response.Headers.First(x => x.Key == f.Key);
|
||||
values.Values.ToList()[f.Index].ShouldBe(f.Replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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 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 DownstreamResponse _response;
|
||||
private Placeholders _placeholders;
|
||||
private readonly 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 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> {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 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>();
|
||||
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com/";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com/";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com/test/product";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com/test/product";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com:123/test/product";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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()
|
||||
{
|
||||
const string downstreamUrl = "http://downstream.com:123/test/product";
|
||||
|
||||
var request =
|
||||
new HttpRequestMessage(HttpMethod.Get, "http://test.com") {RequestUri = new System.Uri(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>
|
||||
{
|
||||
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)
|
||||
{
|
||||
var values = _response.Headers.First(x => x.Key == f.Key);
|
||||
values.Values.ToList()[f.Index].ShouldBe("test");
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenTheFollowingHeaderReplacements(List<HeaderFindAndReplace> fAndRs)
|
||||
{
|
||||
_headerFindAndReplaces = fAndRs;
|
||||
}
|
||||
|
||||
private void GivenTheHttpResponse(DownstreamResponse response)
|
||||
{
|
||||
_response = response;
|
||||
}
|
||||
|
||||
private void WhenICallTheReplacer()
|
||||
{
|
||||
_result = _replacer.Replace(_response, _headerFindAndReplaces, _request);
|
||||
}
|
||||
|
||||
private void ThenTheHeaderShouldBe(string key, string value)
|
||||
{
|
||||
var test = _response.Headers.First(x => x.Key == key);
|
||||
test.Values.First().ShouldBe(value);
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _headerFindAndReplaces)
|
||||
{
|
||||
var values = _response.Headers.First(x => x.Key == f.Key);
|
||||
values.Values.ToList()[f.Index].ShouldBe(f.Replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -49,13 +49,13 @@ namespace Ocelot.UnitTests.Middleware
|
||||
|
||||
var billDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Bill says hi"), HttpStatusCode.OK, new EditableList<KeyValuePair<string, IEnumerable<string>>>()),
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Bill says hi"), HttpStatusCode.OK, new EditableList<KeyValuePair<string, IEnumerable<string>>>(), "some reason"),
|
||||
DownstreamReRoute = billDownstreamReRoute
|
||||
};
|
||||
|
||||
var georgeDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("George says hi"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>()),
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("George says hi"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"),
|
||||
DownstreamReRoute = georgeDownstreamReRoute
|
||||
};
|
||||
|
||||
@ -69,6 +69,7 @@ namespace Ocelot.UnitTests.Middleware
|
||||
.When(x => WhenIAggregate())
|
||||
.Then(x => ThenTheContentIs(expected))
|
||||
.And(x => ThenTheContentTypeIs("application/json"))
|
||||
.And(x => ThenTheReasonPhraseIs("cannot return from aggregate..which reason phrase would you use?"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -91,13 +92,13 @@ namespace Ocelot.UnitTests.Middleware
|
||||
|
||||
var billDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Bill says hi"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>()),
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Bill says hi"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"),
|
||||
DownstreamReRoute = billDownstreamReRoute
|
||||
};
|
||||
|
||||
var georgeDownstreamContext = new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Error"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>()),
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Error"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason"),
|
||||
DownstreamReRoute = georgeDownstreamReRoute,
|
||||
};
|
||||
|
||||
@ -116,6 +117,11 @@ namespace Ocelot.UnitTests.Middleware
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheReasonPhraseIs(string expected)
|
||||
{
|
||||
_upstreamContext.DownstreamResponse.ReasonPhrase.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenTheErrorIsMapped()
|
||||
{
|
||||
_upstreamContext.Errors.ShouldBe(_downstreamContexts[1].Errors);
|
||||
|
@ -43,11 +43,11 @@ namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Tom"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>())
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Tom"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason")
|
||||
},
|
||||
new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Laura"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>())
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Laura"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason")
|
||||
}
|
||||
};
|
||||
|
||||
@ -72,11 +72,11 @@ namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Tom"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>())
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Tom"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason")
|
||||
},
|
||||
new DownstreamContext(new DefaultHttpContext())
|
||||
{
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Laura"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>())
|
||||
DownstreamResponse = new DownstreamResponse(new StringContent("Laura"), HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason")
|
||||
}
|
||||
};
|
||||
|
||||
@ -146,7 +146,7 @@ namespace Ocelot.UnitTests.Middleware
|
||||
var laura = await responses[1].Content.ReadAsStringAsync();
|
||||
var content = $"{tom}, {laura}";
|
||||
var headers = responses.SelectMany(x => x.Headers).ToList();
|
||||
return new DownstreamResponse(new StringContent(content), HttpStatusCode.OK, headers);
|
||||
return new DownstreamResponse(new StringContent(content), HttpStatusCode.OK, headers, "some reason");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Ocelot.Headers;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Middleware.Multiplexer;
|
||||
@ -31,7 +32,7 @@ namespace Ocelot.UnitTests.Responder
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("Transfer-Encoding", new List<string> {"woop"})
|
||||
});
|
||||
}, "some reason");
|
||||
|
||||
_responder.SetResponseOnHttpContext(httpContext, response).GetAwaiter().GetResult();
|
||||
var header = httpContext.Response.Headers["Transfer-Encoding"];
|
||||
@ -43,7 +44,7 @@ namespace Ocelot.UnitTests.Responder
|
||||
{
|
||||
var httpContext = new DefaultHttpContext();
|
||||
var response = new DownstreamResponse(new StringContent("test"), HttpStatusCode.OK,
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>());
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>(), "some reason");
|
||||
|
||||
_responder.SetResponseOnHttpContext(httpContext, response).GetAwaiter().GetResult();
|
||||
var header = httpContext.Response.Headers["Content-Length"];
|
||||
@ -58,13 +59,28 @@ namespace Ocelot.UnitTests.Responder
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("test", new List<string> {"test"})
|
||||
});
|
||||
}, "some reason");
|
||||
|
||||
_responder.SetResponseOnHttpContext(httpContext, response).GetAwaiter().GetResult();
|
||||
var header = httpContext.Response.Headers["test"];
|
||||
header.First().ShouldBe("test");
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_add_reason_phrase()
|
||||
{
|
||||
var httpContext = new DefaultHttpContext();
|
||||
var response = new DownstreamResponse(new StringContent(""), HttpStatusCode.OK,
|
||||
new List<KeyValuePair<string, IEnumerable<string>>>
|
||||
{
|
||||
new KeyValuePair<string, IEnumerable<string>>("test", new List<string> {"test"})
|
||||
}, "some reason");
|
||||
|
||||
_responder.SetResponseOnHttpContext(httpContext, response).GetAwaiter().GetResult();
|
||||
httpContext.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase.ShouldBe(response.ReasonPhrase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_without_exception()
|
||||
{
|
||||
|
Reference in New Issue
Block a user