moved logic to request mapper and public setter gone

This commit is contained in:
TomPallister 2020-02-09 15:51:22 +00:00
parent 6bd903bc87
commit 0c5e09d18f
6 changed files with 56 additions and 37 deletions

View File

@ -1,12 +1,13 @@
namespace Ocelot.Request.Mapper namespace Ocelot.Request.Mapper
{ {
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration;
using Ocelot.Responses; using Ocelot.Responses;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
public interface IRequestMapper public interface IRequestMapper
{ {
Task<Response<HttpRequestMessage>> Map(HttpRequest request); Task<Response<HttpRequestMessage>> Map(HttpRequest request, DownstreamReRoute downstreamReRoute);
} }
} }

View File

@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
using Ocelot.Configuration;
using Ocelot.Responses; using Ocelot.Responses;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -15,14 +16,14 @@
{ {
private readonly string[] _unsupportedHeaders = { "host" }; private readonly string[] _unsupportedHeaders = { "host" };
public async Task<Response<HttpRequestMessage>> Map(HttpRequest request) public async Task<Response<HttpRequestMessage>> Map(HttpRequest request, DownstreamReRoute downstreamReRoute)
{ {
try try
{ {
var requestMessage = new HttpRequestMessage() var requestMessage = new HttpRequestMessage()
{ {
Content = await MapContent(request), Content = await MapContent(request),
Method = MapMethod(request), Method = MapMethod(request, downstreamReRoute),
RequestUri = MapUri(request) RequestUri = MapUri(request)
}; };
@ -71,8 +72,13 @@
} }
} }
private HttpMethod MapMethod(HttpRequest request) private HttpMethod MapMethod(HttpRequest request, DownstreamReRoute downstreamReRoute)
{ {
if (!string.IsNullOrEmpty(downstreamReRoute?.DownstreamHttpMethod))
{
return new HttpMethod(downstreamReRoute.DownstreamHttpMethod);
}
return new HttpMethod(request.Method); return new HttpMethod(request.Method);
} }

View File

@ -24,7 +24,7 @@ namespace Ocelot.Request.Middleware
public HttpRequestHeaders Headers { get; } public HttpRequestHeaders Headers { get; }
public string Method { get; set; } public string Method { get; }
public string OriginalString { get; } public string OriginalString { get; }

View File

@ -24,7 +24,7 @@ namespace Ocelot.Request.Middleware
public async Task Invoke(DownstreamContext context) public async Task Invoke(DownstreamContext context)
{ {
var downstreamRequest = await _requestMapper.Map(context.HttpContext.Request); var downstreamRequest = await _requestMapper.Map(context.HttpContext.Request, context.DownstreamReRoute);
if (downstreamRequest.IsError) if (downstreamRequest.IsError)
{ {
@ -34,11 +34,6 @@ namespace Ocelot.Request.Middleware
context.DownstreamRequest = _creator.Create(downstreamRequest.Data); context.DownstreamRequest = _creator.Create(downstreamRequest.Data);
if (!string.IsNullOrEmpty(context.DownstreamReRoute?.DownstreamHttpMethod))
{
context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod;
}
await _next.Invoke(context); await _next.Invoke(context);
} }
} }

View File

@ -12,6 +12,7 @@
using Ocelot.Responses; using Ocelot.Responses;
using Shouldly; using Shouldly;
using System.Net.Http; using System.Net.Http;
using Ocelot.Configuration;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
@ -69,20 +70,16 @@
.BDDfy(); .BDDfy();
} }
[Theory] [Fact]
[InlineData("POST", "POST")] public void Should_map_downstream_reroute_method_to_downstream_request()
[InlineData(null, "GET")]
[InlineData("", "GET")]
public void Should_map_downstream_reroute_method_to_downstream_request(string input, string expected)
{ {
this.Given(_ => GivenTheHttpContextContainsARequest()) this.Given(_ => GivenTheHttpContextContainsARequest())
.And(_ => GivenTheDownstreamReRouteMethodIs(input))
.And(_ => GivenTheMapperWillReturnAMappedRequest()) .And(_ => GivenTheMapperWillReturnAMappedRequest())
.When(_ => WhenTheMiddlewareIsInvoked()) .When(_ => WhenTheMiddlewareIsInvoked())
.Then(_ => ThenTheContexRequestIsMappedToADownstreamRequest()) .Then(_ => ThenTheContexRequestIsMappedToADownstreamRequest())
.And(_ => ThenTheDownstreamRequestIsStored()) .And(_ => ThenTheDownstreamRequestIsStored())
.And(_ => ThenTheNextMiddlewareIsInvoked()) .And(_ => ThenTheNextMiddlewareIsInvoked())
.And(_ => ThenTheDownstreamRequestMethodIs(expected)) .And(_ => ThenTheDownstreamRequestMethodIs("GET"))
.BDDfy(); .BDDfy();
} }
@ -98,11 +95,6 @@
.BDDfy(); .BDDfy();
} }
private void GivenTheDownstreamReRouteMethodIs(string input)
{
_downstreamContext.DownstreamReRoute = new DownstreamReRouteBuilder().WithDownStreamHttpMethod(input).Build();
}
private void ThenTheDownstreamRequestMethodIs(string expected) private void ThenTheDownstreamRequestMethodIs(string expected)
{ {
_downstreamContext.DownstreamRequest.Method.ShouldBe(expected); _downstreamContext.DownstreamRequest.Method.ShouldBe(expected);
@ -120,7 +112,7 @@
_mappedRequest = new OkResponse<HttpRequestMessage>(new HttpRequestMessage(HttpMethod.Get, "http://www.bbc.co.uk")); _mappedRequest = new OkResponse<HttpRequestMessage>(new HttpRequestMessage(HttpMethod.Get, "http://www.bbc.co.uk"));
_requestMapper _requestMapper
.Setup(rm => rm.Map(It.IsAny<HttpRequest>())) .Setup(rm => rm.Map(It.IsAny<HttpRequest>(), It.IsAny<DownstreamReRoute>()))
.ReturnsAsync(_mappedRequest); .ReturnsAsync(_mappedRequest);
} }
@ -129,7 +121,7 @@
_mappedRequest = new ErrorResponse<HttpRequestMessage>(new UnmappableRequestError(new System.Exception("boooom!"))); _mappedRequest = new ErrorResponse<HttpRequestMessage>(new UnmappableRequestError(new System.Exception("boooom!")));
_requestMapper _requestMapper
.Setup(rm => rm.Map(It.IsAny<HttpRequest>())) .Setup(rm => rm.Map(It.IsAny<HttpRequest>(), It.IsAny<DownstreamReRoute>()))
.ReturnsAsync(_mappedRequest); .ReturnsAsync(_mappedRequest);
} }
@ -140,7 +132,7 @@
private void ThenTheContexRequestIsMappedToADownstreamRequest() private void ThenTheContexRequestIsMappedToADownstreamRequest()
{ {
_requestMapper.Verify(rm => rm.Map(_httpRequest.Object), Times.Once); _requestMapper.Verify(rm => rm.Map(_httpRequest.Object, _downstreamContext.DownstreamReRoute), Times.Once);
} }
private void ThenTheDownstreamRequestIsStored() private void ThenTheDownstreamRequestIsStored()

