mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-18 07:00:04 +02:00
Fixed Proxy when using MultipartForm with byte[] (#473)
* wip * ByteArrayContentHelper * ByteArrayContentHelperTests
This commit is contained in:
30
src/WireMock.Net/Http/ByteArrayContentHelper.cs
Normal file
30
src/WireMock.Net/Http/ByteArrayContentHelper.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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:
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
44
test/WireMock.Net.Tests/Http/ByteArrayContentHelperTests.cs
Normal file
44
test/WireMock.Net.Tests/Http/ByteArrayContentHelperTests.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user