mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 04:48:17 +08:00
Added request id functionality and general refactoring..also turned out i wasnt returning headers....sigh
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
@ -7,6 +8,9 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Request.Builder;
|
||||
using Ocelot.Request.Middleware;
|
||||
@ -26,6 +30,7 @@ namespace Ocelot.UnitTests.Request
|
||||
private HttpResponseMessage _result;
|
||||
private OkResponse<Ocelot.Request.Request> _request;
|
||||
private OkResponse<string> _downstreamUrl;
|
||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public HttpRequestBuilderMiddlewareTests()
|
||||
{
|
||||
@ -56,19 +61,34 @@ namespace Ocelot.UnitTests.Request
|
||||
[Fact]
|
||||
public void happy_path()
|
||||
{
|
||||
|
||||
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithRequestIdKey("LSRequestId").Build());
|
||||
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
|
||||
.And(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||
.And(x => x.GivenTheRequestBuilderReturns(new Ocelot.Request.Request(new HttpRequestMessage(), new CookieContainer())))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
_scopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheRequestBuilderReturns(Ocelot.Request.Request request)
|
||||
{
|
||||
_request = new OkResponse<Ocelot.Request.Request>(request);
|
||||
_requestBuilder
|
||||
.Setup(x => x.Build(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Stream>(), It.IsAny<IHeaderDictionary>(),
|
||||
It.IsAny<IRequestCookieCollection>(), It.IsAny<QueryString>(), It.IsAny<string>()))
|
||||
It.IsAny<IRequestCookieCollection>(), It.IsAny<QueryString>(), It.IsAny<string>(), It.IsAny<Ocelot.RequestId.RequestId>()))
|
||||
.ReturnsAsync(_request);
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,7 @@ namespace Ocelot.UnitTests.Request
|
||||
private string _contentType;
|
||||
private readonly IRequestBuilder _requestBuilder;
|
||||
private Response<Ocelot.Request.Request> _result;
|
||||
private Ocelot.RequestId.RequestId _requestId;
|
||||
|
||||
public RequestBuilderTests()
|
||||
{
|
||||
@ -114,6 +115,62 @@ namespace Ocelot.UnitTests.Request
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_request_id()
|
||||
{
|
||||
var requestId = Guid.NewGuid().ToString();
|
||||
|
||||
this.Given(x => x.GivenIHaveHttpMethod("GET"))
|
||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
||||
.And(x => x.GivenTheHttpHeadersAre(new HeaderDictionary()))
|
||||
.And(x => x.GivenTheRequestIdIs(new Ocelot.RequestId.RequestId("RequestId", requestId)))
|
||||
.When(x => x.WhenICreateARequest())
|
||||
.And(x => x.ThenTheCorrectHeadersAreUsed(new HeaderDictionary
|
||||
{
|
||||
{"RequestId", requestId }
|
||||
}))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_use_request_if_if_already_in_headers()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveHttpMethod("GET"))
|
||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
||||
.And(x => x.GivenTheHttpHeadersAre(new HeaderDictionary
|
||||
{
|
||||
{"RequestId", "534534gv54gv45g" }
|
||||
}))
|
||||
.And(x => x.GivenTheRequestIdIs(new Ocelot.RequestId.RequestId("RequestId", Guid.NewGuid().ToString())))
|
||||
.When(x => x.WhenICreateARequest())
|
||||
.And(x => x.ThenTheCorrectHeadersAreUsed(new HeaderDictionary
|
||||
{
|
||||
{"RequestId", "534534gv54gv45g" }
|
||||
}))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, "blahh")]
|
||||
[InlineData("", "blahh")]
|
||||
[InlineData("RequestId", "")]
|
||||
[InlineData("RequestId", null)]
|
||||
public void should_not_use_request_id(string requestIdKey, string requestIdValue)
|
||||
{
|
||||
this.Given(x => x.GivenIHaveHttpMethod("GET"))
|
||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
||||
.And(x => x.GivenTheHttpHeadersAre(new HeaderDictionary()))
|
||||
.And(x => x.GivenTheRequestIdIs(new Ocelot.RequestId.RequestId(requestIdKey, requestIdValue)))
|
||||
.When(x => x.WhenICreateARequest())
|
||||
.And(x => x.ThenTheRequestIdIsNotInTheHeaders())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheRequestIdIs(Ocelot.RequestId.RequestId requestId)
|
||||
{
|
||||
_requestId = requestId;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_cookies()
|
||||
{
|
||||
@ -174,6 +231,11 @@ namespace Ocelot.UnitTests.Request
|
||||
_cookies = cookies;
|
||||
}
|
||||
|
||||
private void ThenTheRequestIdIsNotInTheHeaders()
|
||||
{
|
||||
_result.Data.HttpRequestMessage.Headers.ShouldNotContain(x => x.Key == "RequestId");
|
||||
}
|
||||
|
||||
private void ThenTheCorrectHeadersAreUsed(IHeaderDictionary expected)
|
||||
{
|
||||
var expectedHeaders = expected.Select(x => new KeyValuePair<string, string[]>(x.Key, x.Value));
|
||||
@ -219,7 +281,7 @@ namespace Ocelot.UnitTests.Request
|
||||
private void WhenICreateARequest()
|
||||
{
|
||||
_result = _requestBuilder.Build(_httpMethod, _downstreamUrl, _content?.ReadAsStreamAsync().Result, _headers,
|
||||
_cookies, _query, _contentType).Result;
|
||||
_cookies, _query, _contentType, _requestId).Result;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user