Matt
Matt

Reputation: 3662

How to test SOAPAction header with Spring WS Test

My app is calling an external Soap WS using spring-ws's WebServiceTemplate, which I mock in my tests using MockWebServiceServer.

It works fine to simulate the response depending on the request payload.

But now I'd like to test which SOAP action is called. It should be defined in the "SOAPAction" HTTP header of the request.

I'm using Spring-WS 2.1.4.

Does anyone know if it's possible to test that and how?

Here is my test class :

public class MyWebServiceTest {
    @Autowired
    private WebServiceTemplate webServiceTemplate;

    private MockWebServiceServer mockServer;                                               

    @Before
    public void createServer() throws Exception {
        mockServer = MockWebServiceServer.createServer(webServiceTemplate);
    }

    @Test
    public void callStambiaWithExistingFileShouldSuccess() throws IOException {

        Resource requestPayload = new ClassPathResource("request-payload.xml");
        Resource responseSoapEnvelope = new ClassPathResource("success-response-soap-envoloppe.xml");

        mockServer.expect(payload(requestPayload)).andRespond(withSoapEnvelope(responseSoapEnvelope));
        //init job
        //myService call the webservice via WebServiceTemplate
        myService.executeJob(job);

        mockServer.verify();
        //some asserts
    }

}

So what I want to test is the soap action called. So I want something like this in my test class :

mockServer.expect(....withSoapAction("calledSoapAction")).andRespond(...

Upvotes: 3

Views: 3170

Answers (1)

beny23
beny23

Reputation: 35018

Creating your own RequestMatcher is pretty straightforward:

public class SoapActionMatcher implements RequestMatcher {

    private final String expectedSoapAction;

    public SoapActionMatcher(String expectedSoapAction) {
        this.expectedSoapAction = SoapUtils.escapeAction(expectedSoapAction);
    }

    @Override
    public void match(URI uri, WebServiceMessage request) 
            throws IOException, AssertionError {
        assertThat(request, instanceOf(SoapMessage.class));
        SoapMessage soapMessage = (SoapMessage) request;
        assertThat(soapMessage.getSoapAction(), equalTo(expectedSoapAction));
    }
}

Usage

mockServer.expect(connectionTo("http://server/"))
        .andExpect(new SoapActionMatcher("calledSoapAction"))
        .andRespond(withPayload(...)));

Upvotes: 6

Related Questions