mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 14:02:49 +08:00
added happy path tests for more middlewares
This commit is contained in:
parent
52ba77f4d5
commit
7ae35f4ce3
@ -12,6 +12,7 @@
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
|
||||
private readonly IRouteRequiresAuthentication _requiresAuthentication;
|
||||
|
||||
public AuthenticationMiddleware(RequestDelegate next,
|
||||
IScopedRequestDataRepository scopedRequestDataRepository,
|
||||
IRouteRequiresAuthentication requiresAuthentication)
|
||||
|
@ -4,11 +4,11 @@ using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
{
|
||||
public class OcelotMiddleware
|
||||
public abstract class OcelotMiddleware
|
||||
{
|
||||
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
|
||||
|
||||
public OcelotMiddleware(IScopedRequestDataRepository scopedRequestDataRepository)
|
||||
protected OcelotMiddleware(IScopedRequestDataRepository scopedRequestDataRepository)
|
||||
{
|
||||
_scopedRequestDataRepository = scopedRequestDataRepository;
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
{
|
||||
public static class RequestLoggerExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseRequestLogger(this IApplicationBuilder builder)
|
||||
{
|
||||
return builder.UseMiddleware<RequestLoggerMiddleware>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
{
|
||||
public class RequestLoggerMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public RequestLoggerMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
|
||||
{
|
||||
_next = next;
|
||||
_logger = loggerFactory.CreateLogger<RequestLoggerMiddleware>();
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
_logger.LogInformation("Handling request: " + context.Request.Path);
|
||||
await _next.Invoke(context);
|
||||
_logger.LogInformation("Finished handling request.");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Library.Infrastructure.Authentication;
|
||||
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
|
||||
using Ocelot.Library.Infrastructure.Repository;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
using Ocelot.Library.Infrastructure.UrlMatcher;
|
||||
using Ocelot.Library.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
public class AuthenticationMiddlewareTests : IDisposable
|
||||
{
|
||||
private readonly Mock<IRouteRequiresAuthentication> _requiresAuth;
|
||||
private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
|
||||
private readonly string _url;
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
private HttpResponseMessage _result;
|
||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public AuthenticationMiddlewareTests()
|
||||
{
|
||||
_url = "http://localhost:51879";
|
||||
_requiresAuth = new Mock<IRouteRequiresAuthentication>();
|
||||
_scopedRepository = new Mock<IScopedRequestDataRepository>();
|
||||
|
||||
var builder = new WebHostBuilder()
|
||||
.ConfigureServices(x =>
|
||||
{
|
||||
x.AddSingleton(_requiresAuth.Object);
|
||||
x.AddSingleton(_scopedRepository.Object);
|
||||
})
|
||||
.UseUrls(_url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(_url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseAuthenticationMiddleware();
|
||||
});
|
||||
|
||||
_server = new TestServer(builder);
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void happy_path()
|
||||
{
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "any old string")))
|
||||
.And(x => x.GivenTheRouteIsNotAuthenticated())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenNoExceptionsAreThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenNoExceptionsAreThrown()
|
||||
{
|
||||
//todo not suck
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
_scopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheRouteIsNotAuthenticated()
|
||||
{
|
||||
_requiresAuth
|
||||
.Setup(x => x.IsAuthenticated(It.IsAny<DownstreamRoute>(), It.IsAny<string>()))
|
||||
.Returns(new OkResponse<bool>(false));
|
||||
}
|
||||
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
_result = _client.GetAsync(_url).Result;
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
_server.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,24 +1,21 @@
|
||||
/*
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Library.Infrastructure.Repository;
|
||||
using Ocelot.Library.Infrastructure.RequestBuilder;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
using Ocelot.Library.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Library.Infrastructure.DownstreamRouteFinder;
|
||||
using Library.Infrastructure.Repository;
|
||||
using Library.Infrastructure.RequestBuilder;
|
||||
using Library.Infrastructure.Responses;
|
||||
using Library.Infrastructure.UrlMatcher;
|
||||
using Library.Infrastructure.UrlTemplateReplacer;
|
||||
using Library.Middleware;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class HttpRequestBuilderMiddlewareTests : IDisposable
|
||||
{
|
||||
private readonly Mock<IRequestBuilder> _requestBuilder;
|
||||
@ -27,6 +24,7 @@ namespace Ocelot.UnitTests.Middleware
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
private HttpResponseMessage _result;
|
||||
private OkResponse<Request> _request;
|
||||
private OkResponse<string> _downstreamUrl;
|
||||
|
||||
public HttpRequestBuilderMiddlewareTests()
|
||||
@ -58,25 +56,26 @@ namespace Ocelot.UnitTests.Middleware
|
||||
[Fact]
|
||||
public void happy_path()
|
||||
{
|
||||
this.Given(x => x.GivenTheDownStreamUrlIs(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "any old string")))
|
||||
.And(x => x.GivenTheRequestBuilderReturns("any old string"))
|
||||
this.Given(x => x.GivenTheDownStreamUrlIs("any old string"))
|
||||
.And(x => x.GivenTheRequestBuilderReturns(new Request(new HttpRequestMessage(), new CookieContainer())))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheRequestBuilderReturns(Response<Request> request)
|
||||
private void GivenTheRequestBuilderReturns(Request request)
|
||||
{
|
||||
_downstreamUrl = new OkResponse<string>(downstreamUrl);
|
||||
_downstreamUrlTemplateVariableReplacer
|
||||
.Setup(x => x.ReplaceTemplateVariables(It.IsAny<DownstreamRoute>()))
|
||||
.Returns(_downstreamUrl);
|
||||
_request = new OkResponse<Request>(request);
|
||||
_requestBuilder
|
||||
.Setup(x => x.Build(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Stream>(), It.IsAny<IHeaderDictionary>(),
|
||||
It.IsAny<IRequestCookieCollection>(), It.IsAny<string>(), It.IsAny<string>()))
|
||||
.ReturnsAsync(_request);
|
||||
}
|
||||
|
||||
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
||||
{
|
||||
_scopedRepository
|
||||
.Verify(x => x.Add("DownstreamUrl", _downstreamUrl.Data), Times.Once());
|
||||
.Verify(x => x.Add("Request", _request.Data), Times.Once());
|
||||
}
|
||||
|
||||
private void WhenICallTheMiddleware()
|
||||
@ -84,9 +83,9 @@ namespace Ocelot.UnitTests.Middleware
|
||||
_result = _client.GetAsync(_url).Result;
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamUrlIs(string downstreamRoute)
|
||||
private void GivenTheDownStreamUrlIs(string downstreamUrl)
|
||||
{
|
||||
_downstreamUrl = new OkResponse<string>(downstreamRoute);
|
||||
_downstreamUrl = new OkResponse<string>(downstreamUrl);
|
||||
_scopedRepository
|
||||
.Setup(x => x.Get<string>(It.IsAny<string>()))
|
||||
.Returns(_downstreamUrl);
|
||||
@ -99,4 +98,3 @@ namespace Ocelot.UnitTests.Middleware
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Library.Infrastructure.Repository;
|
||||
using Ocelot.Library.Infrastructure.RequestBuilder;
|
||||
using Ocelot.Library.Infrastructure.Requester;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
using Ocelot.Library.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
public class HttpRequesterMiddlewareTests : IDisposable
|
||||
{
|
||||
private readonly Mock<IHttpRequester> _requester;
|
||||
private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
|
||||
private readonly string _url;
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
private HttpResponseMessage _result;
|
||||
private OkResponse<HttpResponseMessage> _response;
|
||||
private OkResponse<Request> _request;
|
||||
|
||||
public HttpRequesterMiddlewareTests()
|
||||
{
|
||||
_url = "http://localhost:51879";
|
||||
_requester = new Mock<IHttpRequester>();
|
||||
_scopedRepository = new Mock<IScopedRequestDataRepository>();
|
||||
|
||||
var builder = new WebHostBuilder()
|
||||
.ConfigureServices(x =>
|
||||
{
|
||||
x.AddSingleton(_requester.Object);
|
||||
x.AddSingleton(_scopedRepository.Object);
|
||||
})
|
||||
.UseUrls(_url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(_url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseHttpRequesterMiddleware();
|
||||
});
|
||||
|
||||
_server = new TestServer(builder);
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void happy_path()
|
||||
{
|
||||
this.Given(x => x.GivenTheRequestIs(new Request(new HttpRequestMessage(),new CookieContainer())))
|
||||
.And(x => x.GivenTheRequesterReturns(new HttpResponseMessage()))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheRequesterReturns(HttpResponseMessage response)
|
||||
{
|
||||
_response = new OkResponse<HttpResponseMessage>(response);
|
||||
_requester
|
||||
.Setup(x => x.GetResponse(It.IsAny<Request>()))
|
||||
.ReturnsAsync(_response);
|
||||
}
|
||||
|
||||
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
||||
{
|
||||
_scopedRepository
|
||||
.Verify(x => x.Add("Response", _response.Data), Times.Once());
|
||||
}
|
||||
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
_result = _client.GetAsync(_url).Result;
|
||||
}
|
||||
|
||||
private void GivenTheRequestIs(Request request)
|
||||
{
|
||||
_request = new OkResponse<Request>(request);
|
||||
_scopedRepository
|
||||
.Setup(x => x.Get<Request>(It.IsAny<string>()))
|
||||
.Returns(_request);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
_server.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Library.Infrastructure.Repository;
|
||||
using Ocelot.Library.Infrastructure.Responder;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
using Ocelot.Library.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Middleware
|
||||
{
|
||||
public class HttpResponderMiddlewareTests : IDisposable
|
||||
{
|
||||
private readonly Mock<IHttpResponder> _responder;
|
||||
private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
|
||||
private readonly string _url;
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
private HttpResponseMessage _result;
|
||||
private OkResponse<HttpResponseMessage> _response;
|
||||
|
||||
public HttpResponderMiddlewareTests()
|
||||
{
|
||||
_url = "http://localhost:51879";
|
||||
_responder = new Mock<IHttpResponder>();
|
||||
_scopedRepository = new Mock<IScopedRequestDataRepository>();
|
||||
|
||||
var builder = new WebHostBuilder()
|
||||
.ConfigureServices(x =>
|
||||
{
|
||||
x.AddSingleton(_responder.Object);
|
||||
x.AddSingleton(_scopedRepository.Object);
|
||||
})
|
||||
.UseUrls(_url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(_url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseHttpResponderMiddleware();
|
||||
});
|
||||
|
||||
_server = new TestServer(builder);
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void happy_path()
|
||||
{
|
||||
this.Given(x => x.GivenTheHttpResponseMessageIs(new HttpResponseMessage()))
|
||||
.And(x => x.GivenThereAreNoPipelineErrors())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.TheResponderIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereAreNoPipelineErrors()
|
||||
{
|
||||
_scopedRepository
|
||||
.Setup(x => x.Get<bool>(It.IsAny<string>()))
|
||||
.Returns(new OkResponse<bool>(false));
|
||||
}
|
||||
|
||||
private void TheResponderIsCalledCorrectly()
|
||||
{
|
||||
_responder
|
||||
.Verify(x => x.CreateResponse(It.IsAny<HttpContext>(), _response.Data), Times.Once);
|
||||
}
|
||||
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
_result = _client.GetAsync(_url).Result;
|
||||
}
|
||||
|
||||
private void GivenTheHttpResponseMessageIs(HttpResponseMessage response)
|
||||
{
|
||||
_response = new OkResponse<HttpResponseMessage>(response);
|
||||
_scopedRepository
|
||||
.Setup(x => x.Get<HttpResponseMessage>(It.IsAny<string>()))
|
||||
.Returns(_response);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
_server.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user