spring boot MediaType.MULTIPART_FORM_DATA_VALUE doesn't match with org.mock-server MediaType.MULTIPART_FORM_DATA

80 views Asked by At

I have a Spring boot application wherein I am trying to post a byte array data to a Mock server(org.mock-server) endpoint that I configured in my local for development and testing purposes.

I am using 5.13.2 version of mockserver. Following is the dependency in my pom.xml

<dependency>
            <groupId>org.mock-server</groupId>
            <artifactId>mockserver-netty-no-dependencies</artifactId>
            <version>5.13.2</version>
        </dependency>

As I post the data using rest template it fails with the following message in mock server

didn't match expectation: because:

method matched

path matched

body matched

headers didn't match

As I am using Spring's MediaType.MULTIPART_FORM_DATA, behind the scenes it appends boundary value and the header finally becomes like the following

"multipart/form data;boundary=clexl9TkRm9qMj0rUEmsVt3WtQmwNfoffzTIIHN"

Similar to spring-boot application I am setting set the expectation in Mockserver application as

.withContentType(MediaType.MULTIPART_FORM_DATA))

However, Mockserver doesn't append any boundary values.

Due to this, my request doesn't match the expectation and it fails saying headers didn't match.

Following is what I get in the error message in mock server:

 didn't match expectation:
  {
    "httpRequest" : {
      "method" : "POST",
      "path" : "<some url>",
      "headers" : {
        "content-type" : [ "multipart/form-data" ]
      }
    },
    "httpResponseClassCallback" : {
      "callbackClass" : "client.mock.MockServer$MockServerTestCallback"
    },
    "id" : "5ad06177-2fb7-4a1e-a67d-3efd4aad451e",
    "priority" : 0,
    "timeToLive" : {
      "unlimited" : true
    },
    "times" : {
      "unlimited" : true
    }
  }
 because:
  method matched
  path matched
  body matched
  headers didn't match

and the request received in mock server:

{
    "method" : "POST",
    "path" : "<some url>",
    "headers" : {
      "content-length" : [ "187787" ],
      "X-Forwarded-Proto" : [ "https" ],
      "X-Forwarded-Port" : [ "443" ],
      "X-Forwarded-For" : [ "10.227.150.116" ],
      "X-Amzn-Trace-Id" : [ "Root=1-6522a19a-0f4fcbe5241bf6491903bb64" ],
      "User-Agent" : [ "Java/1.8.0_361" ],
      "Host" : [ "mock server url" ],
      "Content-Type" : [ "multipart/form-data;boundary=clexl9TkRm9qMj0rUEmsVt3WtQmwNfoffzTIIHN" ],
      "Accept" : [ "text/plain, application/json, application/*+json, */*" ]
    },
    "keepAlive" : true,
    "secure" : false,
     ...
     ...

Is there a way I can match the incoming headers in mock-server?

0

There are 0 answers