WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.35k stars 197 forks source link

Feature: support multipart body in `IRequestMessage` #1090

Closed alexeyzimarev closed 2 months ago

alexeyzimarev commented 2 months ago

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 WebApplication with IFormFile, 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 2 months ago

@alexeyzimarev You should be able to use the BodyAsMimeMessage property which is actually a MimeMessage from the NuGet MimeKitLite.

alexeyzimarev commented 2 months ago

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 HttpMultipartParser and some classes from Microsoft.AspNetCore.WebUtilities. The first one doesn't understand filename*, 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 BodyAsMimeMessage like recipient and so on are clearly email-oriented.

StefH commented 2 months ago

What .NET framework do you use?

alexeyzimarev commented 2 months ago

I target .NET FW 4.8, .NET 6, 7, and 8. It builds on macOS arm64, Linux & Windows x64

StefH commented 2 months ago

@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 BodyAsMimeMessage from the IRequestMessage without any problems.

Can you check if this solution also works for you?

alexeyzimarev commented 2 months ago

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)?