Content-Type multipart/form-data header should also be proxied (#1296)

This commit is contained in:
Stef Heyenrath
2025-05-15 18:21:21 +02:00
committed by GitHub
parent baa33552e9
commit 61b6eb8752
4 changed files with 54 additions and 17 deletions

View File

@@ -2,7 +2,6 @@
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using Stef.Validation;
namespace WireMock.Http; namespace WireMock.Http;
@@ -16,8 +15,6 @@ internal static class ByteArrayContentHelper
/// <returns>ByteArrayContent</returns> /// <returns>ByteArrayContent</returns>
internal static ByteArrayContent Create(byte[] content, MediaTypeHeaderValue? contentType) internal static ByteArrayContent Create(byte[] content, MediaTypeHeaderValue? contentType)
{ {
Guard.NotNull(content);
var byteContent = new ByteArrayContent(content); var byteContent = new ByteArrayContent(content);
if (contentType != null) if (contentType != null)
{ {

View File

@@ -37,10 +37,11 @@ internal static class HttpRequestMessageHelper
var bodyData = requestMessage.BodyData; var bodyData = requestMessage.BodyData;
httpRequestMessage.Content = bodyData?.GetBodyType() switch httpRequestMessage.Content = bodyData?.GetBodyType() switch
{ {
BodyType.Bytes => ByteArrayContentHelper.Create(bodyData!.BodyAsBytes!, contentType), BodyType.Bytes => ByteArrayContentHelper.Create(bodyData.BodyAsBytes!, contentType),
BodyType.Json => StringContentHelper.Create(JsonConvert.SerializeObject(bodyData!.BodyAsJson), contentType), BodyType.Json => StringContentHelper.Create(JsonConvert.SerializeObject(bodyData.BodyAsJson), contentType),
BodyType.String => StringContentHelper.Create(bodyData!.BodyAsString!, contentType), BodyType.String => StringContentHelper.Create(bodyData.BodyAsString!, contentType),
BodyType.FormUrlEncoded => StringContentHelper.Create(bodyData!.BodyAsString!, contentType), BodyType.FormUrlEncoded => StringContentHelper.Create(bodyData.BodyAsString!, contentType),
BodyType.MultiPart => StringContentHelper.Create(bodyData.BodyAsString!, contentType),
_ => httpRequestMessage.Content _ => httpRequestMessage.Content
}; };

View File

@@ -2,7 +2,6 @@
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using Stef.Validation;
namespace WireMock.Http; namespace WireMock.Http;
@@ -16,8 +15,6 @@ internal static class StringContentHelper
/// <returns>StringContent</returns> /// <returns>StringContent</returns>
internal static StringContent Create(string content, MediaTypeHeaderValue? contentType) internal static StringContent Create(string content, MediaTypeHeaderValue? contentType)
{ {
Guard.NotNull(content);
var stringContent = new StringContent(content); var stringContent = new StringContent(content);
stringContent.Headers.ContentType = contentType; stringContent.Headers.ContentType = contentType;
return stringContent; return stringContent;

View File

@@ -21,7 +21,7 @@ public class HttpRequestMessageHelperTests
public void HttpRequestMessageHelper_Create() public void HttpRequestMessageHelper_Create()
{ {
// Assign // Assign
var headers = new Dictionary<string, string[]> { { "x", new[] { "value-1" } } }; var headers = new Dictionary<string, string[]> { { "x", ["value-1"] } };
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, null, headers); var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, null, headers);
// Act // Act
@@ -91,7 +91,7 @@ public class HttpRequestMessageHelperTests
public async Task HttpRequestMessageHelper_Create_Json_With_ContentType_ApplicationJson() public async Task HttpRequestMessageHelper_Create_Json_With_ContentType_ApplicationJson()
{ {
// Assign // Assign
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/json" } } }; var headers = new Dictionary<string, string[]> { { "Content-Type", ["application/json"] } };
var body = new BodyData var body = new BodyData
{ {
BodyAsJson = new { x = 42 }, BodyAsJson = new { x = 42 },
@@ -111,7 +111,7 @@ public class HttpRequestMessageHelperTests
public async Task HttpRequestMessageHelper_Create_Json_With_ContentType_ApplicationJson_UTF8() public async Task HttpRequestMessageHelper_Create_Json_With_ContentType_ApplicationJson_UTF8()
{ {
// Assign // Assign
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/json; charset=utf-8" } } }; var headers = new Dictionary<string, string[]> { { "Content-Type", ["application/json; charset=utf-8"] } };
var body = new BodyData var body = new BodyData
{ {
BodyAsJson = new { x = 42 }, BodyAsJson = new { x = 42 },
@@ -131,7 +131,7 @@ public class HttpRequestMessageHelperTests
public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml() public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml()
{ {
// Assign // Assign
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/xml" } } }; var headers = new Dictionary<string, string[]> { { "Content-Type", ["application/xml"] } };
var body = new BodyData var body = new BodyData
{ {
BodyAsString = "<xml>hello</xml>", BodyAsString = "<xml>hello</xml>",
@@ -150,7 +150,7 @@ public class HttpRequestMessageHelperTests
public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml_UTF8() public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml_UTF8()
{ {
// Assign // Assign
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/xml; charset=UTF-8" } } }; var headers = new Dictionary<string, string[]> { { "Content-Type", ["application/xml; charset=UTF-8"] } };
var body = new BodyData var body = new BodyData
{ {
BodyAsString = "<xml>hello</xml>", BodyAsString = "<xml>hello</xml>",
@@ -169,7 +169,7 @@ public class HttpRequestMessageHelperTests
public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml_ASCII() public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml_ASCII()
{ {
// Assign // Assign
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/xml; charset=Ascii" } } }; var headers = new Dictionary<string, string[]> { { "Content-Type", ["application/xml; charset=Ascii"] } };
var body = new BodyData var body = new BodyData
{ {
BodyAsString = "<xml>hello</xml>", BodyAsString = "<xml>hello</xml>",
@@ -184,6 +184,48 @@ public class HttpRequestMessageHelperTests
Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/xml; charset=Ascii"); Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/xml; charset=Ascii");
} }
[Fact]
public async Task HttpRequestMessageHelper_Create_MultiPart_With_ContentType_MultiPartFormData()
{
// Assign
var contentType = "multipart/form-data";
var headers = new Dictionary<string, string[]> { { "Content-Type", [contentType] } };
var body =
"""
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="text"
text default
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain
Content of a txt
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html
<!DOCTYPE html><title>Content of a.html.</title>
-----------------------------9051914041544843365972754266--
""";
var bodyData = new BodyData
{
BodyAsString = body,
DetectedBodyType = BodyType.String,
DetectedBodyTypeFromContentType = BodyType.MultiPart
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, bodyData, headers);
// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert
Check.That(await message.Content.ReadAsStringAsync().ConfigureAwait(false)).Equals(body);
Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("multipart/form-data");
}
[Theory] [Theory]
[InlineData("HEAD", true)] [InlineData("HEAD", true)]
[InlineData("GET", false)] [InlineData("GET", false)]
@@ -199,7 +241,7 @@ public class HttpRequestMessageHelperTests
// Arrange // Arrange
var key = "Content-Length"; var key = "Content-Length";
var value = 1234; var value = 1234;
var headers = new Dictionary<string, string[]> { { key, new[] { "1234" } } }; var headers = new Dictionary<string, string[]> { { key, ["1234"] } };
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), method, ClientIp, null, headers); var request = new RequestMessage(new UrlDetails("http://localhost/foo"), method, ClientIp, null, headers);
// Act // Act