mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.54k stars 1.06k forks source link

How to Mock Post request with multi part form body #1431

Closed parimalakolisetty closed 1 year ago

parimalakolisetty commented 1 year ago

I need to mock a post request with multi part form body as given below. tried with regex pattern for unqiue string Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN , but didnt work. can some one guide me on this .

BODY :

"body":"--Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN Content-Disposition: form-data; name="dataset" Content-Type: text/plain;charset=UTF-8 Content-Length: 14 drugs --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN Content-Disposition: form-data; name="searchQuery" Content-Type: text/plain;charset=UTF-8 Content-Length: 6 cancer --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN Content-Disposition: form-data; name="queryLanguage" Content-Type: text/plain;charset=UTF-8 Content-Length: 4 ssql --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN Content-Disposition: form-data; name="returnFields" Content-Type: text/plain;charset=UTF-8 Content-Length: 22 Id,name --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN Content-Disposition: form-data; name="returnLimit" Content-Type: text/plain;charset=UTF-8 Content-Length: 1 1 --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN Content-Disposition: form-data; name="offset" Content-Type: text/plain;charset=UTF-8 Content-Length: 1 0 --Z5KfGboCdJvPl4VsEq74AeK7PZN4EyN-- "

I tried like -

mockServerClient .when( request() .withMethod("POST") .withHeaders( new Header("Content-Type", "multipart/form-data")) .withPath("abc/xyz") .withBody(regex(**"[\n.\w\W]** Content-Disposition: form-data; name=\"dataset\" Content-Type: text/plain;charset=UTF-8 Content-Length: 14 drugs [\n.\w\W] Content-Disposition: form-data; name=\"searchQuery\" Content-Type: text/plain;charset=UTF-8 Content-Length: 6 cancer [\n.\w\W] Content-Disposition: form-data; name=\"queryLanguage\" Content-Type: text/plain;charset=UTF-8 Content-Length: 4 ssql [\n.\w\W] Content-Disposition: form-data; name=\"returnFields\" Content-Type: text/plain;charset=UTF-8 Content-Length: 22 Id,name [\n.\w\W] Content-Disposition: form-data; name=\"returnLimit\" Content-Type: text/plain;charset=UTF-8 Content-Length: 1 1 [\n.\w\W] Content-Disposition: form-data; name=\"offset\" Content-Type: text/plain;charset=UTF-8 Content-Length: 1 0 [\n.\w\W]*-- ")) )

jamesdbloom commented 1 year ago

MockServer doesn't currently support multi-part forms but this is on the backlog here: https://trello.com/c/zt7loffA/14-add-support-for-multipartbody