namespace Ocelot.UnitTests.Headers { using System.Collections.Generic; using System.Net.Http; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Moq; using Ocelot.Configuration; using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; using Ocelot.DownstreamRouteFinder.UrlMatcher; using Ocelot.Headers; using Ocelot.Headers.Middleware; using Ocelot.Logging; using Ocelot.Responses; using TestStack.BDDfy; using Xunit; public class HttpRequestHeadersBuilderMiddlewareTests : ServerHostedMiddlewareTest { private readonly Mock _addHeaders; private readonly HttpRequestMessage _downstreamRequest; private Response _downstreamRoute; public HttpRequestHeadersBuilderMiddlewareTests() { _addHeaders = new Mock(); _downstreamRequest = new HttpRequestMessage(); ScopedRepository .Setup(sr => sr.Get("DownstreamRequest")) .Returns(new OkResponse(_downstreamRequest)); GivenTheTestServerIsConfigured(); } [Fact] public void should_call_add_headers_to_request_correctly() { var downstreamRoute = new DownstreamRoute(new List(), new ReRouteBuilder() .WithDownstreamPathTemplate("any old string") .WithClaimsToHeaders(new List { new ClaimToThing("UserId", "Subject", "", 0) }) .WithUpstreamHttpMethod(new List { "Get" }) .Build()); this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute)) .And(x => x.GivenTheAddHeadersToDownstreamRequestReturnsOk()) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheAddHeadersToRequestIsCalledCorrectly()) .BDDfy(); } protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services) { services.AddSingleton(); services.AddLogging(); services.AddSingleton(_addHeaders.Object); services.AddSingleton(ScopedRepository.Object); } protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app) { app.UseHttpRequestHeadersBuilderMiddleware(); } private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute) { _downstreamRoute = new OkResponse(downstreamRoute); ScopedRepository .Setup(x => x.Get(It.IsAny())) .Returns(_downstreamRoute); } private void GivenTheAddHeadersToDownstreamRequestReturnsOk() { _addHeaders .Setup(x => x.SetHeadersOnDownstreamRequest( It.IsAny>(), It.IsAny>(), It.IsAny())) .Returns(new OkResponse()); } private void ThenTheAddHeadersToRequestIsCalledCorrectly() { _addHeaders .Verify(x => x.SetHeadersOnDownstreamRequest( It.IsAny>(), It.IsAny>(), _downstreamRequest), Times.Once); } } }