Skip to content

Commit

Permalink
ThreeMammals#490 use byte content not stream (ThreeMammals#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomPallister authored Jul 25, 2018
1 parent 69ca874 commit d186f0e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Ocelot/Request/Mapper/IRequestMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

public interface IRequestMapper
{
Response<HttpRequestMessage> Map(HttpRequest request);
Task<Response<HttpRequestMessage>> Map(HttpRequest request);
}
}
21 changes: 17 additions & 4 deletions src/Ocelot/Request/Mapper/RequestMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class RequestMapper : IRequestMapper
{
private readonly string[] _unsupportedHeaders = { "host" };

public Response<HttpRequestMessage> Map(HttpRequest request)
public async Task<Response<HttpRequestMessage>> Map(HttpRequest request)
{
try
{
var requestMessage = new HttpRequestMessage()
{
Content = MapContent(request),
Content = await MapContent(request),
Method = MapMethod(request),
RequestUri = MapUri(request)
};
Expand All @@ -36,14 +36,15 @@ public Response<HttpRequestMessage> Map(HttpRequest request)
}
}

private HttpContent MapContent(HttpRequest request)
private async Task<HttpContent> MapContent(HttpRequest request)
{
if (request.Body == null || (request.Body.CanSeek && request.Body.Length <= 0))
{
return null;
}

var content = new StreamContent(request.Body);
// Never change this to StreamContent again, I forgot it doesnt work in #464.
var content = new ByteArrayContent(await ToByteArray(request.Body));

if(!string.IsNullOrEmpty(request.ContentType))
{
Expand Down Expand Up @@ -95,5 +96,17 @@ private bool IsSupportedHeader(KeyValuePair<string, StringValues> header)
{
return !_unsupportedHeaders.Contains(header.Key.ToLower());
}

private async Task<byte[]> ToByteArray(Stream stream)
{
using(stream)
{
using (var memStream = new MemoryStream())
{
await stream.CopyToAsync(memStream);
return memStream.ToArray();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public DownstreamRequestInitialiserMiddleware(OcelotRequestDelegate next,

public async Task Invoke(DownstreamContext context)
{
var downstreamRequest = _requestMapper.Map(context.HttpContext.Request);
var downstreamRequest = await _requestMapper.Map(context.HttpContext.Request);

if (downstreamRequest.IsError)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void GivenTheMapperWillReturnAMappedRequest()

_requestMapper
.Setup(rm => rm.Map(It.IsAny<HttpRequest>()))
.Returns(_mappedRequest);
.ReturnsAsync(_mappedRequest);
}

private void GivenTheMapperWillReturnAnError()
Expand All @@ -104,7 +104,7 @@ private void GivenTheMapperWillReturnAnError()

_requestMapper
.Setup(rm => rm.Map(It.IsAny<HttpRequest>()))
.Returns(_mappedRequest);
.ReturnsAsync(_mappedRequest);
}

private void WhenTheMiddlewareIsInvoked()
Expand Down
5 changes: 3 additions & 2 deletions test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Threading.Tasks;

public class RequestMapperTests
{
Expand Down Expand Up @@ -378,9 +379,9 @@ private void GivenTheInputRequestHasNullContent()
_inputRequest.Body = null;
}

private void WhenMapped()
private async Task WhenMapped()
{
_mappedRequest = _requestMapper.Map(_inputRequest);
_mappedRequest = await _requestMapper.Map(_inputRequest);
}

private void ThenNoErrorIsReturned()
Expand Down

0 comments on commit d186f0e

Please sign in to comment.