mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:42:50 +08:00
adding midleware tests
This commit is contained in:
parent
20181ab4ee
commit
52ba77f4d5
@ -0,0 +1,89 @@
|
|||||||
|
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.Responses;
|
||||||
|
using Library.Infrastructure.UrlMatcher;
|
||||||
|
using Library.Middleware;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Moq;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
public class DownstreamRouteFinderMiddlewareTests : IDisposable
|
||||||
|
{
|
||||||
|
private readonly Mock<IDownstreamRouteFinder> _downstreamRouteFinder;
|
||||||
|
private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
|
||||||
|
private readonly string _url;
|
||||||
|
private readonly TestServer _server;
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
private Response<DownstreamRoute> _downstreamRoute;
|
||||||
|
private HttpResponseMessage _result;
|
||||||
|
|
||||||
|
public DownstreamRouteFinderMiddlewareTests()
|
||||||
|
{
|
||||||
|
_url = "http://localhost:51879";
|
||||||
|
_downstreamRouteFinder = new Mock<IDownstreamRouteFinder>();
|
||||||
|
_scopedRepository = new Mock<IScopedRequestDataRepository>();
|
||||||
|
|
||||||
|
var builder = new WebHostBuilder()
|
||||||
|
.ConfigureServices(x =>
|
||||||
|
{
|
||||||
|
x.AddSingleton(_downstreamRouteFinder.Object);
|
||||||
|
x.AddSingleton(_scopedRepository.Object);
|
||||||
|
})
|
||||||
|
.UseUrls(_url)
|
||||||
|
.UseKestrel()
|
||||||
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
|
.UseIISIntegration()
|
||||||
|
.UseUrls(_url)
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseDownstreamRouteFinderMiddleware();
|
||||||
|
});
|
||||||
|
|
||||||
|
_server = new TestServer(builder);
|
||||||
|
_client = _server.CreateClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void happy_path()
|
||||||
|
{
|
||||||
|
this.Given(x => x.GivenTheDownStreamRouteFinderReturns(new DownstreamRoute(new List<TemplateVariableNameAndValue>(), "any old string")))
|
||||||
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
|
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
||||||
|
{
|
||||||
|
_scopedRepository
|
||||||
|
.Verify(x => x.Add("DownstreamRoute", _downstreamRoute.Data), Times.Once());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenICallTheMiddleware()
|
||||||
|
{
|
||||||
|
_result = _client.GetAsync(_url).Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRoute)
|
||||||
|
{
|
||||||
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
|
_downstreamRouteFinder
|
||||||
|
.Setup(x => x.FindDownstreamRoute(It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
.Returns(_downstreamRoute);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_client.Dispose();
|
||||||
|
_server.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,100 @@
|
|||||||
|
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.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 DownstreamUrlCreatorMiddlewareTests : IDisposable
|
||||||
|
{
|
||||||
|
private readonly Mock<IDownstreamUrlTemplateVariableReplacer> _downstreamUrlTemplateVariableReplacer;
|
||||||
|
private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
|
||||||
|
private readonly string _url;
|
||||||
|
private readonly TestServer _server;
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
private Response<DownstreamRoute> _downstreamRoute;
|
||||||
|
private HttpResponseMessage _result;
|
||||||
|
private OkResponse<string> _downstreamUrl;
|
||||||
|
|
||||||
|
public DownstreamUrlCreatorMiddlewareTests()
|
||||||
|
{
|
||||||
|
_url = "http://localhost:51879";
|
||||||
|
_downstreamUrlTemplateVariableReplacer = new Mock<IDownstreamUrlTemplateVariableReplacer>();
|
||||||
|
_scopedRepository = new Mock<IScopedRequestDataRepository>();
|
||||||
|
|
||||||
|
var builder = new WebHostBuilder()
|
||||||
|
.ConfigureServices(x =>
|
||||||
|
{
|
||||||
|
x.AddSingleton(_downstreamUrlTemplateVariableReplacer.Object);
|
||||||
|
x.AddSingleton(_scopedRepository.Object);
|
||||||
|
})
|
||||||
|
.UseUrls(_url)
|
||||||
|
.UseKestrel()
|
||||||
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
|
.UseIISIntegration()
|
||||||
|
.UseUrls(_url)
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseDownstreamUrlCreatorMiddleware();
|
||||||
|
});
|
||||||
|
|
||||||
|
_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.TheUrlReplacerReturns("any old string"))
|
||||||
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
|
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TheUrlReplacerReturns(string downstreamUrl)
|
||||||
|
{
|
||||||
|
_downstreamUrl = new OkResponse<string>(downstreamUrl);
|
||||||
|
_downstreamUrlTemplateVariableReplacer
|
||||||
|
.Setup(x => x.ReplaceTemplateVariables(It.IsAny<DownstreamRoute>()))
|
||||||
|
.Returns(_downstreamUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
||||||
|
{
|
||||||
|
_scopedRepository
|
||||||
|
.Verify(x => x.Add("DownstreamUrl", _downstreamUrl.Data), Times.Once());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenICallTheMiddleware()
|
||||||
|
{
|
||||||
|
_result = _client.GetAsync(_url).Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||||
|
{
|
||||||
|
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||||
|
.Returns(_downstreamRoute);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_client.Dispose();
|
||||||
|
_server.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
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;
|
||||||
|
private readonly Mock<IScopedRequestDataRepository> _scopedRepository;
|
||||||
|
private readonly string _url;
|
||||||
|
private readonly TestServer _server;
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
private HttpResponseMessage _result;
|
||||||
|
private OkResponse<string> _downstreamUrl;
|
||||||
|
|
||||||
|
public HttpRequestBuilderMiddlewareTests()
|
||||||
|
{
|
||||||
|
_url = "http://localhost:51879";
|
||||||
|
_requestBuilder = new Mock<IRequestBuilder>();
|
||||||
|
_scopedRepository = new Mock<IScopedRequestDataRepository>();
|
||||||
|
|
||||||
|
var builder = new WebHostBuilder()
|
||||||
|
.ConfigureServices(x =>
|
||||||
|
{
|
||||||
|
x.AddSingleton(_requestBuilder.Object);
|
||||||
|
x.AddSingleton(_scopedRepository.Object);
|
||||||
|
})
|
||||||
|
.UseUrls(_url)
|
||||||
|
.UseKestrel()
|
||||||
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
|
.UseIISIntegration()
|
||||||
|
.UseUrls(_url)
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseHttpRequestBuilderMiddleware();
|
||||||
|
});
|
||||||
|
|
||||||
|
_server = new TestServer(builder);
|
||||||
|
_client = _server.CreateClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
[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"))
|
||||||
|
.When(x => x.WhenICallTheMiddleware())
|
||||||
|
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||||
|
.BDDfy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheRequestBuilderReturns(Response<Request> request)
|
||||||
|
{
|
||||||
|
_downstreamUrl = new OkResponse<string>(downstreamUrl);
|
||||||
|
_downstreamUrlTemplateVariableReplacer
|
||||||
|
.Setup(x => x.ReplaceTemplateVariables(It.IsAny<DownstreamRoute>()))
|
||||||
|
.Returns(_downstreamUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
||||||
|
{
|
||||||
|
_scopedRepository
|
||||||
|
.Verify(x => x.Add("DownstreamUrl", _downstreamUrl.Data), Times.Once());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WhenICallTheMiddleware()
|
||||||
|
{
|
||||||
|
_result = _client.GetAsync(_url).Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenTheDownStreamUrlIs(string downstreamRoute)
|
||||||
|
{
|
||||||
|
_downstreamUrl = new OkResponse<string>(downstreamRoute);
|
||||||
|
_scopedRepository
|
||||||
|
.Setup(x => x.Get<string>(It.IsAny<string>()))
|
||||||
|
.Returns(_downstreamUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_client.Dispose();
|
||||||
|
_server.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
Loading…
x
Reference in New Issue
Block a user