Fixed failing admin requests when content type includes a charset (based on idea from Paul Roub) (#353)

* .

* #350

* fix

* .
This commit is contained in:
Stef Heyenrath
2019-10-05 17:20:10 +02:00
committed by GitHub
parent 0a9214ef47
commit 3cc361e216
6 changed files with 179 additions and 10 deletions

View File

@@ -1,9 +1,10 @@
using NFluent;
using System;
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using NFluent;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
@@ -187,7 +188,7 @@ namespace WireMock.Net.Tests
var server = FluentMockServer.Start();
server
.Given(Request.Create().WithBody((byte[] bodyBytes) => bodyBytes != null))
.Given(Request.Create().WithBody((byte[] bodyBytes) => bodyBytes != null))
.AtPriority(0)
.RespondWith(Response.Create().WithStatusCode(400));
server
@@ -203,7 +204,7 @@ namespace WireMock.Net.Tests
// Assert
Check.That(response.StatusCode).Equals(HttpStatusCode.OK);
}
[Theory]
[InlineData("POST")]
[InlineData("PUT")]
@@ -233,5 +234,38 @@ namespace WireMock.Net.Tests
// Assert
Check.That(response.StatusCode).Equals(HttpStatusCode.OK);
}
[Theory]
[InlineData("application/json")]
[InlineData("application/json; charset=ascii")]
[InlineData("application/json; charset=utf-8")]
[InlineData("application/json; charset=UTF-8")]
public async Task WireMockServer_Should_AcceptPostMappingsWithContentTypeJsonAndAnyCharset(string contentType)
{
// Arrange
string message = @"{
""request"": {
""method"": ""GET"",
""url"": ""/some/thing""
},
""response"": {
""status"": 200,
""body"": ""Hello world!"",
""headers"": {
""Content-Type"": ""text/plain""
}
}
}";
var stringContent = new StringContent(message);
stringContent.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
var server = FluentMockServer.StartWithAdminInterface();
// Act
var response = await new HttpClient().PostAsync($"{server.Urls[0]}/__admin/mappings", stringContent);
// Assert
Check.That(response.StatusCode).Equals(HttpStatusCode.Created);
Check.That(await response.Content.ReadAsStringAsync()).Contains("Mapping added");
}
}
}

View File

@@ -0,0 +1,58 @@
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class ContentTypeMatcherTests
{
[Theory]
[InlineData("application/json")]
[InlineData("application/json; charset=ascii")]
[InlineData("application/json; charset=utf-8")]
[InlineData("application/json; charset=UTF-8")]
public void ContentTypeMatcher_IsMatchWithIgnoreCaseFalse_Positive(string contentType)
{
var matcher = new ContentTypeMatcher("application/json");
Check.That(matcher.IsMatch(contentType)).IsEqualTo(1.0d);
}
[Theory]
[InlineData("application/json")]
[InlineData("application/JSON")]
[InlineData("application/json; CharSet=ascii")]
[InlineData("application/json; charset=utf-8")]
[InlineData("application/json; charset=UTF-8")]
public void ContentTypeMatcher_IsMatchWithIgnoreCaseTrue_Positive(string contentType)
{
var matcher = new ContentTypeMatcher("application/json", true);
Check.That(matcher.IsMatch(contentType)).IsEqualTo(1.0d);
}
[Fact]
public void ContentTypeMatcher_GetName()
{
// Assign
var matcher = new ContentTypeMatcher("x");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("ContentTypeMatcher");
}
[Fact]
public void ContentTypeMatcher_GetPatterns()
{
// Assign
var matcher = new ContentTypeMatcher("x");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("x");
}
}
}