mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
fix contentTypeHeader
This commit is contained in:
@@ -71,13 +71,6 @@ namespace WireMock.Http
|
||||
|
||||
var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url);
|
||||
|
||||
WireMockList<string> contentTypeHeader = null;
|
||||
bool contentTypeHeaderPresent = requestMessage.Headers.Any(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase));
|
||||
if (contentTypeHeaderPresent)
|
||||
{
|
||||
contentTypeHeader = requestMessage.Headers[HttpKnownHeaderNames.ContentType];
|
||||
}
|
||||
|
||||
// Set Body if present
|
||||
if (requestMessage.BodyAsBytes != null)
|
||||
{
|
||||
@@ -119,6 +112,12 @@ namespace WireMock.Http
|
||||
if (httpResponseMessage.Content != null)
|
||||
{
|
||||
var stream = await httpResponseMessage.Content.ReadAsStreamAsync();
|
||||
IEnumerable<string> contentTypeHeader = null;
|
||||
if (headers.Any(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
contentTypeHeader = headers.First(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)).Value;
|
||||
}
|
||||
|
||||
var body = await BodyParser.Parse(stream, contentTypeHeader?.FirstOrDefault());
|
||||
responseMessage.Body = body.BodyAsString;
|
||||
responseMessage.BodyAsJson = body.BodyAsJson;
|
||||
|
||||
@@ -69,8 +69,9 @@ namespace WireMock.ResponseBuilders
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates this instance.
|
||||
/// Creates this instance with the specified function.
|
||||
/// </summary>
|
||||
/// <param name="func">The callback function.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static IResponseBuilder Create([NotNull] Func<ResponseMessage> func)
|
||||
|
||||
@@ -230,9 +230,10 @@ namespace WireMock.Server
|
||||
requestMessage.Query.Loop((key, value) => request.WithParam(key, value.ToArray()));
|
||||
requestMessage.Cookies.Loop((key, value) => request.WithCookie(key, value));
|
||||
|
||||
var allBlackListedHeaders = new List<string>(blacklistedHeaders) { "Cookie" };
|
||||
requestMessage.Headers.Loop((key, value) =>
|
||||
{
|
||||
if (!blacklistedHeaders.Any(b => string.Equals(key, b, StringComparison.OrdinalIgnoreCase)))
|
||||
if (!allBlackListedHeaders.Any(b => string.Equals(key, b, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
request.WithHeader(key, value.ToArray());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.ResponseBuilders;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.ResponseBuilderTests
|
||||
{
|
||||
public class ResponseCreateTests
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
|
||||
[Fact]
|
||||
public async Task Response_Create()
|
||||
{
|
||||
// Assign
|
||||
var responseMessage = new ResponseMessage { StatusCode = 500 };
|
||||
var request = new RequestMessage(new Uri("http://localhost"), "GET", ClientIp);
|
||||
|
||||
var response = Response.Create(() => responseMessage);
|
||||
|
||||
// Act
|
||||
var providedResponse = await response.ProvideResponseAsync(request);
|
||||
|
||||
// Assert
|
||||
Check.That(providedResponse).Equals(responseMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.ResponseBuilderTests
|
||||
|
||||
27
test/WireMock.Net.Tests/Util/BodyParserTests.cs
Normal file
27
test/WireMock.Net.Tests/Util/BodyParserTests.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.Util
|
||||
{
|
||||
public class BodyParserTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task BodyParser_Parse_ApplicationXml()
|
||||
{
|
||||
// Assign
|
||||
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("<xml>hello</xml>"));
|
||||
|
||||
// Act
|
||||
var body = await BodyParser.Parse(memoryStream, "application/xml; charset=UTF-8");
|
||||
|
||||
// Assert
|
||||
Check.That(body.BodyAsBytes).IsNull();
|
||||
Check.That(body.BodyAsJson).IsNull();
|
||||
Check.That(body.BodyAsString).Equals("<xml>hello</xml>");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user