diff --git a/src/WireMock.Net/Http/ByteArrayContentHelper.cs b/src/WireMock.Net/Http/ByteArrayContentHelper.cs new file mode 100644 index 00000000..44d15bde --- /dev/null +++ b/src/WireMock.Net/Http/ByteArrayContentHelper.cs @@ -0,0 +1,30 @@ +using System.Net.Http; +using System.Net.Http.Headers; +using JetBrains.Annotations; +using WireMock.Validation; + +namespace WireMock.Http +{ + internal static class ByteArrayContentHelper + { + /// + /// Creates a ByteArrayContent object. + /// + /// The byte[] content (cannot be null) + /// The ContentType (can be null) + /// ByteArrayContent + internal static ByteArrayContent Create([NotNull] byte[] content, [CanBeNull] MediaTypeHeaderValue contentType) + { + Check.NotNull(content, nameof(content)); + + var byteContent = new ByteArrayContent(content); + if (contentType != null) + { + byteContent.Headers.Remove(HttpKnownHeaderNames.ContentType); + byteContent.Headers.ContentType = contentType; + } + + return byteContent; + } + } +} diff --git a/src/WireMock.Net/Http/HttpRequestMessageHelper.cs b/src/WireMock.Net/Http/HttpRequestMessageHelper.cs index ba86bead..2f2005d3 100644 --- a/src/WireMock.Net/Http/HttpRequestMessageHelper.cs +++ b/src/WireMock.Net/Http/HttpRequestMessageHelper.cs @@ -1,10 +1,10 @@ -using JetBrains.Annotations; -using Newtonsoft.Json; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; +using JetBrains.Annotations; +using Newtonsoft.Json; using WireMock.Types; using WireMock.Validation; @@ -29,7 +29,7 @@ namespace WireMock.Http switch (requestMessage.BodyData?.DetectedBodyType) { case BodyType.Bytes: - httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyData.BodyAsBytes); + httpRequestMessage.Content = ByteArrayContentHelper.Create(requestMessage.BodyData.BodyAsBytes, contentType); break; case BodyType.Json: diff --git a/src/WireMock.Net/Owin/Mappers/OwinRequestMapper.cs b/src/WireMock.Net/Owin/Mappers/OwinRequestMapper.cs index 8871cde9..49f8b538 100644 --- a/src/WireMock.Net/Owin/Mappers/OwinRequestMapper.cs +++ b/src/WireMock.Net/Owin/Mappers/OwinRequestMapper.cs @@ -64,6 +64,7 @@ namespace WireMock.Owin.Mappers ContentEncoding = contentEncodingHeader?.FirstOrDefault(), DecompressGZipAndDeflate = !options.DisableRequestBodyDecompressing.GetValueOrDefault(false) }; + body = await BodyParser.Parse(bodyParserSettings); } diff --git a/src/WireMock.Net/Util/BodyParser.cs b/src/WireMock.Net/Util/BodyParser.cs index 826b399e..cd91c9c9 100644 --- a/src/WireMock.Net/Util/BodyParser.cs +++ b/src/WireMock.Net/Util/BodyParser.cs @@ -130,7 +130,7 @@ namespace WireMock.Util data.Encoding = encoding; data.DetectedBodyType = BodyType.String; } - + return data; } diff --git a/test/WireMock.Net.Tests/Http/ByteArrayContentHelperTests.cs b/test/WireMock.Net.Tests/Http/ByteArrayContentHelperTests.cs new file mode 100644 index 00000000..2bb6169c --- /dev/null +++ b/test/WireMock.Net.Tests/Http/ByteArrayContentHelperTests.cs @@ -0,0 +1,44 @@ +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using FluentAssertions; +using WireMock.Http; +using Xunit; + +namespace WireMock.Net.Tests.Http +{ + public class ByteArrayContentHelperTests + { + [Fact] + public async Task ByteArrayContentHelperTests_Create_WithNullContentType() + { + // Arrange + var content = Encoding.UTF8.GetBytes("test"); + + // Act + var result = ByteArrayContentHelper.Create(content, null); + + // Assert + result.Headers.ContentType.Should().BeNull(); + (await result.ReadAsByteArrayAsync()).Should().BeEquivalentTo(content); + } + + [Theory] + [InlineData("application/octet-stream", "application/octet-stream")] + [InlineData("application/soap+xml", "application/soap+xml")] + [InlineData("multipart/form-data; boundary=------------------------x", "multipart/form-data; boundary=------------------------x")] + public async Task ByteArrayContentHelperTests_Create(string test, string expected) + { + // Arrange + var content = Encoding.UTF8.GetBytes("test"); + var contentType = MediaTypeHeaderValue.Parse(test); + + // Act + var result = ByteArrayContentHelper.Create(content, contentType); + + // Assert + result.Headers.ContentType.ToString().Should().Be(expected); + (await result.ReadAsByteArrayAsync()).Should().BeEquivalentTo(content); + } + } +}