mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-23 00:32:50 +08:00
Get rest of unit tests working
This commit is contained in:
parent
7c1a277147
commit
a1c6ab4ce4
@ -43,20 +43,6 @@ namespace Ocelot.DownstreamUrlCreator.Middleware
|
||||
return;
|
||||
}
|
||||
|
||||
//var dsScheme = DownstreamRoute.ReRoute.DownstreamScheme;
|
||||
|
||||
//var dsHostAndPort = HostAndPort;
|
||||
|
||||
//var dsUrl = _urlBuilder.Build(dsPath.Data.Value, dsScheme, dsHostAndPort);
|
||||
|
||||
//if (dsUrl.IsError)
|
||||
//{
|
||||
// _logger.LogDebug("IUrlBuilder returned an error, setting pipeline error");
|
||||
|
||||
// SetPipelineError(dsUrl.Errors);
|
||||
// return;
|
||||
//}
|
||||
|
||||
var uriBuilder = new UriBuilder(DownstreamRequest.RequestUri)
|
||||
{
|
||||
Path = dsPath.Data.Value,
|
||||
@ -67,8 +53,6 @@ namespace Ocelot.DownstreamUrlCreator.Middleware
|
||||
|
||||
_logger.LogDebug("downstream url is {downstreamUrl.Data.Value}", DownstreamRequest.RequestUri);
|
||||
|
||||
//SetDownstreamUrlForThisRequest(dsUrl.Data.Value);
|
||||
|
||||
_logger.LogDebug("calling next middleware");
|
||||
|
||||
await _next.Invoke(context);
|
||||
|
@ -43,17 +43,6 @@ namespace Ocelot.Request.Middleware
|
||||
return;
|
||||
}
|
||||
|
||||
//var buildResult = await _requestCreator
|
||||
// .Build(context.Request.Method,
|
||||
// DownstreamUrl,
|
||||
// context.Request.Body,
|
||||
// context.Request.Headers,
|
||||
// context.Request.QueryString,
|
||||
// context.Request.ContentType,
|
||||
// new RequestId.RequestId(DownstreamRoute?.ReRoute?.RequestIdKey, context.TraceIdentifier),
|
||||
// DownstreamRoute.ReRoute.IsQos,
|
||||
// qosProvider.Data);
|
||||
|
||||
var buildResult = await _requestCreator.Build(
|
||||
DownstreamRequest,
|
||||
DownstreamRoute.ReRoute.IsQos,
|
||||
|
@ -18,6 +18,7 @@ using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamUrlCreator
|
||||
{
|
||||
@ -30,10 +31,9 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
private HttpResponseMessage _result;
|
||||
private OkResponse<DownstreamPath> _downstreamPath;
|
||||
private OkResponse<DownstreamUrl> _downstreamUrl;
|
||||
private HostAndPort _hostAndPort;
|
||||
private HttpRequestMessage _downstreamRequest;
|
||||
private HttpResponseMessage _result;
|
||||
|
||||
public DownstreamUrlCreatorMiddlewareTests()
|
||||
{
|
||||
@ -60,69 +60,34 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
|
||||
app.UseDownstreamUrlCreatorMiddleware();
|
||||
});
|
||||
|
||||
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "https://my.url/abc/?q=123");
|
||||
|
||||
_scopedRepository
|
||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||
.Returns(new OkResponse<HttpRequestMessage>(new HttpRequestMessage(HttpMethod.Get, "https://my.url/abc/?q=123")));
|
||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||
|
||||
_server = new TestServer(builder);
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_dependencies_correctly()
|
||||
public void should_replace_scheme_and_path()
|
||||
{
|
||||
var hostAndPort = new HostAndPort("127.0.0.1", 80);
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(
|
||||
new DownstreamRoute(
|
||||
new List<UrlPathPlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("any old string")
|
||||
.WithUpstreamHttpMethod("Get")
|
||||
.WithDownstreamScheme("https")
|
||||
.Build())))
|
||||
.And(x => x.GivenTheHostAndPortIs(hostAndPort))
|
||||
.And(x => x.TheUrlReplacerReturns("/api/products/1"))
|
||||
.And(x => x.TheUrlBuilderReturns("http://127.0.0.1:80/api/products/1"))
|
||||
.And(x => x.GivenTheDownstreamRequestUriIs("http://my.url/abc?q=123"))
|
||||
.And(x => x.GivenTheUrlReplacerWillReturn("/api/products/1"))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||
.Then(x => x.ThenTheDownstreamRequestUriIs("https://my.url:80/api/products/1?q=123"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheHostAndPortIs(HostAndPort hostAndPort)
|
||||
{
|
||||
_hostAndPort = hostAndPort;
|
||||
_scopedRepository
|
||||
.Setup(x => x.Get<HostAndPort>("HostAndPort"))
|
||||
.Returns(new OkResponse<HostAndPort>(_hostAndPort));
|
||||
}
|
||||
|
||||
private void TheUrlBuilderReturns(string dsUrl)
|
||||
{
|
||||
_downstreamUrl = new OkResponse<DownstreamUrl>(new DownstreamUrl(dsUrl));
|
||||
_urlBuilder
|
||||
.Setup(x => x.Build(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<HostAndPort>()))
|
||||
.Returns(_downstreamUrl);
|
||||
}
|
||||
|
||||
private void TheUrlReplacerReturns(string downstreamUrl)
|
||||
{
|
||||
_downstreamPath = new OkResponse<DownstreamPath>(new DownstreamPath(downstreamUrl));
|
||||
_downstreamUrlTemplateVariableReplacer
|
||||
.Setup(x => x.Replace(It.IsAny<PathTemplate>(), It.IsAny<List<UrlPathPlaceholderNameAndValue>>()))
|
||||
.Returns(_downstreamPath);
|
||||
}
|
||||
|
||||
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
||||
{
|
||||
_scopedRepository
|
||||
.Verify(x => x.Add("DownstreamUrl", _downstreamUrl.Data.Value), Times.Once());
|
||||
}
|
||||
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
_result = _client.GetAsync(_url).Result;
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
@ -131,6 +96,29 @@ namespace Ocelot.UnitTests.DownstreamUrlCreator
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamRequestUriIs(string uri)
|
||||
{
|
||||
_downstreamRequest.RequestUri = new Uri(uri);
|
||||
}
|
||||
|
||||
private void GivenTheUrlReplacerWillReturn(string path)
|
||||
{
|
||||
_downstreamPath = new OkResponse<DownstreamPath>(new DownstreamPath(path));
|
||||
_downstreamUrlTemplateVariableReplacer
|
||||
.Setup(x => x.Replace(It.IsAny<PathTemplate>(), It.IsAny<List<UrlPathPlaceholderNameAndValue>>()))
|
||||
.Returns(_downstreamPath);
|
||||
}
|
||||
|
||||
private void WhenICallTheMiddleware()
|
||||
{
|
||||
_result = _client.GetAsync(_url).Result;
|
||||
}
|
||||
|
||||
private void ThenTheDownstreamRequestUriIs(string expectedUri)
|
||||
{
|
||||
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_client.Dispose();
|
||||
|
@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.DownstreamUrlCreator;
|
||||
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
|
@ -12,6 +12,7 @@ using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
@ -19,6 +20,8 @@ namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
private readonly AddHeadersToRequest _addHeadersToRequest;
|
||||
private readonly Mock<IClaimsParser> _parser;
|
||||
private readonly HttpRequestMessage _downstreamRequest;
|
||||
private List<Claim> _claims;
|
||||
private List<ClaimToThing> _configuration;
|
||||
private HttpContext _context;
|
||||
private Response _result;
|
||||
@ -28,17 +31,15 @@ namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_addHeadersToRequest = new AddHeadersToRequest(_parser.Object);
|
||||
_downstreamRequest = new HttpRequestMessage();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_headers_to_context()
|
||||
public void should_add_headers_to_downstreamRequest()
|
||||
{
|
||||
var context = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
}))
|
||||
};
|
||||
|
||||
this.Given(
|
||||
@ -46,7 +47,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
new ClaimToThing("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.Given(x => x.GivenClaims(claims))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
@ -55,25 +56,19 @@ namespace Ocelot.UnitTests.Headers
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void if_header_exists_should_replace_it()
|
||||
public void should_replace_existing_headers_on_request()
|
||||
{
|
||||
var context = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
})),
|
||||
};
|
||||
|
||||
context.Request.Headers.Add("header-key", new StringValues("initial"));
|
||||
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.Given(x => x.GivenClaims(new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
}))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.And(x => x.GivenThatTheRequestContainsHeader("header-key", "initial"))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.And(x => x.ThenTheHeaderIsAdded())
|
||||
@ -88,7 +83,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
new ClaimToThing("", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
|
||||
.Given(x => x.GivenClaims(new List<Claim>()))
|
||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
@ -98,10 +93,9 @@ namespace Ocelot.UnitTests.Headers
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheHeaderIsAdded()
|
||||
private void GivenClaims(List<Claim> claims)
|
||||
{
|
||||
var header = _context.Request.Headers.First(x => x.Key == "header-key");
|
||||
header.Value.First().ShouldBe(_claimValue.Data);
|
||||
_claims = claims;
|
||||
}
|
||||
|
||||
private void GivenConfigurationHeaderExtractorProperties(List<ClaimToThing> configuration)
|
||||
@ -109,9 +103,9 @@ namespace Ocelot.UnitTests.Headers
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
private void GivenHttpContext(HttpContext context)
|
||||
private void GivenThatTheRequestContainsHeader(string key, string value)
|
||||
{
|
||||
_context = context;
|
||||
_downstreamRequest.Headers.Add(key, value);
|
||||
}
|
||||
|
||||
private void GivenTheClaimParserReturns(Response<string> claimValue)
|
||||
@ -129,9 +123,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
|
||||
private void WhenIAddHeadersToTheRequest()
|
||||
{
|
||||
//_result = _addHeadersToRequest.SetHeadersOnContext(_configuration, _context);
|
||||
//TODO: pass in DownstreamRequest
|
||||
_result = _addHeadersToRequest.SetHeadersOnDownstreamRequest(_configuration, _context.User.Claims, null);
|
||||
_result = _addHeadersToRequest.SetHeadersOnDownstreamRequest(_configuration, _claims, _downstreamRequest);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsSuccess()
|
||||
@ -145,6 +137,12 @@ namespace Ocelot.UnitTests.Headers
|
||||
_result.IsError.ShouldBe(true);
|
||||
}
|
||||
|
||||
private void ThenTheHeaderIsAdded()
|
||||
{
|
||||
var header = _downstreamRequest.Headers.First(x => x.Key == "header-key");
|
||||
header.Value.First().ShouldBe(_claimValue.Data);
|
||||
}
|
||||
|
||||
class AnyError : Error
|
||||
{
|
||||
public AnyError()
|
||||
|
@ -3,16 +3,13 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.DownstreamUrlCreator.Middleware;
|
||||
using Ocelot.Headers;
|
||||
using Ocelot.Headers.Middleware;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
@ -27,6 +24,7 @@ namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
||||
private readonly Mock<IAddHeadersToRequest> _addHeaders;
|
||||
private readonly HttpRequestMessage _downstreamRequest;
|
||||
private readonly string _url;
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
@ -58,6 +56,10 @@ namespace Ocelot.UnitTests.Headers
|
||||
app.UseHttpRequestHeadersBuilderMiddleware();
|
||||
});
|
||||
|
||||
_scopedRepository
|
||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||
|
||||
_server = new TestServer(builder);
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
@ -76,18 +78,14 @@ namespace Ocelot.UnitTests.Headers
|
||||
.Build());
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||
.And(x => x.GivenTheAddHeadersToRequestReturns())
|
||||
.And(x => x.GivenTheAddHeadersToDownstreamRequestReturnsOk())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheAddHeadersToRequestIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheAddHeadersToRequestReturns()
|
||||
private void GivenTheAddHeadersToDownstreamRequestReturnsOk()
|
||||
{
|
||||
//_addHeaders
|
||||
// .Setup(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
// It.IsAny<HttpContext>()))
|
||||
// .Returns(new OkResponse());
|
||||
_addHeaders
|
||||
.Setup(x => x.SetHeadersOnDownstreamRequest(
|
||||
It.IsAny<List<ClaimToThing>>(),
|
||||
@ -98,14 +96,11 @@ namespace Ocelot.UnitTests.Headers
|
||||
|
||||
private void ThenTheAddHeadersToRequestIsCalledCorrectly()
|
||||
{
|
||||
//_addHeaders
|
||||
// .Verify(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
// It.IsAny<HttpContext>()), Times.Once);
|
||||
_addHeaders
|
||||
.Verify(x => x.SetHeadersOnDownstreamRequest(
|
||||
It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<IEnumerable<System.Security.Claims.Claim>>(),
|
||||
It.IsAny<HttpRequestMessage>()), Times.Once);
|
||||
_downstreamRequest), Times.Once);
|
||||
}
|
||||
|
||||
private void WhenICallTheMiddleware()
|
||||
|
@ -25,6 +25,7 @@ namespace Ocelot.UnitTests.Request
|
||||
private readonly Mock<IRequestCreator> _requestBuilder;
|
||||
private readonly Mock<IRequestScopedDataRepository> _scopedRepository;
|
||||
private readonly Mock<IQosProviderHouse> _qosProviderHouse;
|
||||
private readonly HttpRequestMessage _downstreamRequest;
|
||||
private readonly string _url;
|
||||
private readonly TestServer _server;
|
||||
private readonly HttpClient _client;
|
||||
@ -58,6 +59,12 @@ namespace Ocelot.UnitTests.Request
|
||||
app.UseHttpRequestBuilderMiddleware();
|
||||
});
|
||||
|
||||
_downstreamRequest = new HttpRequestMessage();
|
||||
|
||||
_scopedRepository
|
||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||
|
||||
_server = new TestServer(builder);
|
||||
_client = _server.CreateClient();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user