SOAP attachments in WireMock

2019-07-21 22:18发布

问题:

I'm using WireMock to mock a SOAP service.

It works great, but one of the services contains an attachment. Is there any way to mock it with WireMock?

Thanks

回答1:

Yes it's possible. First, you can use SOAP ui to mock the response you are expecting with the attachment. [On the soap resource, right click: generate SOAP mock service] On the mock created, in the response you should see a dummy body corresponding to the wsld. There you can click on attachment and add a file: You run this mock and try to hit it manually with a soap request that should then appear on the request part.

It will produce for you the response with the attachment. You can see the raw part looking like this:

Content-Type: multipart/related; type="application/xop+xml"; start="<rootpart@soapui.org>"; start-info="text/xml"; boundary="----=_Part_19_678369072.1513344309074",
MIME-Version: 1.0
------=_Part_19_678369072.1513344309074
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID: <rootpart@soapui.org>

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getFileResponse xmlns:ns3="urn:eu:europa:ec:etrustex:integration:service:notification:v2.0" xmlns:ns2="urn:eu:europa:ec:etrustex:integration:service:filerepository:v2.0" xmlns="urn:eu:europa:ec:etrustex:integration:model:common:v2.0">
         <ns2:fileWrapper>
            <Content><inc:Include href="cid:test.txt" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></Content>
         </ns2:fileWrapper>
      </ns2:getFileResponse>
   </S:Body>
</S:Envelope>
------=_Part_19_678369072.1513344309074
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-ID: <test.txt>
Content-Disposition: attachment; name="test.txt"

TEST
------=_Part_19_678369072.1513344309074--

Now, you can set up wiremock with something like this:

{        
    "request": {                                    
        "method": "POST",
        "urlPattern": "/mockFileRepository"
    },                                      
    "response": {                                   
        "status": 200,                          
        "bodyFileName": "responseTest.raw",
        "headers": {
            "Content-Type": "multipart/related; type=\"application/xop+xml\"; start=\"<rootpart@soapui.org>\"; start-info=\"text/xml\"; boundary=\"----=_Part_19_678369072.1513344309074\"",
            "MIME-Version": "1.0"
        }
    }                                               
}

Pay attention the headers content-type should be the same as on the raw parts you got from soap ui.

the responseTest.raw looks something like this:

 ------=_Part_19_678369072.1513344309074
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID: <rootpart@soapui.org>

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getFileResponse xmlns:ns3="urn:eu:europa:ec:etrustex:integration:service:notification:v2.0" xmlns:ns2="urn:eu:europa:ec:etrustex:integration:service:filerepository:v2.0" xmlns="urn:eu:europa:ec:etrustex:integration:model:common:v2.0">
         <ns2:fileWrapper>
            <Content><inc:Include href="cid:test.txt" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></Content>
         </ns2:fileWrapper>
      </ns2:getFileResponse>
   </S:Body>
</S:Envelope>
------=_Part_19_678369072.1513344309074
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-ID: <test.txt>
Content-Disposition: attachment; name="test.txt"

TEST
------=_Part_19_678369072.1513344309074--

And voila!



标签: soap wiremock