added some router thing and removed loads of crap

This commit is contained in:
Tom Gardham-Pallister
2016-07-02 16:09:54 +01:00
parent 948776115c
commit 3777e8c0c8
28 changed files with 425 additions and 590 deletions

View File

@ -0,0 +1,12 @@
namespace Ocelot.ApiGateway.Infrastructure.Responses
{
public abstract class Error
{
public Error(string message)
{
Message = message;
}
public string Message { get; private set; }
}
}

View File

@ -0,0 +1,11 @@
namespace Ocelot.ApiGateway.Infrastructure.Responses
{
using System.Collections.Generic;
public class ErrorResponse : Response
{
public ErrorResponse(List<Error> errors) : base(errors)
{
}
}
}

View File

@ -0,0 +1,11 @@
namespace Ocelot.ApiGateway.Infrastructure.Responses
{
using System.Collections.Generic;
public class ErrorResponse<T> : Response<T>
{
public ErrorResponse(List<Error> errors) : base(errors)
{
}
}
}

View File

@ -0,0 +1,9 @@
namespace Ocelot.ApiGateway.Infrastructure.Responses
{
public class OkResponse : Response
{
public OkResponse()
{
}
}
}

View File

@ -0,0 +1,9 @@
namespace Ocelot.ApiGateway.Infrastructure.Responses
{
public class OkResponse<T> : Response<T>
{
public OkResponse(T data) : base(data)
{
}
}
}

View File

@ -0,0 +1,19 @@
namespace Ocelot.ApiGateway.Infrastructure.Responses
{
using System.Collections.Generic;
public abstract class Response
{
protected Response()
{
Errors = new List<Error>();
}
protected Response(List<Error> errors)
{
Errors = errors ?? new List<Error>();
}
public List<Error> Errors { get; private set; }
}
}

View File

@ -0,0 +1,18 @@
namespace Ocelot.ApiGateway.Infrastructure.Responses
{
using System.Collections.Generic;
public abstract class Response<T> : Response
{
protected Response(T data)
{
Data = data;
}
protected Response(List<Error> errors) : base(errors)
{
}
public T Data { get; private set; }
}
}

View File

