diff --git a/src/WireMock.Net/Http/ByteArrayContentHelper.cs b/src/WireMock.Net/Http/ByteArrayContentHelper.cs index 339532e1..24f45324 100644 --- a/src/WireMock.Net/Http/ByteArrayContentHelper.cs +++ b/src/WireMock.Net/Http/ByteArrayContentHelper.cs @@ -2,7 +2,6 @@ using System.Net.Http; using System.Net.Http.Headers; -using Stef.Validation; namespace WireMock.Http; @@ -16,8 +15,6 @@ internal static class ByteArrayContentHelper /// ByteArrayContent internal static ByteArrayContent Create(byte[] content, MediaTypeHeaderValue? contentType) { - Guard.NotNull(content); - var byteContent = new ByteArrayContent(content); if (contentType != null) { diff --git a/src/WireMock.Net/Http/HttpRequestMessageHelper.cs b/src/WireMock.Net/Http/HttpRequestMessageHelper.cs index a43a6528..e73e1f4c 100644 --- a/src/WireMock.Net/Http/HttpRequestMessageHelper.cs +++ b/src/WireMock.Net/Http/HttpRequestMessageHelper.cs @@ -37,10 +37,11 @@ internal static class HttpRequestMessageHelper var bodyData = requestMessage.BodyData; httpRequestMessage.Content = bodyData?.GetBodyType() switch { - BodyType.Bytes => ByteArrayContentHelper.Create(bodyData!.BodyAsBytes!, contentType), - BodyType.Json => StringContentHelper.Create(JsonConvert.SerializeObject(bodyData!.BodyAsJson), contentType), - BodyType.String => StringContentHelper.Create(bodyData!.BodyAsString!, contentType), - BodyType.FormUrlEncoded => StringContentHelper.Create(bodyData!.BodyAsString!, contentType), + BodyType.Bytes => ByteArrayContentHelper.Create(bodyData.BodyAsBytes!, contentType), + BodyType.Json => StringContentHelper.Create(JsonConvert.SerializeObject(bodyData.BodyAsJson), contentType), + BodyType.String => StringContentHelper.Create(bodyData.BodyAsString!, contentType), + BodyType.FormUrlEncoded => StringContentHelper.Create(bodyData.BodyAsString!, contentType), + BodyType.MultiPart => StringContentHelper.Create(bodyData.BodyAsString!, contentType), _ => httpRequestMessage.Content }; diff --git a/src/WireMock.Net/Http/StringContentHelper.cs b/src/WireMock.Net/Http/StringContentHelper.cs index 1bc518fa..e6b5918d 100644 --- a/src/WireMock.Net/Http/StringContentHelper.cs +++ b/src/WireMock.Net/Http/StringContentHelper.cs @@ -2,7 +2,6 @@ using System.Net.Http; using System.Net.Http.Headers; -using Stef.Validation; namespace WireMock.Http; @@ -16,8 +15,6 @@ internal static class StringContentHelper /// StringContent internal static StringContent Create(string content, MediaTypeHeaderValue? contentType) { - Guard.NotNull(content); - var stringContent = new StringContent(content); stringContent.Headers.ContentType = contentType; return stringContent; diff --git a/test/WireMock.Net.Tests/Http/HttpRequestMessageHelperTests.cs b/test/WireMock.Net.Tests/Http/HttpRequestMessageHelperTests.cs index edd22b2b..be2c4eb3 100644 --- a/test/WireMock.Net.Tests/Http/HttpRequestMessageHelperTests.cs +++ b/test/WireMock.Net.Tests/Http/HttpRequestMessageHelperTests.cs @@ -21,7 +21,7 @@ public class HttpRequestMessageHelperTests public void HttpRequestMessageHelper_Create() { // Assign - var headers = new Dictionary { { "x", new[] { "value-1" } } }; + var headers = new Dictionary { { "x", ["value-1"] } }; var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, null, headers); // Act @@ -91,7 +91,7 @@ public class HttpRequestMessageHelperTests public async Task HttpRequestMessageHelper_Create_Json_With_ContentType_ApplicationJson() { // Assign - var headers = new Dictionary { { "Content-Type", new[] { "application/json" } } }; + var headers = new Dictionary { { "Content-Type", ["application/json"] } }; var body = new BodyData { BodyAsJson = new { x = 42 }, @@ -111,7 +111,7 @@ public class HttpRequestMessageHelperTests public async Task HttpRequestMessageHelper_Create_Json_With_ContentType_ApplicationJson_UTF8() { // Assign - var headers = new Dictionary { { "Content-Type", new[] { "application/json; charset=utf-8" } } }; + var headers = new Dictionary { { "Content-Type", ["application/json; charset=utf-8"] } }; var body = new BodyData { BodyAsJson = new { x = 42 }, @@ -131,7 +131,7 @@ public class HttpRequestMessageHelperTests public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml() { // Assign - var headers = new Dictionary { { "Content-Type", new[] { "application/xml" } } }; + var headers = new Dictionary { { "Content-Type", ["application/xml"] } }; var body = new BodyData { BodyAsString = "hello", @@ -150,7 +150,7 @@ public class HttpRequestMessageHelperTests public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml_UTF8() { // Assign - var headers = new Dictionary { { "Content-Type", new[] { "application/xml; charset=UTF-8" } } }; + var headers = new Dictionary { { "Content-Type", ["application/xml; charset=UTF-8"] } }; var body = new BodyData { BodyAsString = "hello", @@ -169,7 +169,7 @@ public class HttpRequestMessageHelperTests public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml_ASCII() { // Assign - var headers = new Dictionary { { "Content-Type", new[] { "application/xml; charset=Ascii" } } }; + var headers = new Dictionary { { "Content-Type", ["application/xml; charset=Ascii"] } }; var body = new BodyData { BodyAsString = "hello", @@ -184,6 +184,48 @@ public class HttpRequestMessageHelperTests 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 { { "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 + + Content of a.html. + + -----------------------------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] [InlineData("HEAD", true)] [InlineData("GET", false)] @@ -199,7 +241,7 @@ public class HttpRequestMessageHelperTests // Arrange var key = "Content-Length"; var value = 1234; - var headers = new Dictionary { { key, new[] { "1234" } } }; + var headers = new Dictionary { { key, ["1234"] } }; var request = new RequestMessage(new UrlDetails("http://localhost/foo"), method, ClientIp, null, headers); // Act