mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:42:50 +08:00
more refactoring acceptance tests
This commit is contained in:
parent
9798cf01e5
commit
4427ef459f
@ -19,7 +19,6 @@ namespace Ocelot.AcceptanceTests
|
|||||||
{
|
{
|
||||||
private IWebHost _servicebuilder;
|
private IWebHost _servicebuilder;
|
||||||
private readonly Steps _steps;
|
private readonly Steps _steps;
|
||||||
private BearerToken _token;
|
|
||||||
private IWebHost _identityServerBuilder;
|
private IWebHost _identityServerBuilder;
|
||||||
private string _identityServerRootUrl = "http://localhost:51888";
|
private string _identityServerRootUrl = "http://localhost:51888";
|
||||||
private string _downstreamServiceRootUrl = "http://localhost:51876/";
|
private string _downstreamServiceRootUrl = "http://localhost:51876/";
|
||||||
|
@ -3,20 +3,14 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using IdentityServer4.Models;
|
using IdentityServer4.Models;
|
||||||
using IdentityServer4.Services.InMemory;
|
using IdentityServer4.Services.InMemory;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Ocelot.Configuration.Yaml;
|
using Ocelot.Configuration.Yaml;
|
||||||
using Ocelot.ManualTest;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@ -25,11 +19,7 @@ namespace Ocelot.AcceptanceTests
|
|||||||
{
|
{
|
||||||
public class ClaimsToHeadersForwardingTests : IDisposable
|
public class ClaimsToHeadersForwardingTests : IDisposable
|
||||||
{
|
{
|
||||||
private TestServer _ocelotServer;
|
|
||||||
private HttpClient _ocelotClient;
|
|
||||||
private HttpResponseMessage _response;
|
|
||||||
private IWebHost _servicebuilder;
|
private IWebHost _servicebuilder;
|
||||||
private BearerToken _token;
|
|
||||||
private IWebHost _identityServerBuilder;
|
private IWebHost _identityServerBuilder;
|
||||||
private readonly Steps _steps;
|
private readonly Steps _steps;
|
||||||
|
|
||||||
@ -54,12 +44,9 @@ namespace Ocelot.AcceptanceTests
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:52888", "api", AccessTokenType.Jwt, user))
|
var yamlConfiguration = new YamlConfiguration
|
||||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:52876", 200))
|
{
|
||||||
.And(x => x.GivenIHaveAToken("http://localhost:52888"))
|
ReRoutes = new List<YamlReRoute>
|
||||||
.And(x => _steps.GivenThereIsAConfiguration(new YamlConfiguration
|
|
||||||
{
|
|
||||||
ReRoutes = new List<YamlReRoute>
|
|
||||||
{
|
{
|
||||||
new YamlReRoute
|
new YamlReRoute
|
||||||
{
|
{
|
||||||
@ -87,36 +74,20 @@ namespace Ocelot.AcceptanceTests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}))
|
};
|
||||||
.And(x => x.GivenTheApiGatewayIsRunning())
|
|
||||||
.And(x => x.GivenIHaveAddedATokenToMyRequest())
|
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:52888", "api", AccessTokenType.Jwt, user))
|
||||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
|
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:52876", 200))
|
||||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
.And(x => _steps.GivenIHaveAToken("http://localhost:52888"))
|
||||||
.And(x => x.ThenTheResponseBodyShouldBe("CustomerId: 123 LocationId: 1 UserType: registered UserId: 1231231"))
|
.And(x => _steps.GivenThereIsAConfiguration(yamlConfiguration))
|
||||||
|
.And(x => _steps.GivenOcelotIsRunning())
|
||||||
|
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||||
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||||
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||||
|
.And(x => _steps.ThenTheResponseBodyShouldBe("CustomerId: 123 LocationId: 1 UserType: registered UserId: 1231231"))
|
||||||
.BDDfy();
|
.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, int statusCode)
|
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
||||||
{
|
{
|
||||||
_servicebuilder = new WebHostBuilder()
|
_servicebuilder = new WebHostBuilder()
|
||||||
@ -203,71 +174,14 @@ namespace Ocelot.AcceptanceTests
|
|||||||
|
|
||||||
_identityServerBuilder.Start();
|
_identityServerBuilder.Start();
|
||||||
|
|
||||||
VerifyIdentiryServerStarted(url);
|
_steps.VerifyIdentiryServerStarted(url);
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void VerifyIdentiryServerStarted(string url)
|
|
||||||
{
|
|
||||||
using (var httpClient = new HttpClient())
|
|
||||||
{
|
|
||||||
var response = httpClient.GetAsync($"{url}/.well-known/openid-configuration").Result;
|
|
||||||
response.EnsureSuccessStatusCode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenIHaveAToken(string url)
|
|
||||||
{
|
|
||||||
var tokenUrl = $"{url}/connect/token";
|
|
||||||
var formData = new List<KeyValuePair<string, string>>
|
|
||||||
{
|
|
||||||
new KeyValuePair<string, string>("client_id", "client"),
|
|
||||||
new KeyValuePair<string, string>("client_secret", "secret"),
|
|
||||||
new KeyValuePair<string, string>("scope", "api"),
|
|
||||||
new KeyValuePair<string, string>("username", "test"),
|
|
||||||
new KeyValuePair<string, string>("password", "test"),
|
|
||||||
new KeyValuePair<string, string>("grant_type", "password")
|
|
||||||
};
|
|
||||||
var content = new FormUrlEncodedContent(formData);
|
|
||||||
|
|
||||||
using (var httpClient = new HttpClient())
|
|
||||||
{
|
|
||||||
var response = httpClient.PostAsync(tokenUrl, content).Result;
|
|
||||||
response.EnsureSuccessStatusCode();
|
|
||||||
var responseContent = response.Content.ReadAsStringAsync().Result;
|
|
||||||
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenIHaveAddedATokenToMyRequest()
|
|
||||||
{
|
|
||||||
_ocelotClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
|
|
||||||
{
|
|
||||||
_response.StatusCode.ShouldBe(expectedHttpStatusCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_servicebuilder?.Dispose();
|
_servicebuilder?.Dispose();
|
||||||
_ocelotClient?.Dispose();
|
_steps.Dispose();
|
||||||
_ocelotServer?.Dispose();
|
|
||||||
_identityServerBuilder?.Dispose();
|
_identityServerBuilder?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReSharper disable once ClassNeverInstantiated.Local
|
|
||||||
class BearerToken
|
|
||||||
{
|
|
||||||
[JsonProperty("access_token")]
|
|
||||||
public string AccessToken { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("expires_in")]
|
|
||||||
public int ExpiresIn { get; set; }
|
|
||||||
|
|
||||||
[JsonProperty("token_type")]
|
|
||||||
public string TokenType { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,15 +6,10 @@ using System.Net.Http;
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Ocelot.Configuration.Yaml;
|
using Ocelot.Configuration.Yaml;
|
||||||
using Ocelot.DependencyInjection;
|
|
||||||
using Ocelot.Middleware;
|
using Ocelot.Middleware;
|
||||||
using Ocelot.ScopedData;
|
using Ocelot.ScopedData;
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@ -22,9 +17,6 @@ namespace Ocelot.AcceptanceTests
|
|||||||
{
|
{
|
||||||
public class CustomMiddlewareTests : IDisposable
|
public class CustomMiddlewareTests : IDisposable
|
||||||
{
|
{
|
||||||
private TestServer _server;
|
|
||||||
private HttpClient _client;
|
|
||||||
private HttpResponseMessage _response;
|
|
||||||
private readonly string _configurationPath;
|
private readonly string _configurationPath;
|
||||||
private IWebHost _builder;
|
private IWebHost _builder;
|
||||||
private readonly Steps _steps;
|
private readonly Steps _steps;
|
||||||
@ -32,7 +24,7 @@ namespace Ocelot.AcceptanceTests
|
|||||||
public CustomMiddlewareTests()
|
public CustomMiddlewareTests()
|
||||||
{
|
{
|
||||||
_steps = new Steps();;
|
_steps = new Steps();;
|
||||||
_configurationPath = $"configuration.yaml";
|
_configurationPath = "configuration.yaml";
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -46,10 +38,9 @@ namespace Ocelot.AcceptanceTests
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
var yamlConfiguration = new YamlConfiguration
|
||||||
.And(x => _steps.GivenThereIsAConfiguration(new YamlConfiguration
|
{
|
||||||
{
|
ReRoutes = new List<YamlReRoute>
|
||||||
ReRoutes = new List<YamlReRoute>
|
|
||||||
{
|
{
|
||||||
new YamlReRoute
|
new YamlReRoute
|
||||||
{
|
{
|
||||||
@ -58,11 +49,14 @@ namespace Ocelot.AcceptanceTests
|
|||||||
UpstreamHttpMethod = "Get",
|
UpstreamHttpMethod = "Get",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, _configurationPath))
|
};
|
||||||
.And(x => x.GivenOcelotIsRunning(configuration))
|
|
||||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
|
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
.And(x => _steps.GivenThereIsAConfiguration(yamlConfiguration, _configurationPath))
|
||||||
.And(x => x.ThenTheResponseBodyShouldBe("PreHttpResponderMiddleware"))
|
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||||
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||||
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||||
|
.And(x => _steps.ThenTheResponseBodyShouldBe("PreHttpResponderMiddleware"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,10 +72,9 @@ namespace Ocelot.AcceptanceTests
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
var yamlConfiguration = new YamlConfiguration
|
||||||
.And(x => _steps.GivenThereIsAConfiguration(new YamlConfiguration
|
{
|
||||||
{
|
ReRoutes = new List<YamlReRoute>
|
||||||
ReRoutes = new List<YamlReRoute>
|
|
||||||
{
|
{
|
||||||
new YamlReRoute
|
new YamlReRoute
|
||||||
{
|
{
|
||||||
@ -90,49 +83,17 @@ namespace Ocelot.AcceptanceTests
|
|||||||
UpstreamHttpMethod = "Get",
|
UpstreamHttpMethod = "Get",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, _configurationPath))
|
};
|
||||||
.And(x => x.GivenOcelotIsRunning(configuration))
|
|
||||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
|
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
.And(x => _steps.GivenThereIsAConfiguration(yamlConfiguration, _configurationPath))
|
||||||
.And(x => x.ThenTheResponseBodyShouldBe("PreHttpRequesterMiddleware"))
|
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||||
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||||
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||||
|
.And(x => _steps.ThenTheResponseBodyShouldBe("PreHttpRequesterMiddleware"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <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 GivenOcelotIsRunning(OcelotMiddlewareConfiguration ocelotMiddlewareConfig)
|
|
||||||
{
|
|
||||||
var builder = new ConfigurationBuilder()
|
|
||||||
.SetBasePath(Directory.GetCurrentDirectory())
|
|
||||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
|
||||||
.AddYamlFile("configuration.yaml")
|
|
||||||
.AddEnvironmentVariables();
|
|
||||||
|
|
||||||
var configuration = builder.Build();
|
|
||||||
|
|
||||||
_server = new TestServer(new WebHostBuilder()
|
|
||||||
.UseConfiguration(configuration)
|
|
||||||
.ConfigureServices(s =>
|
|
||||||
{
|
|
||||||
s.AddOcelotYamlConfiguration(configuration);
|
|
||||||
s.AddOcelot();
|
|
||||||
})
|
|
||||||
.ConfigureLogging(l =>
|
|
||||||
{
|
|
||||||
l.AddConsole(configuration.GetSection("Logging"));
|
|
||||||
l.AddDebug();
|
|
||||||
})
|
|
||||||
.Configure(a =>
|
|
||||||
{
|
|
||||||
a.UseOcelot(ocelotMiddlewareConfig);
|
|
||||||
}));
|
|
||||||
|
|
||||||
_client = _server.CreateClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
||||||
{
|
{
|
||||||
_builder = new WebHostBuilder()
|
_builder = new WebHostBuilder()
|
||||||
@ -153,26 +114,10 @@ namespace Ocelot.AcceptanceTests
|
|||||||
_builder.Start();
|
_builder.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenIGetUrlOnTheApiGateway(string url)
|
|
||||||
{
|
|
||||||
_response = _client.GetAsync(url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
|
|
||||||
{
|
|
||||||
_response.StatusCode.ShouldBe(expectedHttpStatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheResponseBodyShouldBe(string expectedBody)
|
|
||||||
{
|
|
||||||
_response.Content.ReadAsStringAsync().Result.ShouldBe(expectedBody);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_builder?.Dispose();
|
_builder?.Dispose();
|
||||||
_client.Dispose();
|
_steps.Dispose();
|
||||||
_server.Dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,9 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Ocelot.Configuration.Yaml;
|
using Ocelot.Configuration.Yaml;
|
||||||
using Ocelot.ManualTest;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@ -16,9 +12,6 @@ namespace Ocelot.AcceptanceTests
|
|||||||
{
|
{
|
||||||
public class ReturnsErrorTests : IDisposable
|
public class ReturnsErrorTests : IDisposable
|
||||||
{
|
{
|
||||||
private TestServer _ocelotServer;
|
|
||||||
private HttpClient _ocelotClient;
|
|
||||||
private HttpResponseMessage _response;
|
|
||||||
private IWebHost _servicebuilder;
|
private IWebHost _servicebuilder;
|
||||||
private readonly Steps _steps;
|
private readonly Steps _steps;
|
||||||
|
|
||||||
@ -30,10 +23,9 @@ namespace Ocelot.AcceptanceTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_response_200_and_foward_claim_as_header()
|
public void should_return_response_200_and_foward_claim_as_header()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53876"))
|
var yamlConfiguration = new YamlConfiguration
|
||||||
.And(x => _steps.GivenThereIsAConfiguration(new YamlConfiguration
|
{
|
||||||
{
|
ReRoutes = new List<YamlReRoute>
|
||||||
ReRoutes = new List<YamlReRoute>
|
|
||||||
{
|
{
|
||||||
new YamlReRoute
|
new YamlReRoute
|
||||||
{
|
{
|
||||||
@ -42,34 +34,16 @@ namespace Ocelot.AcceptanceTests
|
|||||||
UpstreamHttpMethod = "Get",
|
UpstreamHttpMethod = "Get",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}))
|
};
|
||||||
.And(x => x.GivenTheApiGatewayIsRunning())
|
|
||||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
|
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53876"))
|
||||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
|
.And(x => _steps.GivenThereIsAConfiguration(yamlConfiguration))
|
||||||
|
.And(x => _steps.GivenOcelotIsRunning())
|
||||||
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||||
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
|
||||||
.BDDfy();
|
.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)
|
private void GivenThereIsAServiceRunningOn(string url)
|
||||||
{
|
{
|
||||||
_servicebuilder = new WebHostBuilder()
|
_servicebuilder = new WebHostBuilder()
|
||||||
@ -90,16 +64,10 @@ namespace Ocelot.AcceptanceTests
|
|||||||
_servicebuilder.Start();
|
_servicebuilder.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
|
|
||||||
{
|
|
||||||
_response.StatusCode.ShouldBe(expectedHttpStatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_servicebuilder?.Dispose();
|
_servicebuilder?.Dispose();
|
||||||
_ocelotClient?.Dispose();
|
_steps.Dispose();
|
||||||
_ocelotServer?.Dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,10 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Ocelot.Configuration.Yaml;
|
using Ocelot.Configuration.Yaml;
|
||||||
using Ocelot.ManualTest;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
using TestStack.BDDfy;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@ -17,10 +13,6 @@ namespace Ocelot.AcceptanceTests
|
|||||||
{
|
{
|
||||||
public class RoutingTests : IDisposable
|
public class RoutingTests : IDisposable
|
||||||
{
|
{
|
||||||
private TestServer _server;
|
|
||||||
private HttpClient _client;
|
|
||||||
private HttpResponseMessage _response;
|
|
||||||
private StringContent _postContent;
|
|
||||||
private IWebHost _builder;
|
private IWebHost _builder;
|
||||||
private readonly Steps _steps;
|
private readonly Steps _steps;
|
||||||
|
|
||||||
@ -33,19 +25,18 @@ namespace Ocelot.AcceptanceTests
|
|||||||
public void should_return_response_404_when_no_configuration_at_all()
|
public void should_return_response_404_when_no_configuration_at_all()
|
||||||
{
|
{
|
||||||
this.Given(x => _steps.GivenThereIsAConfiguration(new YamlConfiguration()))
|
this.Given(x => _steps.GivenThereIsAConfiguration(new YamlConfiguration()))
|
||||||
.And(x => x.GivenTheApiGatewayIsRunning())
|
.And(x => _steps.GivenOcelotIsRunning())
|
||||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_response_200_with_simple_url()
|
public void should_return_response_200_with_simple_url()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 200, "Hello from Laura"))
|
var yamlConfiguration = new YamlConfiguration
|
||||||
.And(x => _steps.GivenThereIsAConfiguration(new YamlConfiguration
|
{
|
||||||
{
|
ReRoutes = new List<YamlReRoute>
|
||||||
ReRoutes = new List<YamlReRoute>
|
|
||||||
{
|
{
|
||||||
new YamlReRoute
|
new YamlReRoute
|
||||||
{
|
{
|
||||||
@ -54,21 +45,23 @@ namespace Ocelot.AcceptanceTests
|
|||||||
UpstreamHttpMethod = "Get",
|
UpstreamHttpMethod = "Get",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}))
|
};
|
||||||
.And(x => x.GivenTheApiGatewayIsRunning())
|
|
||||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/"))
|
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 200, "Hello from Laura"))
|
||||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
.And(x => _steps.GivenThereIsAConfiguration(yamlConfiguration))
|
||||||
.And(x => x.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
.And(x => _steps.GivenOcelotIsRunning())
|
||||||
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||||
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||||
|
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_response_200_with_complex_url()
|
public void should_return_response_200_with_complex_url()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879/api/products/1", 200, "Some Product"))
|
var yamlConfiguration = new YamlConfiguration
|
||||||
.And(x => _steps.GivenThereIsAConfiguration(new YamlConfiguration
|
{
|
||||||
{
|
ReRoutes = new List<YamlReRoute>
|
||||||
ReRoutes = new List<YamlReRoute>
|
|
||||||
{
|
{
|
||||||
new YamlReRoute
|
new YamlReRoute
|
||||||
{
|
{
|
||||||
@ -77,21 +70,23 @@ namespace Ocelot.AcceptanceTests
|
|||||||
UpstreamHttpMethod = "Get"
|
UpstreamHttpMethod = "Get"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}))
|
};
|
||||||
.And(x => x.GivenTheApiGatewayIsRunning())
|
|
||||||
.When(x => x.WhenIGetUrlOnTheApiGateway("/products/1"))
|
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879/api/products/1", 200, "Some Product"))
|
||||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
.And(x => _steps.GivenThereIsAConfiguration(yamlConfiguration))
|
||||||
.And(x => x.ThenTheResponseBodyShouldBe("Some Product"))
|
.And(x => _steps.GivenOcelotIsRunning())
|
||||||
|
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/products/1"))
|
||||||
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||||
|
.And(x => _steps.ThenTheResponseBodyShouldBe("Some Product"))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_response_201_with_simple_url()
|
public void should_return_response_201_with_simple_url()
|
||||||
{
|
{
|
||||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 201, string.Empty))
|
var yamlConfiguration = new YamlConfiguration
|
||||||
.And(x => _steps.GivenThereIsAConfiguration(new YamlConfiguration
|
{
|
||||||
{
|
ReRoutes = new List<YamlReRoute>
|
||||||
ReRoutes = new List<YamlReRoute>
|
|
||||||
{
|
{
|
||||||
new YamlReRoute
|
new YamlReRoute
|
||||||
{
|
{
|
||||||
@ -100,30 +95,17 @@ namespace Ocelot.AcceptanceTests
|
|||||||
UpstreamHttpMethod = "Post"
|
UpstreamHttpMethod = "Post"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}))
|
};
|
||||||
.And(x => x.GivenTheApiGatewayIsRunning())
|
|
||||||
.And(x => x.GivenThePostHasContent("postContent"))
|
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 201, string.Empty))
|
||||||
.When(x => x.WhenIPostUrlOnTheApiGateway("/"))
|
.And(x => _steps.GivenThereIsAConfiguration(yamlConfiguration))
|
||||||
.Then(x => x.ThenTheStatusCodeShouldBe(HttpStatusCode.Created))
|
.And(x => _steps.GivenOcelotIsRunning())
|
||||||
|
.And(x => _steps.GivenThePostHasContent("postContent"))
|
||||||
|
.When(x => _steps.WhenIPostUrlOnTheApiGateway("/"))
|
||||||
|
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Created))
|
||||||
.BDDfy();
|
.BDDfy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenThePostHasContent(string postcontent)
|
|
||||||
{
|
|
||||||
_postContent = new StringContent(postcontent);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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 GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
||||||
{
|
{
|
||||||
_builder = new WebHostBuilder()
|
_builder = new WebHostBuilder()
|
||||||
@ -145,31 +127,10 @@ namespace Ocelot.AcceptanceTests
|
|||||||
_builder.Start();
|
_builder.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenIGetUrlOnTheApiGateway(string url)
|
|
||||||
{
|
|
||||||
_response = _client.GetAsync(url).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WhenIPostUrlOnTheApiGateway(string url)
|
|
||||||
{
|
|
||||||
_response = _client.PostAsync(url, _postContent).Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
|
|
||||||
{
|
|
||||||
_response.StatusCode.ShouldBe(expectedHttpStatusCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ThenTheResponseBodyShouldBe(string expectedBody)
|
|
||||||
{
|
|
||||||
_response.Content.ReadAsStringAsync().Result.ShouldBe(expectedBody);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_builder?.Dispose();
|
_builder?.Dispose();
|
||||||
_client.Dispose();
|
_steps.Dispose();
|
||||||
_server.Dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,13 @@ using System.Net.Http;
|
|||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.TestHost;
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Ocelot.Configuration.Yaml;
|
using Ocelot.Configuration.Yaml;
|
||||||
|
using Ocelot.DependencyInjection;
|
||||||
using Ocelot.ManualTest;
|
using Ocelot.ManualTest;
|
||||||
|
using Ocelot.Middleware;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using YamlDotNet.Serialization;
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
@ -21,7 +25,6 @@ namespace Ocelot.AcceptanceTests
|
|||||||
private HttpResponseMessage _response;
|
private HttpResponseMessage _response;
|
||||||
private HttpContent _postContent;
|
private HttpContent _postContent;
|
||||||
private BearerToken _token;
|
private BearerToken _token;
|
||||||
|
|
||||||
public HttpClient OcelotClient => _ocelotClient;
|
public HttpClient OcelotClient => _ocelotClient;
|
||||||
|
|
||||||
public void GivenThereIsAConfiguration(YamlConfiguration yamlConfiguration)
|
public void GivenThereIsAConfiguration(YamlConfiguration yamlConfiguration)
|
||||||
@ -67,6 +70,39 @@ namespace Ocelot.AcceptanceTests
|
|||||||
_ocelotClient = _ocelotServer.CreateClient();
|
_ocelotClient = _ocelotServer.CreateClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public void GivenOcelotIsRunning(OcelotMiddlewareConfiguration ocelotMiddlewareConfig)
|
||||||
|
{
|
||||||
|
var builder = new ConfigurationBuilder()
|
||||||
|
.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||||
|
.AddYamlFile("configuration.yaml")
|
||||||
|
.AddEnvironmentVariables();
|
||||||
|
|
||||||
|
var configuration = builder.Build();
|
||||||
|
|
||||||
|
_ocelotServer = new TestServer(new WebHostBuilder()
|
||||||
|
.UseConfiguration(configuration)
|
||||||
|
.ConfigureServices(s =>
|
||||||
|
{
|
||||||
|
s.AddOcelotYamlConfiguration(configuration);
|
||||||
|
s.AddOcelot();
|
||||||
|
})
|
||||||
|
.ConfigureLogging(l =>
|
||||||
|
{
|
||||||
|
l.AddConsole(configuration.GetSection("Logging"));
|
||||||
|
l.AddDebug();
|
||||||
|
})
|
||||||
|
.Configure(a =>
|
||||||
|
{
|
||||||
|
a.UseOcelot(ocelotMiddlewareConfig);
|
||||||
|
}));
|
||||||
|
|
||||||
|
_ocelotClient = _ocelotServer.CreateClient();
|
||||||
|
}
|
||||||
|
|
||||||
public void GivenIHaveAddedATokenToMyRequest()
|
public void GivenIHaveAddedATokenToMyRequest()
|
||||||
{
|
{
|
||||||
_ocelotClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
_ocelotClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||||
@ -95,6 +131,16 @@ namespace Ocelot.AcceptanceTests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void VerifyIdentiryServerStarted(string url)
|
||||||
|
{
|
||||||
|
using (var httpClient = new HttpClient())
|
||||||
|
{
|
||||||
|
var response = httpClient.GetAsync($"{url}/.well-known/openid-configuration").Result;
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void WhenIGetUrlOnTheApiGateway(string url)
|
public void WhenIGetUrlOnTheApiGateway(string url)
|
||||||
{
|
{
|
||||||
_response = _ocelotClient.GetAsync(url).Result;
|
_response = _ocelotClient.GetAsync(url).Result;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user