mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-05-03 02:52:50 +08:00

* started messing around with this on the train last night * mega hacking away to change middleware into Ocelot iddleware * scoped data repo back in * broken commit getting tests working * another broken commit farting around with tests * all unit tests passing again * mw pipeline for ocelot...still loads of hacks but getting there now to get acceptance tests working, then fix config so you can have aggregate and then imlement multiplexer, then mapping to response...loads to do * all tests passing before aggregation feature implemented * removed all the request middleware stuff we dont need it * updated how errors work...tho i think there could be edge case here when aggregating because one downstream could error and this would effect another * removed multiplexer so you dont have to send route down, this isnt very thread safe...sigh * hacking around getting the config for aggregates in, this might change * refactored builder and unit tests passing now * Updated a bunch of ports for tests * plugged in code to create reroutes that are aggregates * made multiplexer a class * hacked test to death * simple aggregator done, initial validation done * removed request id from context, it is still specific for http request * now aggregates to json always * docs for aggregate reroutes * Updated docs
96 lines
3.4 KiB
C#
96 lines
3.4 KiB
C#
using Ocelot.Configuration;
|
|
using Ocelot.Middleware;
|
|
|
|
namespace Ocelot.UnitTests.Authentication
|
|
{
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Moq;
|
|
using Ocelot.Authentication.Middleware;
|
|
using Ocelot.Configuration.Builder;
|
|
using Ocelot.DownstreamRouteFinder;
|
|
using Ocelot.DownstreamRouteFinder.Middleware;
|
|
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
|
using Ocelot.Logging;
|
|
using Ocelot.Responses;
|
|
using Shouldly;
|
|
using TestStack.BDDfy;
|
|
using Xunit;
|
|
|
|
public class AuthenticationMiddlewareTests
|
|
{
|
|
private OkResponse<DownstreamRoute> _downstreamRoute;
|
|
private AuthenticationMiddleware _middleware;
|
|
private Mock<IOcelotLoggerFactory> _factory;
|
|
private Mock<IOcelotLogger> _logger;
|
|
private OcelotRequestDelegate _next;
|
|
private DownstreamContext _downstreamContext;
|
|
|
|
public AuthenticationMiddlewareTests()
|
|
{
|
|
_factory = new Mock<IOcelotLoggerFactory>();
|
|
_logger = new Mock<IOcelotLogger>();
|
|
_factory.Setup(x => x.CreateLogger<AuthenticationMiddleware>()).Returns(_logger.Object);
|
|
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
|
|
}
|
|
|
|
[Fact]
|
|
public void should_call_next_middleware_if_route_is_not_authenticated()
|
|
{
|
|
this.Given(x => GivenTheDownStreamRouteIs(
|
|
new DownstreamReRouteBuilder().WithUpstreamHttpMethod(new List<string> { "Get" }).Build()))
|
|
.And(x => GivenTheTestServerPipelineIsConfigured())
|
|
.When(x => WhenICallTheMiddleware())
|
|
.Then(x => ThenTheUserIsAuthenticated())
|
|
.BDDfy();
|
|
}
|
|
|
|
private void WhenICallTheMiddleware()
|
|
{
|
|
_next = async (context) => {
|
|
byte[] byteArray = Encoding.ASCII.GetBytes("The user is authenticated");
|
|
MemoryStream stream = new MemoryStream(byteArray);
|
|
context.HttpContext.Response.Body = stream;
|
|
};
|
|
_middleware = new AuthenticationMiddleware(_next, _factory.Object);
|
|
_middleware.Invoke(_downstreamContext).GetAwaiter().GetResult();
|
|
}
|
|
|
|
private void GivenTheTestServerPipelineIsConfigured()
|
|
{
|
|
_next = async (context) => {
|
|
byte[] byteArray = Encoding.ASCII.GetBytes("The user is authenticated");
|
|
MemoryStream stream = new MemoryStream(byteArray);
|
|
context.HttpContext.Response.Body = stream;
|
|
};
|
|
}
|
|
|
|
private void ThenTheUserIsAuthenticated()
|
|
{
|
|
var content = _downstreamContext.HttpContext.Response.Body.AsString();
|
|
content.ShouldBe("The user is authenticated");
|
|
}
|
|
|
|
private void GivenTheDownStreamRouteIs(DownstreamReRoute downstreamRoute)
|
|
{
|
|
_downstreamContext.DownstreamReRoute = downstreamRoute;
|
|
}
|
|
}
|
|
|
|
public static class StreamExtensions
|
|
{
|
|
public static string AsString(this Stream stream)
|
|
{
|
|
using(var reader = new StreamReader(stream))
|
|
{
|
|
string text = reader.ReadToEnd();
|
|
return text;
|
|
};
|
|
}
|
|
}
|
|
}
|