Skip to content

Commit

Permalink
Re-attaching content related headers to the request content in a Http…
Browse files Browse the repository at this point in the history
…RequestMessage
  • Loading branch information
neilcampbell committed Sep 10, 2014
1 parent 6ba8e66 commit 3a5921b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,17 @@ public void Convert_WithContentTypeAndCustomHeader_OnlyCustomHeadersIsAddedToHtt
}

[Fact]
public void Convert_WithContentLengthHeader_ContentLengthHeaderIsNotAdded()
public void Convert_WithContentLengthHeader_ContentLengthHeaderIsNotAddedToHttpRequestMessage()
{
var request = new ProviderServiceRequest
{
Method = HttpVerb.Post,
Path = "/events",
Headers = new Dictionary<string, string>
{
{ "Content-Length", "100" }
}
{ "Content-Length", "12" }
},
Body = "Some content"
};

var mapper = GetSubject();
Expand All @@ -241,6 +242,92 @@ public void Convert_WithContentLengthHeader_ContentLengthHeaderIsNotAdded()
Assert.Equal(0, result.Headers.Count());
}

[Fact]
public void Convert_WithContentLengthHeader_ContentLengthHeaderIsAddedToHttpRequestMessageContentHeaders()
{
var request = new ProviderServiceRequest
{
Method = HttpVerb.Post,
Path = "/events",
Headers = new Dictionary<string, string>
{
{ "Content-Length", "12" }
},
Body = "Some content"
};
var httpBodyContent = new HttpBodyContent(request.Body, "text/plain", Encoding.UTF8);
var stringContent = new StringContent(request.Body, Encoding.UTF8, "text/plain");

var mapper = GetSubject();

_mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
_mockHttpBodyContentMapper.Convert(Arg.Any<string>(), Arg.Any<IDictionary<string, string>>()).Returns(httpBodyContent);
_mockHttpContentMapper.Convert(httpBodyContent).Returns(stringContent);

var result = mapper.Convert(request);

Assert.Equal(request.Headers.Last().Key, result.Content.Headers.Last().Key);
Assert.Equal(request.Headers.Last().Value, result.Content.Headers.Last().Value.First());
}

[Fact]
public void Convert_WithContentTypeSpecifiedAndAlsoBeingSetByStringContent_ContentTypeHeaderIsNotReAddedToHttpRequestMessageContentHeaders()
{
var request = new ProviderServiceRequest
{
Method = HttpVerb.Post,
Path = "/events",
Headers = new Dictionary<string, string>
{
{ "Content-Type", "text/plain" }
},
Body = "Some content"
};
var httpBodyContent = new HttpBodyContent(request.Body, "text/plain", Encoding.UTF8);
var stringContent = new StringContent(request.Body, Encoding.UTF8, "text/plain");

var mapper = GetSubject();

_mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
_mockHttpBodyContentMapper.Convert(Arg.Any<string>(), Arg.Any<IDictionary<string, string>>()).Returns(httpBodyContent);
_mockHttpContentMapper.Convert(httpBodyContent).Returns(stringContent);

var result = mapper.Convert(request);

Assert.Equal(1, result.Content.Headers.Count());
Assert.Equal(request.Headers.First().Key, result.Content.Headers.First().Key);
Assert.Equal("text/plain; charset=utf-8", result.Content.Headers.First().Value.First());
}

[Fact]
public void Convert_WithContentTypeSpecifiedButNotBeingSetByByteArrayContent_ContentTypeHeaderIsNotReAddedToHttpRequestMessageContentHeaders()
{
var request = new ProviderServiceRequest
{
Method = HttpVerb.Post,
Path = "/events",
Headers = new Dictionary<string, string>
{
{ "Content-Type", "application/octet-stream" }
},
Body = Encoding.UTF8.GetBytes("Some content")
};
var httpBodyContent = new HttpBodyContent(request.Body, "text/plain", Encoding.UTF8);
var byteArrayContent = new ByteArrayContent(request.Body as byte[]);

var mapper = GetSubject();

_mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
_mockHttpBodyContentMapper.Convert(Arg.Any<object>(), Arg.Any<IDictionary<string, string>>()).Returns(httpBodyContent);
_mockHttpContentMapper.Convert(httpBodyContent).Returns(byteArrayContent);

var result = mapper.Convert(request);

Assert.Equal(1, result.Content.Headers.Count());
Assert.Equal(request.Headers.First().Key, result.Content.Headers.First().Key);
Assert.Equal("application/octet-stream", result.Content.Headers.First().Value.First());
}

[Fact]
public void Convert_WithBody_HttpContentMapperIsCalled()
{
Expand Down Expand Up @@ -279,7 +366,7 @@ public void Convert_WithTheWorks_CorrectlyMappedHttpRequestMessageIsReturned()
{
{ "Content-Type", contentTypeString + "; charset=" + encodingString },
{ "X-Custom", "My Custom header" },
{ "Content-Length", "10000" }, //This header is removed and replace with the correct value of 29
{ "Content-Length", "1000" }
},
Body = new
{
Expand Down Expand Up @@ -314,7 +401,7 @@ public void Convert_WithTheWorks_CorrectlyMappedHttpRequestMessageIsReturned()

//Content-Length header
Assert.Equal(request.Headers.Last().Key, contentLengthHeader.Key);
Assert.Equal("29", contentLengthHeader.Value.First());
Assert.Equal(request.Headers.Last().Value, contentLengthHeader.Value.First());
}
}
}
2 changes: 1 addition & 1 deletion PactNet/Mocks/MockHttpService/HttpClientRequestSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ProviderServiceResponse Send(ProviderServiceRequest request)

if (httpRequest != null)
{
if (httpResponse.Content != null)
if (httpRequest.Content != null)
{
httpRequest.Content.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using PactNet.Mocks.MockHttpService.Models;
Expand Down Expand Up @@ -40,13 +41,15 @@ public HttpRequestMessage Convert(ProviderServiceRequest from)

var to = new HttpRequestMessage(requestHttpMethod, requestPath);

var contentRelatedHeaders = new Dictionary<string, string>();
if (from.Headers != null && from.Headers.Any())
{
foreach (var requestHeader in from.Headers)
{
//Strip any Content- headers as they need to be attached to Request content when using a HttpRequestMessage
if (requestHeader.Key.IndexOf("Content-", StringComparison.InvariantCultureIgnoreCase) == 0)
{
contentRelatedHeaders.Add(requestHeader.Key, requestHeader.Value);
continue;
}

Expand All @@ -57,7 +60,24 @@ public HttpRequestMessage Convert(ProviderServiceRequest from)
if (from.Body != null)
{
HttpBodyContent bodyContent = _httpBodyContentMapper.Convert(from.Body, from.Headers);
to.Content = _httpContentMapper.Convert(bodyContent);
var httpContent = _httpContentMapper.Convert(bodyContent);

//Set the content related headers
if (httpContent != null && contentRelatedHeaders.Any())
{
foreach (var contentHeader in contentRelatedHeaders)
{
if (contentHeader.Key.Equals("Content-Type", StringComparison.InvariantCultureIgnoreCase) &&
httpContent.Headers.Any(x => x.Key.Equals("Content-Type", StringComparison.InvariantCultureIgnoreCase)))
{
continue;
}

httpContent.Headers.Add(contentHeader.Key, contentHeader.Value);
}
}

to.Content = httpContent;
}

return to;
Expand Down

0 comments on commit 3a5921b

Please sign in to comment.