Skip to content

Commit

Permalink
Change to fix issue not setting content type header
Browse files Browse the repository at this point in the history
  • Loading branch information
tom.pallister committed Apr 28, 2017
1 parent 1a76c9f commit be3a2fb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Ocelot/Request/Mapper/RequestMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -44,7 +45,11 @@ private async Task<HttpContent> MapContent(HttpRequest request)
return null;
}

return new ByteArrayContent(await ToByteArray(request.Body));
var content = new ByteArrayContent(await ToByteArray(request.Body));

content.Headers.TryAddWithoutValidation("Content-Type", new[] {request.ContentType});

return content;
}

private HttpMethod MapMethod(HttpRequest request)
Expand Down
29 changes: 29 additions & 0 deletions test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,35 @@ public void Should_map_content()
.BDDfy();
}

[Fact]
public void Should_map_content_type_header()
{
this.Given(_ => GivenTheInputRequestHasContent("This is my content"))
.And(_ => GivenTheContentTypeIs("application/json"))
.And(_ => GivenTheInputRequestHasMethod("GET"))
.And(_ => GivenTheInputRequestHasAValidUri())
.When(_ => WhenMapped())
.Then(_ => ThenNoErrorIsReturned())
.And(_ => ThenTheMappedRequestHasContentTypeHeader("application/json"))
.And(_ => ThenTheMappedRequestHasContentSize("This is my content".Length))
.BDDfy();
}

private void GivenTheContentTypeIs(string contentType)
{
_inputRequest.ContentType = contentType;
}

private void ThenTheMappedRequestHasContentTypeHeader(string expected)
{
_mappedRequest.Data.Content.Headers.ContentType.MediaType.ShouldBe(expected);
}

private void ThenTheMappedRequestHasContentSize(long expected)
{
_mappedRequest.Data.Content.Headers.ContentLength.ShouldBe(expected);
}

[Fact]
public void Should_handle_no_content()
{
Expand Down

0 comments on commit be3a2fb

Please sign in to comment.