As APIs evolve, RESTful services have become widely popular, but SOAP (Simple Object Access Protocol) services are still prevalent in many enterprise applications. Rest Assured, a popular library for testing RESTful APIs, is versatile enough to help us test SOAP services as well. In this blog, we’ll explore how you can effectively use Rest Assured to test SOAP-based web services.
What is SOAP?
SOAP is a protocol for exchanging structured information in a decentralized, distributed environment. Unlike REST, SOAP relies on XML for message format and usually operates over HTTP, but it can work over other protocols as well. SOAP services often define a WSDL (Web Services Description Language) file, which provides a contract for what the service offers and how to interact with it.
Why Use Rest Assured for SOAP Testing?
Rest Assured is primarily used for testing REST APIs, but its flexibility makes it suitable for testing SOAP as well. Using Rest Assured, you can send HTTP requests to SOAP endpoints and validate responses, making it a convenient tool if you’re already familiar with REST API testing in Java.
Prerequisites
- Java - Install Java SDK, as Rest Assured is a Java-based library.
- Maven - For dependency management.
- Rest Assured - Add it to your Maven project.
Here’s the dependency to add in your pom.xml
:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.3.0</version>
</dependency>
Getting Started: Testing SOAP with Rest Assured
Step 1: Understand the SOAP Request Structure
SOAP requests are XML-based. A typical SOAP request message includes an envelope, header, and body:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://www.example.com/webservices">
<soapenv:Header/>
<soapenv:Body>
<web:GetCityWeatherByZIP>
<web:ZIP>10001</web:ZIP>
</web:GetCityWeatherByZIP>
</soapenv:Body>
</soapenv:Envelope>
In this request:
- The
Envelope
is the outermost element. - The
Body
contains the main message. - The
Header
is optional, often used for security or authentication tokens.
Step 2: Set Up Rest Assured for SOAP
To work with SOAP services, we’ll need to configure Rest Assured to:
- Send
Content-Type: text/xml
in the header. - Use XML-based request bodies.
Let’s assume we are testing a SOAP service with an endpoint URL like http://www.example.com/webservice
.
Step 3: Create a SOAP Request Using Rest Assured
Here’s a Java method for sending a SOAP request using Rest Assured:
import io.restassured.RestAssured;
import io.restassured.response.Response;
public class SOAPServiceTest {
public static void main(String[] args) {
// Define SOAP XML request as a String
String requestBody =
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"xmlns:web=\"http://www.example.com/webservices\">" +
"<soapenv:Header/>" +
"<soapenv:Body>" +
"<web:GetCityWeatherByZIP>" +
"<web:ZIP>10001</web:ZIP>" +
"</web:GetCityWeatherByZIP>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
// Send SOAP request using Rest Assured
Response response = RestAssured.given()
.header("Content-Type", "text/xml")
.body(requestBody)
.post("http://www.example.com/webservice");
// Print the response
System.out.println("Response: " + response.asString());
// Assertion example
response.then().statusCode(200);
response.then().body("Envelope.Body.GetCityWeatherByZIPResponse.City", equalTo("New York"));
}
}
In this example:
- We define the SOAP XML request as a
String
. - We configure Rest Assured to send the request to the SOAP endpoint.
- We set the
Content-Type
header totext/xml
. - We use
.post()
to send the request, as SOAP typically uses HTTP POST.
Step 4: Parse and Validate the SOAP Response
In SOAP services, the response is also XML. You can use Rest Assured’s xmlPath()
to navigate and validate parts of the XML response.
// Extract a specific field from the response
String city = response.xmlPath().getString("Envelope.Body.GetCityWeatherByZIPResponse.City");
System.out.println("City: " + city);
// Validate the extracted data
assertEquals(city, "New York");
Here, we use xmlPath()
to navigate through the XML structure and extract the city name from the response. We then validate it with an assertion.
Advanced SOAP Testing with Rest Assured
Handling Authentication
Some SOAP services require authentication, which you can handle by adding an Authorization
header or using basic()
for basic authentication.
RestAssured.given()
.auth()
.basic("username", "password")
.header("Content-Type", "text/xml")
.body(requestBody)
.post("http://www.example.com/secureWebService");
Dynamic Request Parameters
If your SOAP request requires dynamic parameters, you can use Java string formatting or template-based libraries like StringTemplate
to construct the XML payload.
String zipCode = "10001";
String requestBody = String.format(
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"xmlns:web=\"http://www.example.com/webservices\">" +
"<soapenv:Header/>" +
"<soapenv:Body>" +
"<web:GetCityWeatherByZIP>" +
"<web:ZIP>%s</web:ZIP>" +
"</web:GetCityWeatherByZIP>" +
"</soapenv:Body>" +
"</soapenv:Envelope>", zipCode);