REST-Assured is a library that is specifically designed for testing REST API services. However, what if you want to test both REST and SOAP API services using a single framework? Fortunately, it is possible to use REST-Assured for testing both types of APIs. Although REST-Assured supports XML format, it does not have a native SOAPAction method for sending requests to SOAP services.
In this article, we will explore a method for using REST-Assured to send requests to SOAP services despite this limitation.
To demo, I am using a public SOAP webservice from https://www.crcind.com/csp/samples/SOAP.Demo.cls
The WSDL file is present at https://www.crcind.com/csp/samples/SOAP.Demo.CLS?WSDL=1 and is loaded into SoapUI to do a quick sanity check for sending a sample request.
The value of the SOAPAction field is to be noted and feeds into the header in the REST Assured call. The operation we are interested in is the addition operation and hence the http://tempuri.org/SOAP.Demo.AddInteger is copied
public void RESTAssuredSOAPCall() throws IOException { File requestBody = new File(getClass().getClassLoader().getResource("addition-requestbody.xml").getFile()); Response response = RESTAssured.given() .baseUri("https://www.crcind.com:443") .basePath("/csp/samples/SOAP.Demo.cls") .header("SOAPAction", "http://tempuri.org/SOAP.Demo.AddInteger") .header("Content-Type", "text/xml; charset=utf-8") .body(requestBody) .post(); assertEquals(200,response.statusCode()); System.out.println(response.prettyPrint()); }
The request payload is saved in the addition-requestbody.xml file which is read and passed into the body().
Legal Stuff