using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; using Ocelot.Configuration.Yaml; using Ocelot.ManualTest; using Shouldly; using TestStack.BDDfy; using Xunit; namespace Ocelot.AcceptanceTests { public class ReturnsErrorTests : IDisposable { private TestServer _ocelotServer; private HttpClient _ocelotClient; private HttpResponseMessage _response; private IWebHost _servicebuilder; private readonly Steps _steps; public ReturnsErrorTests() { _steps = new Steps(); } [Fact] public void should_return_response_200_and_foward_claim_as_header() { this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53876")) .And(x => _steps.GivenThereIsAConfiguration(new YamlConfiguration { ReRoutes = new List { new YamlReRoute { DownstreamTemplate = "http://localhost:53876/", UpstreamTemplate = "/", UpstreamHttpMethod = "Get", } } })) .And(x => x.GivenTheApiGatewayIsRunning()) .When(x => x.WhenIGetUrlOnTheApiGateway("/")) .Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError)) .BDDfy(); } private void WhenIGetUrlOnTheApiGateway(string url) { _response = _ocelotClient.GetAsync(url).Result; } private void ThenTheResponseBodyShouldBe(string expectedBody) { _response.Content.ReadAsStringAsync().Result.ShouldBe(expectedBody); } /// /// This is annoying cos it should be in the constructor but we need to set up the yaml file before calling startup so its a step. /// private void GivenTheApiGatewayIsRunning() { _ocelotServer = new TestServer(new WebHostBuilder() .UseStartup()); _ocelotClient = _ocelotServer.CreateClient(); } private void GivenThereIsAServiceRunningOn(string url) { _servicebuilder = new WebHostBuilder() .UseUrls(url) .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseUrls(url) .Configure(app => { app.Run(context => { throw new Exception("BLAMMMM"); }); }) .Build(); _servicebuilder.Start(); } private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode) { _response.StatusCode.ShouldBe(expectedHttpStatusCode); } public void Dispose() { _servicebuilder?.Dispose(); _ocelotClient?.Dispose(); _ocelotServer?.Dispose(); } } }