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.6k stars 1.07k forks source link

Multipart and regex #93

Closed danidemi closed 9 years ago

danidemi commented 9 years ago

Hi.

I'm getting this exception following an assertion.

java.lang.AssertionError: Request not found exactly once, expected:<{
  "method" : "POST",
  "body" : {
    "type" : "REGEX",
    "value" : "XMLSIP"
  }
}> but was:<{
  "method" : "POST",
  "path" : "/",
  "body" : "--_Ts2FnAsKewlbhGOMZZ0hpCevvSIMOfq4Wf6\r\nContent-Disposition: form-data; name=\"VERSIONE\"\r\n\r\nversione\r\n--_Ts2FnAsKewlbhGOMZZ0hpCevvSIMOfq4Wf6\r\nContent-Disposition: form-data; name=\"LOGINNAME \"\r\n\r\nlogin\r\n--_Ts2FnAsKewlbhGOMZZ0hpCevvSIMOfq4Wf6\r\nContent-Disposition: form-data; name=\"PASSWORD \"\r\n\r\npassword\r\n--_Ts2FnAsKewlbhGOMZZ0hpCevvSIMOfq4Wf6\r\nContent-Disposition: form-data; name=\"XMLSIP\"\r\n\r\n<xml>\r\n--_Ts2FnAsKewlbhGOMZZ0hpCevvSIMOfq4Wf6\r\nContent-Disposition: form-data; name=\"ID1\"; filename=\"filename\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n--_Ts2FnAsKewlbhGOMZZ0hpCevvSIMOfq4Wf6--\r\n",
  "headers" : [ {
    "name" : "Content-Type",
    "values" : [ "multipart/form-data; boundary=_Ts2FnAsKewlbhGOMZZ0hpCevvSIMOfq4Wf6" ]
  }, {
    "name" : "Host",
    "values" : [ "localhost:53588" ]
  }, {
    "name" : "User-Agent",
    "values" : [ "Apache-HttpClient/4.3.6 (java 1.5)" ]
  }, {
    "name" : "Content-Length",
    "values" : [ "590" ]
  } ]
}>

What it's strange is that I specified this assertion on the body...

  "body" : {
    "type" : "REGEX",
    "value" : "XMLSIP"
  }

...and if you check the body the XMLSIP string is actually there. So, if I'm getting the whole thing properly, it should not raise that exception, shouldn't it ?

danidemi commented 9 years ago

Ok, so I don't get the meaning of the regex() method...

Here another example... here I would like to assert that the body matches the regex .+. Now... obviously the body should match it because .+ matches all.

java.lang.AssertionError: Request not found exactly once, expected:<{ "method" : "POST", "body" : { "type" : "REGEX", "value" : ".+" } }> but was:<{ "method" : "POST", "path" : "/", "body" : "--ipL-ZAWLTXJsVcr0mD7neREQXV5KxOCfedJjpBy3\r\nContent-Disposition: form-data; name=\"VERSIONE\"\r\n\r\nversione\r\n--ipL-ZAWLTXJsVcr0mD7neREQXV5KxOCfedJjpBy3\r\nContent-Disposition: form-data; name=\"LOGINNAME \"\r\n\r\nlogin\r\n--ipL-ZAWLTXJsVcr0mD7neREQXV5KxOCfedJjpBy3\r\nContent-Disposition: form-data; name=\"PASSWORD \"\r\n\r\npassword\r\n--ipL-ZAWLTXJsVcr0mD7neREQXV5KxOCfedJjpBy3\r\nContent-Disposition: form-data; name=\"XMLSIP\"\r\n\r\n\r\n--ipL-ZAWLTXJsVcr0mD7neREQXV5KxOCfedJjpBy3\r\nContent-Disposition: form-data; name=\"ID1\"; filename=\"filename\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n--ipL-ZAWLTXJsVcr0mD7neREQXV5KxOCfedJjpBy3--\r\n", "headers" : [ { "name" : "Content-Type", "values" : [ "multipart/form-data; boundary=ipL-ZAWLTXJsVcr0mD7neREQXV5KxOCfedJjpBy3" ] }, { "name" : "Host", "values" : [ "localhost:46440" ] }, { "name" : "User-Agent", "values" : [ "Apache-HttpClient/4.3.6 (java 1.5)" ] }, { "name" : "Content-Length", "values" : [ "614" ] } ] }

danidemi commented 9 years ago

Ok, I got it. The reg ex should match the whole body.

Here is how I fixed it...

    mockServerClient.verify(
            request().withMethod("POST").withBody(regex("[\\n.\\w\\W]*VERSIONE[\\n.\\w\\W]*versione[\\n.\\w\\W]*")),
            VerificationTimes.exactly(1)
    );
radarsh commented 3 years ago

Or you can use multiline regexes that consider newlines as whitespaces by employing the (?ms) flag at the beginning of your regex.