Fixed Proxy when using MultipartForm with byte[] (#473)

* wip

* ByteArrayContentHelper

* ByteArrayContentHelperTests
This commit is contained in:
Stef Heyenrath
2020-05-23 16:48:25 +02:00
committed by GitHub
parent d9e3f38fee
commit 1b1ddeab83
5 changed files with 80 additions and 5 deletions

View File

@@ -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
{
/// <summary>
/// Creates a ByteArrayContent object.
/// </summary>
/// <param name="content">The byte[] content (cannot be null)</param>
/// <param name="contentType">The ContentType (can be null)</param>
/// <returns>ByteArrayContent</returns>
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;
}
}
}

View File

@@ -1,10 +1,10 @@
using JetBrains.Annotations; using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using JetBrains.Annotations;
using Newtonsoft.Json;
using WireMock.Types; using WireMock.Types;
using WireMock.Validation; using WireMock.Validation;
@@ -29,7 +29,7 @@ namespace WireMock.Http
switch (requestMessage.BodyData?.DetectedBodyType) switch (requestMessage.BodyData?.DetectedBodyType)
{ {
case BodyType.Bytes: case BodyType.Bytes:
httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyData.BodyAsBytes); httpRequestMessage.Content = ByteArrayContentHelper.Create(requestMessage.BodyData.BodyAsBytes, contentType);
break; break;
case BodyType.Json: case BodyType.Json:

View File

@@ -64,6 +64,7 @@ namespace WireMock.Owin.Mappers
ContentEncoding = contentEncodingHeader?.FirstOrDefault(), ContentEncoding = contentEncodingHeader?.FirstOrDefault(),
DecompressGZipAndDeflate = !options.DisableRequestBodyDecompressing.GetValueOrDefault(false) DecompressGZipAndDeflate = !options.DisableRequestBodyDecompressing.GetValueOrDefault(false)
}; };
body = await BodyParser.Parse(bodyParserSettings); body = await BodyParser.Parse(bodyParserSettings);
} }

View File

@@ -130,7 +130,7 @@ namespace WireMock.Util
data.Encoding = encoding; data.Encoding = encoding;
data.DetectedBodyType = BodyType.String; data.DetectedBodyType = BodyType.String;
} }
return data; return data;
} }

View File

@@ -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);
}
}
}