mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 03:18:15 +08:00
Added request id functionality and general refactoring..also turned out i wasnt returning headers....sigh
This commit is contained in:
108
test/Ocelot.AcceptanceTests/RequestIdTests.cs
Normal file
108
test/Ocelot.AcceptanceTests/RequestIdTests.cs
Normal file
@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Ocelot.Configuration.Yaml;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class RequestIdTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
public RequestIdTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_default_request_id_and_forward()
|
||||
{
|
||||
var yamlConfiguration = new YamlConfiguration
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new YamlReRoute
|
||||
{
|
||||
DownstreamTemplate = "http://localhost:51879/",
|
||||
UpstreamTemplate = "/",
|
||||
UpstreamHttpMethod = "Get",
|
||||
RequestIdKey = _steps.RequestIdKey
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(yamlConfiguration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheRequestIdIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_request_id_and_forward()
|
||||
{
|
||||
var yamlConfiguration = new YamlConfiguration
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new YamlReRoute
|
||||
{
|
||||
DownstreamTemplate = "http://localhost:51879/",
|
||||
UpstreamTemplate = "/",
|
||||
UpstreamHttpMethod = "Get",
|
||||
RequestIdKey = _steps.RequestIdKey
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var requestId = Guid.NewGuid().ToString();
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(yamlConfiguration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/", requestId))
|
||||
.Then(x => _steps.ThenTheRequestIdIsReturned(requestId))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(context =>
|
||||
{
|
||||
StringValues requestId;
|
||||
context.Request.Headers.TryGetValue(_steps.RequestIdKey, out requestId);
|
||||
context.Response.Headers.Add(_steps.RequestIdKey, requestId.First());
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
@ -26,6 +27,7 @@ namespace Ocelot.AcceptanceTests
|
||||
private HttpContent _postContent;
|
||||
private BearerToken _token;
|
||||
public HttpClient OcelotClient => _ocelotClient;
|
||||
public string RequestIdKey = "OcRequestId";
|
||||
|
||||
public void GivenThereIsAConfiguration(YamlConfiguration yamlConfiguration)
|
||||
{
|
||||
@ -146,6 +148,13 @@ namespace Ocelot.AcceptanceTests
|
||||
_response = _ocelotClient.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
public void WhenIGetUrlOnTheApiGateway(string url, string requestId)
|
||||
{
|
||||
_ocelotClient.DefaultRequestHeaders.TryAddWithoutValidation(RequestIdKey, requestId);
|
||||
|
||||
_response = _ocelotClient.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
public void WhenIPostUrlOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _ocelotClient.PostAsync(url, _postContent).Result;
|
||||
@ -171,5 +180,15 @@ namespace Ocelot.AcceptanceTests
|
||||
_ocelotClient?.Dispose();
|
||||
_ocelotServer?.Dispose();
|
||||
}
|
||||
|
||||
public void ThenTheRequestIdIsReturned()
|
||||
{
|
||||
_response.Headers.GetValues(RequestIdKey).First().ShouldNotBeNullOrEmpty();
|
||||
}
|
||||
|
||||
public void ThenTheRequestIdIsReturned(string expected)
|
||||
{
|
||||
_response.Headers.GetValues(RequestIdKey).First().ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,6 @@
|
||||
},
|
||||
"Ocelot.ManualTest": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
|
@ -50,6 +50,10 @@ ReRoutes:
|
||||
# the value must be registered
|
||||
RouteClaimsRequirement:
|
||||
UserType: registered
|
||||
# This tells Ocelot to look for a header and use its value as a request/correlation id.
|
||||
# If it is set here then the id will be forwarded to the downstream service. If it
|
||||
# does not then it will not be forwarded
|
||||
RequestIdKey: OcRequestId
|
||||
# The next re route...
|
||||
- DownstreamTemplate: http://jsonplaceholder.typicode.com/posts
|
||||
UpstreamTemplate: /posts
|
||||
|
83
test/Ocelot.UnitTests/Errors/GobalErrorHandlerTests.cs
Normal file
83
test/Ocelot.UnitTests/Errors/GobalErrorHandlerTests.cs
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.RequestId.Provider;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Errors
|
||||
{
|
||||
public class GobalErrorHandlerTests
|
||||
{
|
||||
private readonly Mock<ILoggerFactory> _loggerFactory;
|
||||
private readonly Mock<ILogger<ExceptionHandlerMiddleware>> _logger;
|
||||
private readonly Mock<IRequestIdProvider> _requestIdProvider;
|
||||
private readonly string _url;
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
private HttpResponseMessage _result;
|
||||
|
||||
public GobalErrorHandlerTests()
|
||||
{
|
||||
_url = "http://localhost:51879";
|
||||
_logger = new Mock<ILogger<ExceptionHandlerMiddleware>>();
|
||||
_loggerFactory = new Mock<ILoggerFactory>();
|
||||
_requestIdProvider = new Mock<IRequestIdProvider>();
|
||||
var builder = new WebHostBuilder()
|
||||
.ConfigureServices(x =>
|
||||
{
|
||||
x.AddSingleton(_requestIdProvider.Object);
|
||||
x.AddSingleton(_loggerFactory.Object);
|
||||
})
|
||||
.UseUrls(_url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(_url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseExceptionHandlerMiddleware();
|
||||
|
||||
app.Run(x =>
|
||||
{
|
||||
throw new Exception("BLAM");
|
||||
});
|
||||
});
|
||||
|
||||
_loggerFactory
|
||||
.Setup(x => x.CreateLogger<ExceptionHandlerMiddleware>())
|
||||
.Returns(_logger.Object);
|
||||
|
||||
_server = new TestServer(builder);
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_catch_exception_and_log()
|
||||
{
|
||||
this.When(x => x.WhenICallTheMiddleware())
|
||||
.And(x => x.TheLoggerIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void TheLoggerIsCalledCorrectly()
|
||||
{
|
||||
_logger
|
||||
.Verify(x => x.LogError(It.IsAny<EventId>(), It.IsAny<string>(), It.IsAny<Exception>()), Times.Once);
|
||||
}
|
||||
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
_result = _client.GetAsync(_url).Result;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
52
test/Ocelot.UnitTests/Headers/RemoveHeaders.cs
Normal file
52
test/Ocelot.UnitTests/Headers/RemoveHeaders.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class RemoveHeaders
|
||||
{
|
||||
private HttpResponseHeaders _headers;
|
||||
private readonly Ocelot.Headers.RemoveHeaders _removeHeaders;
|
||||
private Response _result;
|
||||
|
||||
public RemoveHeaders()
|
||||
{
|
||||
_removeHeaders = new Ocelot.Headers.RemoveHeaders();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_remove_header()
|
||||
{
|
||||
var httpResponse = new HttpResponseMessage()
|
||||
{
|
||||
Headers = {{ "Transfer-Encoding", "chunked"}}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenAHttpContext(httpResponse.Headers))
|
||||
.When(x => x.WhenIRemoveTheHeaders())
|
||||
.Then(x => x.TheHeaderIsNoLongerInTheContext())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAHttpContext(HttpResponseHeaders headers)
|
||||
{
|
||||
_headers = headers;
|
||||
}
|
||||
|
||||
private void WhenIRemoveTheHeaders()
|
||||
{
|
||||
_result = _removeHeaders.Remove(_headers);
|
||||
}
|
||||
|
||||
private void TheHeaderIsNoLongerInTheContext()
|
||||
{
|
||||
_result.IsError.ShouldBeFalse();
|
||||
_headers.ShouldNotContain(x => x.Key == "Transfer-Encoding");
|
||||
_headers.ShouldNotContain(x => x.Key == "transfer-encoding");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
using Ocelot.Errors;
|
||||
|
||||
namespace Ocelot.UnitTests.Infrastructure
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
132
test/Ocelot.UnitTests/RequestId/RequestIdMiddlewareTests.cs
Normal file
132
test/Ocelot.UnitTests/RequestId/RequestIdMiddlewareTests.cs
Normal file
@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
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.RequestId.Middleware;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.RequestId
|
||||
{
|
||||
public class RequestIdMiddlewareTests
|
||||
{
|
||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
||||
private readonly string _url;
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
private HttpResponseMessage _result;
|
||||
private string _value;
|
||||
private string _key;
|
||||
|
||||
public RequestIdMiddlewareTests()
|
||||
{
|
||||
_url = "http://localhost:51879";
|
||||
_scopedRepository = new Mock<IRequestScopedDataRepository>();
|
||||
|
||||
var builder = new WebHostBuilder()
|
||||
.ConfigureServices(x =>
|
||||
{
|
||||
x.AddSingleton(_scopedRepository.Object);
|
||||
})
|
||||
.UseUrls(_url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(_url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseRequestIdMiddleware();
|
||||
|
||||
app.Run(x =>
|
||||
{
|
||||
x.Response.Headers.Add("LSRequestId", x.TraceIdentifier);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
});
|
||||
|
||||
_server = new TestServer(builder);
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_request_id_to_repository()
|
||||
{
|
||||
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamTemplate("any old string")
|
||||
.WithRequestIdKey("LSRequestId").Build());
|
||||
|
||||
var requestId = Guid.NewGuid().ToString();
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||
.And(x => x.GivenTheRequestIdIsAddedToTheRequest("LSRequestId", requestId))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheTraceIdIs(requestId))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_trace_indentifier_to_repository()
|
||||
{
|
||||
var downstreamRoute = new DownstreamRoute(new List<UrlPathPlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamTemplate("any old string")
|
||||
.WithRequestIdKey("LSRequestId").Build());
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheTraceIdIsAnything())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheTraceIdIsAnything()
|
||||
{
|
||||
_result.Headers.GetValues("LSRequestId").First().ShouldNotBeNullOrEmpty();
|
||||
}
|
||||
|
||||
private void ThenTheTraceIdIs(string expected)
|
||||
{
|
||||
_result.Headers.GetValues("LSRequestId").First().ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void GivenTheRequestIdIsAddedToTheRequest(string key, string value)
|
||||
{
|
||||
_key = key;
|
||||
_value = value;
|
||||
_client.DefaultRequestHeaders.TryAddWithoutValidation(_key, _value);
|
||||
}
|
||||
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
_result = _client.GetAsync(_url).Result;
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
_scopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
_server.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user