mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 16:28:17 +08:00
Brought in YAML lib for configuration and added failing acceptance test
This commit is contained in:
@ -1,54 +0,0 @@
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Library.Infrastructure.Configuration;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class ConfigurationReaderTests
|
||||
{
|
||||
private readonly IConfigurationReader _configurationReader;
|
||||
private string _configPath;
|
||||
private Configuration _result;
|
||||
|
||||
public ConfigurationReaderTests()
|
||||
{
|
||||
_configurationReader = new ConfigurationReader();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_read_configuration()
|
||||
{
|
||||
const string path = "./ConfigurationReaderTests.can_read_configuration.yaml";
|
||||
|
||||
var expected =
|
||||
new Configuration(new List<Route>
|
||||
{
|
||||
new Route("productservice/category/{categoryId}/products/{productId}/variants/{variantId}",
|
||||
"https://www.moonpig.com/api/products/{categoryId}/{productId}/{variantId}")
|
||||
});
|
||||
|
||||
this.Given(x => x.GivenAConfigPathOf(path))
|
||||
.When(x => x.WhenICallTheConfigurationReader())
|
||||
.Then(x => x.ThenTheFollowingConfigurationIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAConfigPathOf(string configPath)
|
||||
{
|
||||
_configPath = configPath;
|
||||
}
|
||||
|
||||
private void WhenICallTheConfigurationReader()
|
||||
{
|
||||
_result = _configurationReader.Read(_configPath);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingConfigurationIsReturned(Configuration expected)
|
||||
{
|
||||
_result.Routes[0].Downstream.ShouldBe(expected.Routes[0].Downstream);
|
||||
_result.Routes[0].Upstream.ShouldBe(expected.Routes[0].Upstream);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,37 +1,36 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Xunit;
|
||||
using Ocelot.AcceptanceTests.Fake;
|
||||
using Shouldly;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Xunit;
|
||||
using Ocelot.AcceptanceTests.Fake;
|
||||
using Shouldly;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using Library.Infrastructure.Configuration;
|
||||
using TestStack.BDDfy;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
public class OcelotTests : IDisposable
|
||||
{
|
||||
private readonly FakeService _fakeService;
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
private TestServer _server;
|
||||
private HttpClient _client;
|
||||
private HttpResponseMessage _response;
|
||||
|
||||
public OcelotTests()
|
||||
{
|
||||
_server = new TestServer(new WebHostBuilder()
|
||||
.UseStartup<Startup>());
|
||||
|
||||
_client = _server.CreateClient();
|
||||
|
||||
_fakeService = new FakeService();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_404()
|
||||
{
|
||||
this.When(x => x.WhenIRequestTheUrl("/"))
|
||||
this.Given(x => x.GivenTheApiGatewayIsRunning())
|
||||
.When(x => x.WhenIRequestTheUrlOnTheApiGateway("/"))
|
||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
|
||||
.BDDfy();
|
||||
}
|
||||
@ -39,13 +38,57 @@ namespace Ocelot.AcceptanceTests
|
||||
[Fact]
|
||||
public void should_return_response_200()
|
||||
{
|
||||
this.When(x => x.WhenIRequestTheUrl("/"))
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => x.GivenThereIsAConfiguration(new Configuration
|
||||
{
|
||||
Routes = new List<Route>
|
||||
{
|
||||
new Route
|
||||
{
|
||||
Downstream = "http://localhost:51879/",
|
||||
Upstream = "/heee"
|
||||
}
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheApiGatewayIsRunning())
|
||||
.When(x => x.WhenIRequestTheUrlOnTheApiGateway("/"))
|
||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void WhenIRequestTheUrl(string url)
|
||||
/// <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()
|
||||
{
|
||||
_server = new TestServer(new WebHostBuilder()
|
||||
.UseStartup<Startup>());
|
||||
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
|
||||
private void GivenThereIsAConfiguration(Configuration configuration)
|
||||
{
|
||||
var serializer = new Serializer();
|
||||
|
||||
if (File.Exists("./configuration.yaml"))
|
||||
{
|
||||
File.Delete("./configuration.yaml");
|
||||
}
|
||||
|
||||
using (TextWriter writer = File.CreateText("./configuration.yaml"))
|
||||
{
|
||||
serializer.Serialize(writer, configuration);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url)
|
||||
{
|
||||
_fakeService.Start(url);
|
||||
}
|
||||
|
||||
private void WhenIRequestTheUrlOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _client.GetAsync("/").Result;
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
routes:
|
||||
- downstream: "productservice/category/{categoryId}/products/{productId}/variants/{variantId}"
|
||||
upstream: "https://www.moonpig.com/api/products/{categoryId}/{productId}/{variantId}"
|
||||
Routes:
|
||||
- Downstream: http://localhost:51879/
|
||||
Upstream: /heee
|
||||
|
Reference in New Issue
Block a user