mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 12:58:16 +08:00
more refactoring acceptance tests
This commit is contained in:
@ -2,13 +2,9 @@
|
||||
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;
|
||||
|
||||
@ -16,9 +12,6 @@ namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class ReturnsErrorTests : IDisposable
|
||||
{
|
||||
private TestServer _ocelotServer;
|
||||
private HttpClient _ocelotClient;
|
||||
private HttpResponseMessage _response;
|
||||
private IWebHost _servicebuilder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
@ -30,10 +23,9 @@ namespace Ocelot.AcceptanceTests
|
||||
[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<YamlReRoute>
|
||||
var yamlConfiguration = new YamlConfiguration
|
||||
{
|
||||
ReRoutes = new List<YamlReRoute>
|
||||
{
|
||||
new YamlReRoute
|
||||
{
|
||||
@ -42,34 +34,16 @@ namespace Ocelot.AcceptanceTests
|
||||
UpstreamHttpMethod = "Get",
|
||||
}
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheApiGatewayIsRunning())
|
||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53876"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(yamlConfiguration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
private void GivenTheApiGatewayIsRunning()
|
||||
{
|
||||
_ocelotServer = new TestServer(new WebHostBuilder()
|
||||
.UseStartup<Startup>());
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
@ -90,16 +64,10 @@ namespace Ocelot.AcceptanceTests
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
|
||||
{
|
||||
_response.StatusCode.ShouldBe(expectedHttpStatusCode);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_ocelotClient?.Dispose();
|
||||
_ocelotServer?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user