Skip to content

Commit

Permalink
Fixing the dropping of Content-Type header parameters when verifying …
Browse files Browse the repository at this point in the history
…pact
  • Loading branch information
Grant Pedersen committed May 2, 2016
1 parent f27377e commit f2c6a22
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using PactNet.Mocks.MockHttpService.Mappers;
using PactNet.Mocks.MockHttpService.Models;
Expand Down Expand Up @@ -33,5 +35,21 @@ public void Convert_WithEmptyContent_ReturnsNull()

Assert.Empty(result.ReadAsStringAsync().Result);
}

[Fact]
public void Convert_WithContentTypeContainingParameter_ReturnsContentWithContentTypeHeader()
{
const string contentType = "text/plain";
const string content = "test";
var httpBodyContent = new HttpBodyContent(content: Encoding.UTF8.GetBytes(content), contentType: contentType + "; version=1", encoding: Encoding.UTF8);
IHttpContentMapper mapper = GetSubject();

HttpContent result = mapper.Convert(httpBodyContent);

Assert.Equal(contentType, result.Headers.ContentType.MediaType);
Assert.Contains(new NameValueHeaderValue("version", "1"), result.Headers.ContentType.Parameters);
Assert.Equal("utf-8", result.Headers.ContentType.CharSet);
Assert.Equal(content, result.ReadAsStringAsync().Result);
}
}
}
}
8 changes: 7 additions & 1 deletion PactNet/Mocks/MockHttpService/Mappers/HttpContentMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http;
using System.Net.Http.Headers;
using PactNet.Mocks.MockHttpService.Models;

namespace PactNet.Mocks.MockHttpService.Mappers
Expand All @@ -12,7 +13,12 @@ public HttpContent Convert(HttpBodyContent from)
return null;
}

return new StringContent(from.Content, from.Encoding, from.ContentType);
var stringContent = new StringContent(from.Content, from.Encoding);

stringContent.Headers.ContentType = MediaTypeHeaderValue.Parse(from.ContentType);
stringContent.Headers.ContentType.CharSet = from.Encoding.WebName;

return stringContent;
}
}
}

0 comments on commit f2c6a22

Please sign in to comment.