View File

@ -13,6 +13,8 @@
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
@ -27,6 +29,8 @@
private List<KeyValuePair<string, StringValues>> _inputHeaders = null; private List<KeyValuePair<string, StringValues>> _inputHeaders = null;
private DownstreamReRoute _downstreamReRoute;
public RequestMapperTests() public RequestMapperTests()
{ {
_httpContext = new DefaultHttpContext(); _httpContext = new DefaultHttpContext();
@ -82,6 +86,21 @@
.BDDfy(); .BDDfy();
} }
[Theory]
[InlineData("", "GET")]
[InlineData(null, "GET")]
[InlineData("POST", "POST")]
public void Should_use_downstream_reroute_method_if_set(string input, string expected)
{
this.Given(_ => GivenTheInputRequestHasMethod("GET"))
.And(_ => GivenTheDownstreamReRouteMethodIs(input))
.And(_ => GivenTheInputRequestHasAValidUri())
.When(_ => WhenMapped())
.Then(_ => ThenNoErrorIsReturned())
.And(_ => ThenTheMappedRequestHasMethod(expected))
.BDDfy();
}
[Fact] [Fact]
public void Should_map_all_headers() public void Should_map_all_headers()
{ {
@ -154,16 +173,6 @@
.BDDfy(); .BDDfy();
} }
private void GivenTheInputRequestHasNoContentLength()
{
_inputRequest.ContentLength = null;
}
private void GivenTheInputRequestHasNoContentType()
{
_inputRequest.ContentType = null;
}
[Fact] [Fact]
public void Should_map_content_headers() public void Should_map_content_headers()
{ {
@ -212,6 +221,22 @@
.BDDfy(); .BDDfy();
} }
private void GivenTheDownstreamReRouteMethodIs(string input)
{
_downstreamReRoute = new DownstreamReRouteBuilder().WithDownStreamHttpMethod(input).Build();
}
private void GivenTheInputRequestHasNoContentLength()
{
_inputRequest.ContentLength = null;
}
private void GivenTheInputRequestHasNoContentType()
{
_inputRequest.ContentType = null;
}
private void ThenTheContentHeadersAreNotAddedToNonContentHeaders() private void ThenTheContentHeadersAreNotAddedToNonContentHeaders()
{ {
_mappedRequest.Data.Headers.ShouldNotContain(x => x.Key == "Content-Disposition"); _mappedRequest.Data.Headers.ShouldNotContain(x => x.Key == "Content-Disposition");
@ -380,7 +405,7 @@
private async Task WhenMapped() private async Task WhenMapped()
{ {
_mappedRequest = await _requestMapper.Map(_inputRequest); _mappedRequest = await _requestMapper.Map(_inputRequest, _downstreamReRoute);
} }
private void ThenNoErrorIsReturned() private void ThenNoErrorIsReturned()