mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
Feature: support multipart body in IRequestMessage
#590
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @alexeyzimarev on GitHub (Apr 5, 2024).
Originally assigned to: @StefH on GitHub.
Is your feature request related to a problem? Please describe.
I am using WireMock-Net in RestSharp integrated tests. Most of the tests use callbacks as I need to access the request, inspect it, and respond accordingly. I managed to refactor 95% of the tests, except the tests for multipart forms, particularly with files. Unlike URL-encoded forms, multipart forms are parsed into an inaccessible property of BodyData as Mime message, so I can't really inspect the body properly.
Describe the solution you'd like
I previously used a regular
WebApplicationwithIFormFile, and it worked fine. Standard .NET web server allows me to get any field from a multipart form, and get access to files.It would be nice if a multipart body gets parsed to a collection of fields, where a field could be a body object on its own, based on each field content type (JSON, text, binary, etc), and the field name and content disposition, including the file name and encoding.
Describe alternatives you've considered
The only way to do it now is to manually process the binary body data.
Is your feature request supported by WireMock (java version)? Please provide details.
I can't find anything similar in WireMock documentation. I am not sure if WireMock allows mocking responses to multipart form requests and file uploads.
@StefH commented on GitHub (Apr 5, 2024):
@alexeyzimarev
You should be able to use the
BodyAsMimeMessageproperty which is actually aMimeMessagefrom the NuGet MimeKitLite.@alexeyzimarev commented on GitHub (Apr 5, 2024):
It's not accessible for some reason. I checked the code and I see it fenced with pragma, but it is definitely not visible as public for me.
I ended up making a Frankenstein's monster of
HttpMultipartParserand some classes fromMicrosoft.AspNetCore.WebUtilities. The first one doesn't understandfilename*, and the second one is doing funny things with the section stream (it's incomplete).I just want to say that multipart forms are quite common, and aren't directly linked to MIME. Elements of
BodyAsMimeMessagelike recipient and so on are clearly email-oriented.@StefH commented on GitHub (Apr 6, 2024):
What .NET framework do you use?
@alexeyzimarev commented on GitHub (Apr 6, 2024):
I target .NET FW 4.8, .NET 6, 7, and 8. It builds on macOS arm64, Linux & Windows x64
@StefH commented on GitHub (Apr 6, 2024):
@alexeyzimarev
Please see my example here:
https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.Console.NET7.UsingNuGet/Program.cs
This example can access the
BodyAsMimeMessagefrom theIRequestMessagewithout any problems.Can you check if this solution also works for you?
@alexeyzimarev commented on GitHub (Apr 6, 2024):
I will check, but I honestly don't understand what multipart forms have to do with MIME. Yes, one is based on another, but most of the MIME stuff is irrelevant in HTTP server context. Wouldn't it make more sense to use a multipart form parser instead of MimeKit (which, apparently, also creates problems with multi-targeting)?