Multipart validation #528

Closed
opened 2025-12-29 15:25:56 +01:00 by adam · 6 comments
Owner

Originally created by @vhmoura on GitHub (Jul 18, 2023).

Hi,

How do we validate a request that contains a json body and multiform data (an image for example)?

I can't find any matcher for it.

Thanks,
V

Originally created by @vhmoura on GitHub (Jul 18, 2023). Hi, How do we validate a request that contains a json body and multiform data (an image for example)? I can't find any matcher for it. Thanks, V
adam added the feature label 2025-12-29 15:25:56 +01:00
adam closed this issue 2025-12-29 15:25:57 +01:00
Author
Owner

@StefH commented on GitHub (Jul 19, 2023):

Currently there is no special logic to parse a multipart and match the parts. I'll need to think on this how to could be added and how this could be used.

@StefH commented on GitHub (Jul 19, 2023): Currently there is no special logic to parse a multipart and match the parts. I'll need to think on this how to could be added and how this could be used.
Author
Owner

@StefH commented on GitHub (Jul 25, 2023):

@vhmoura
I'm working on this, you can try a preview version 1.5.32-ci-17662.

(https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)

Use it like:

            var textPlainContentTypeMatcher = new ContentTypeMatcher("text/plain");
            var textPlainContentMatcher = new ExactMatcher("This is some plain text");
            var textPlainMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textPlainContentTypeMatcher, null, null, textPlainContentMatcher);

            var partTextJsonContentTypeMatcher = new ContentTypeMatcher("text/json");
            var partTextJsonContentMatcher = new JsonMatcher(new { Key = "Value" }, true);
            var partTextMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, partTextJsonContentTypeMatcher, null, null, partTextJsonContentMatcher);

            var imagePngContentTypeMatcher = new ContentTypeMatcher("image/png");
            var imagePngContentDispositionMatcher = new ExactMatcher("attachment; filename=\"image.png\"");
            var imagePngContentTransferEncodingMatcher = new ExactMatcher("base64");
            var imagePngContentMatcher = new ExactObjectMatcher(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC"));
            var imagePngMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, imagePngContentTypeMatcher, imagePngContentDispositionMatcher, imagePngContentTransferEncodingMatcher, imagePngContentMatcher);

            var matchers = new IMatcher[]
            {
                textPlainMatcher,
                partTextMatcher,
                imagePngMatcher
            };

            server
                .Given(Request.Create()
                    .WithPath("/multipart")
                    .UsingPost()
                    .WithMultiPart(matchers)
                )
                .WithGuid("b9c82182-e469-41da-bcaf-b6e3157fefdb")
                .RespondWith(Response.Create()
                    .WithBody("MultiPart is ok")
                );

Or in JSON:

{
    "Guid": "b9c82182-e469-41da-bcaf-b6e3157fefdb",
    "UpdatedAt": "2023-07-24T18:12:55.564978Z",
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/multipart",
                    "IgnoreCase": false
                }
            ]
        },
        "Methods": [
            "POST"
        ],
        "Body": {
            "Matchers": [
                {
                    "ContentTypeMatcher": {
                        "Name": "ContentTypeMatcher",
                        "Pattern": "text/plain",
                        "IgnoreCase": false
                    },
                    "ContentMatcher": {
                        "Name": "ExactMatcher",
                        "Pattern": "This is some plain text",
                        "IgnoreCase": false
                    },
                    "Name": "MimePartMatcher"
                },
                {
                    "ContentTypeMatcher": {
                        "Name": "ContentTypeMatcher",
                        "Pattern": "text/json",
                        "IgnoreCase": false
                    },
                    "ContentMatcher": {
                        "Name": "JsonMatcher",
                        "Pattern": {
                            "Key": "Value"
                        },
                        "IgnoreCase": true
                    },
                    "Name": "MimePartMatcher"
                },
                {
                    "ContentTypeMatcher": {
                        "Name": "ContentTypeMatcher",
                        "Pattern": "image/png",
                        "IgnoreCase": true
                    },
                    "ContentDispositionMatcher": {
                        "Name": "ExactMatcher",
                        "Pattern": "attachment; filename=\"image.png\"",
                        "IgnoreCase": true
                    },
                    "ContentTransferEncodingMatcher": {
                        "Name": "ExactMatcher",
                        "Pattern": "base64",
                        "IgnoreCase": true
                    },
                    "ContentMatcher": {
                        "Name": "ExactObjectMatcher",
                        "Pattern": "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC"
                    },
                    "Name": "MimePartMatcher"
                }
            ],
            "MatchOperator": "Or"
        }
    },
    "Response": {
        "BodyDestination": "SameAsSource",
        "Body": "MultiPart is ok"
    }
}
@StefH commented on GitHub (Jul 25, 2023): @vhmoura I'm working on this, you can try a preview version `1.5.32-ci-17662`. (https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions) Use it like: ``` c# var textPlainContentTypeMatcher = new ContentTypeMatcher("text/plain"); var textPlainContentMatcher = new ExactMatcher("This is some plain text"); var textPlainMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textPlainContentTypeMatcher, null, null, textPlainContentMatcher); var partTextJsonContentTypeMatcher = new ContentTypeMatcher("text/json"); var partTextJsonContentMatcher = new JsonMatcher(new { Key = "Value" }, true); var partTextMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, partTextJsonContentTypeMatcher, null, null, partTextJsonContentMatcher); var imagePngContentTypeMatcher = new ContentTypeMatcher("image/png"); var imagePngContentDispositionMatcher = new ExactMatcher("attachment; filename=\"image.png\""); var imagePngContentTransferEncodingMatcher = new ExactMatcher("base64"); var imagePngContentMatcher = new ExactObjectMatcher(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC")); var imagePngMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, imagePngContentTypeMatcher, imagePngContentDispositionMatcher, imagePngContentTransferEncodingMatcher, imagePngContentMatcher); var matchers = new IMatcher[] { textPlainMatcher, partTextMatcher, imagePngMatcher }; server .Given(Request.Create() .WithPath("/multipart") .UsingPost() .WithMultiPart(matchers) ) .WithGuid("b9c82182-e469-41da-bcaf-b6e3157fefdb") .RespondWith(Response.Create() .WithBody("MultiPart is ok") ); ``` Or in JSON: ``` json { "Guid": "b9c82182-e469-41da-bcaf-b6e3157fefdb", "UpdatedAt": "2023-07-24T18:12:55.564978Z", "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/multipart", "IgnoreCase": false } ] }, "Methods": [ "POST" ], "Body": { "Matchers": [ { "ContentTypeMatcher": { "Name": "ContentTypeMatcher", "Pattern": "text/plain", "IgnoreCase": false }, "ContentMatcher": { "Name": "ExactMatcher", "Pattern": "This is some plain text", "IgnoreCase": false }, "Name": "MimePartMatcher" }, { "ContentTypeMatcher": { "Name": "ContentTypeMatcher", "Pattern": "text/json", "IgnoreCase": false }, "ContentMatcher": { "Name": "JsonMatcher", "Pattern": { "Key": "Value" }, "IgnoreCase": true }, "Name": "MimePartMatcher" }, { "ContentTypeMatcher": { "Name": "ContentTypeMatcher", "Pattern": "image/png", "IgnoreCase": true }, "ContentDispositionMatcher": { "Name": "ExactMatcher", "Pattern": "attachment; filename=\"image.png\"", "IgnoreCase": true }, "ContentTransferEncodingMatcher": { "Name": "ExactMatcher", "Pattern": "base64", "IgnoreCase": true }, "ContentMatcher": { "Name": "ExactObjectMatcher", "Pattern": "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC" }, "Name": "MimePartMatcher" } ], "MatchOperator": "Or" } }, "Response": { "BodyDestination": "SameAsSource", "Body": "MultiPart is ok" } } ```
Author
Owner

@StefH commented on GitHub (Jul 31, 2023):

@vhmoura
Did you have time to check that specific preview version?

@StefH commented on GitHub (Jul 31, 2023): @vhmoura Did you have time to check that specific preview version?
Author
Owner

@StefH commented on GitHub (Aug 3, 2023):

@vhmoura
Did you have time to check that specific preview version?

It would really help if you can try this version before I merge to this main and create a new official version.

@StefH commented on GitHub (Aug 3, 2023): @vhmoura Did you have time to check that specific preview version? It would really help if you can try this version before I merge to this main and create a new official version.
Author
Owner

@vhmoura commented on GitHub (Aug 3, 2023):

Hi. I didn’t. Sorry I was quite busy making loads of tests but based on what you are showing looks ok to me.

@vhmoura commented on GitHub (Aug 3, 2023): Hi. I didn’t. Sorry I was quite busy making loads of tests but based on what you are showing looks ok to me.
Author
Owner

@StefH commented on GitHub (Aug 3, 2023):

https://github.com/WireMock-Net/WireMock.Net/pull/981

@StefH commented on GitHub (Aug 3, 2023): https://github.com/WireMock-Net/WireMock.Net/pull/981
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#528