@ -0,0 +1,10 @@
using Ocelot.ApiGateway.Infrastructure.Responses;
namespace Ocelot.ApiGateway.Infrastructure.Router
{
public interface IRouterService
{
Response AddRoute(string apiKey, string upstreamApiBaseUrl);
Response<Route> GetRoute(string apiKey);
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using Ocelot.ApiGateway.Infrastructure.Responses;
namespace Ocelot.ApiGateway.Infrastructure.Router
{
public class InMemoryRouterService : IRouterService
{
private Dictionary<string, string> _routes;
public InMemoryRouterService()
{
_routes = new Dictionary<string,string>();
}
public Response AddRoute(string apiKey, string upstreamApiBaseUrl)
{
if(_routes.ContainsKey(apiKey))
{
return new ErrorResponse(new List<Error>(){new RouteKeyAlreadyExists("This key has already been used")});
}
_routes.Add(apiKey, upstreamApiBaseUrl);
return new OkResponse();
}
public Response<Route> GetRoute(string apiKey)
{
Console.WriteLine("looking for {0}", apiKey);
string upstreamApiBaseUrl = null;
if(_routes.TryGetValue(apiKey, out upstreamApiBaseUrl))
{
return new OkResponse<Route>(new Route(apiKey, upstreamApiBaseUrl));
}
Console.WriteLine("Couldnt find it");
return new ErrorResponse<Route>(new List<Error>(){new RouteKeyDoesNotExist("This key does not exist")});
}
}
}

View File

@ -0,0 +1,14 @@
namespace Ocelot.ApiGateway.Infrastructure.Router
{
public class Route
{
public Route(string apiKey, string upstreamRoute)
{
ApiKey = apiKey;
UpstreamRoute = upstreamRoute;
}
public string ApiKey {get;private set;}
public string UpstreamRoute {get;private set;}
}
}

View File

@ -0,0 +1,12 @@
using Ocelot.ApiGateway.Infrastructure.Responses;
namespace Ocelot.ApiGateway.Infrastructure.Router
{
public class RouteKeyAlreadyExists : Error
{
public RouteKeyAlreadyExists(string message)
: base(message)
{
}
}
}

View File

@ -0,0 +1,12 @@
using Ocelot.ApiGateway.Infrastructure.Responses;
namespace Ocelot.ApiGateway.Infrastructure.Router
{
public class RouteKeyDoesNotExist : Error
{
public RouteKeyDoesNotExist(string message)
: base(message)
{
}
}
}

View File

@ -0,0 +1,111 @@
using System;
using Ocelot.ApiGateway.Infrastructure.Responses;
using Shouldly;
using Xunit;
namespace Ocelot.ApiGateway.Infrastructure.Router
{
public class RouterTests
{
private string _upstreamApiUrl;
private string _apiKey;
private IRouterService _router;
private Response _response;
private Response<Route> _getRouteResponse;
public RouterTests()
{
_router = new InMemoryRouterService();
}
[Fact]
public void can_add_route()
{
GivenIHaveAnUpstreamApi("http://www.someapi.com/api1");
GivenIWantToRouteRequestsToMyUpstreamApi("api");
WhenIAddTheConfiguration();
ThenTheResponseIsSuccesful();
}
[Fact]
public void can_get_route_by_key()
{
GivenIHaveSetUpAnApiKeyAndUpstreamUrl("api2", "http://www.someapi.com/api2");
WhenIRetrieveTheRouteByKey();
ThenTheRouteIsReturned();
}
[Fact]
public void should_return_error_response_when_key_already_used()
{
GivenIHaveSetUpAnApiKeyAndUpstreamUrl("api2", "http://www.someapi.com/api2");
WhenITryToUseTheSameKey();
ThenTheKeyHasAlreadyBeenUsed();
}
[Fact]
public void should_return_error_response_if_key_doesnt_exist()
{
GivenIWantToRouteRequestsToMyUpstreamApi("api");
WhenIRetrieveTheRouteByKey();
ThenTheKeyDoesNotExist();
}
private void WhenITryToUseTheSameKey()
{
WhenIAddTheConfiguration();
}
private void ThenTheKeyHasAlreadyBeenUsed()
{
_response.ShouldNotBeNull();
_response.ShouldBeOfType<ErrorResponse>();
_response.Errors[0].Message.ShouldBe("This key has already been used");
}
private void ThenTheKeyDoesNotExist()
{
_getRouteResponse.ShouldNotBeNull();
_getRouteResponse.ShouldBeOfType<ErrorResponse<Route>>();
_getRouteResponse.Errors[0].Message.ShouldBe("This key does not exist");
}
private void WhenIRetrieveTheRouteByKey()
{
_getRouteResponse = _router.GetRoute(_apiKey);
}
private void ThenTheRouteIsReturned()
{
_getRouteResponse.Data.ApiKey.ShouldBe(_apiKey);
_getRouteResponse.Data.UpstreamRoute.ShouldBe(_upstreamApiUrl);
}
private void GivenIHaveSetUpAnApiKeyAndUpstreamUrl(string apiKey, string upstreamUrl)
{
GivenIHaveAnUpstreamApi(upstreamUrl);
GivenIWantToRouteRequestsToMyUpstreamApi(apiKey);
WhenIAddTheConfiguration();
}
private void GivenIHaveAnUpstreamApi(string upstreamApiUrl)
{
_upstreamApiUrl = upstreamApiUrl;
}
private void GivenIWantToRouteRequestsToMyUpstreamApi(string apiKey)
{
_apiKey = apiKey;
}
private void WhenIAddTheConfiguration()
{
_response = _router.AddRoute(_apiKey, _upstreamApiUrl);
}
private void ThenTheResponseIsSuccesful()
{
_response.ShouldBeOfType<OkResponse>();
}
}
}