Improve matcher for BodyType.MultiPart #208

Closed
opened 2025-12-29 14:25:10 +01:00 by adam · 1 comment
Owner

Originally created by @davidshen84 on GitHub (Sep 6, 2019).

ref: 2f406029c9/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs (L130)

Based on the above code, a body which is detected as BodyType.MultiPart will not be tested by an IStringMatcher.

Because all HTTP bodies are plain text, I think it is quite safe to use an IStringMatcher to test the content of a MultiPart body.

For example, a MultiPart body that looks like below after encoded.

--6e8b918a-4ec9-46f1-b908-8c967c342670\r\nContent-Type: application/octet-stream\r\nContent-Disposition: attachment; name=File\r\n\r\ntest file content\r\n--6e8b918a-4ec9-46f1-b908-8c967c342670\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Disposition: form-data; name=Parameters\r\n\r\nlibraryName=lib&divisionId=div&user=user&planNumber=plan&functionalRole=AdminDepartment&documentCategory=doc\r\n--6e8b918a-4ec9-46f1-b908-8c967c342670--\r\n

One can use a string matcher to test if a string "test file content" is presented.

Originally created by @davidshen84 on GitHub (Sep 6, 2019). ref: https://github.com/WireMock-Net/WireMock.Net/blob/2f406029c983cfefab920a7f5b10f29b9ad337cc/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs#L130 Based on the above code, a body which is detected as BodyType.MultiPart will not be tested by an `IStringMatcher`. Because all HTTP bodies are plain text, I think it is quite safe to use an `IStringMatcher` to test the content of a *MultiPart* body. For example, a *MultiPart* body that looks like below after encoded. ``` --6e8b918a-4ec9-46f1-b908-8c967c342670\r\nContent-Type: application/octet-stream\r\nContent-Disposition: attachment; name=File\r\n\r\ntest file content\r\n--6e8b918a-4ec9-46f1-b908-8c967c342670\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Disposition: form-data; name=Parameters\r\n\r\nlibraryName=lib&divisionId=div&user=user&planNumber=plan&functionalRole=AdminDepartment&documentCategory=doc\r\n--6e8b918a-4ec9-46f1-b908-8c967c342670--\r\n ``` One can use a string matcher to test if a string "test file content" is presented.
adam added the invalidquestion labels 2025-12-29 14:25:10 +01:00
adam closed this issue 2025-12-29 14:25:10 +01:00
Author
Owner

@davidshen84 commented on GitHub (Sep 6, 2019):

Not working if there are binary data :(

@davidshen84 commented on GitHub (Sep 6, 2019): Not working if there are binary data :(
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#208