I am using WebServiceGatewaySupport class as a support for sending SOAP requests
@Service
public class SoapClient extends WebServiceGatewaySupport implements BeanNameAware {
private static final Logger LOG = LoggerFactory.getLogger(SoapClient.class);
public <T, U> JAXBElement<T> getSoapResponse(JAXBElement<U> requestObj) {
WebServiceTemplate webServiceTemplate = getWebServiceTemplate();
webServiceTemplate.setMessageSender(getWebServiceMessageSender());
String defaultUri = webServiceTemplate.getDefaultUri();
try {
return (JAXBElement<T>) webServiceTemplate.marshalSendAndReceive(requestObj);
} catch (Exception ex) {
LOG.error("Exception during soap call");
}
}
I am also extending HttpUrlConnectionMessageSender and overriding prepareConnection method for adding custom headers.
public class CustomWebServiceMessageSender extends HttpUrlConnectionMessageSender {
@Override
protected void prepareConnection(HttpURLConnection connection) throws IOException {
addApiKeyHeaders(connection);
super.prepareConnection(connection);
}
private void addApiKeyHeaders(HttpURLConnection connection) {
connection.setRequestProperty("customHeaderKey", "customHeaderValue");
}
}
}
I need to write a test that will check that for any SOAP request the customHeader is added. How can I test that?
Test expectation:
Before adding header:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<!-- Other SOAP header elements -->
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<!-- SOAP body content -->
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
With header:
POST /some-endpoint HTTP/1.1
Host: example.com
Content-Type: text/xml; charset=utf-8
Content-Length: xxx
Custom-Header: CustomValue
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<!-- Other SOAP header elements -->
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<!-- SOAP body content -->
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>