Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post complex XML using rest assured

Using rest-assured we can easily perform GET, POST and other methods. In the example below we are sending a POST to an API that returns a JSON response.

@Test
public void reserveARide()
{
    given().
        header("Authorization", "abcdefgh-123456").
        param("rideId", "gffgr-3423-gsdgh").
        param("guestCount", 2).
    when().
        post("http://someWebsite/reserveRide").
    then().
        contentType(ContentType.JSON).
        body("result.message", equalTo("success"));
}

But I need to create POST request with complex XML body. Body example:

<?xml version="1.0" encoding="UTF-8"?>
<request protocol="3.0" version="xxx" session="xxx">
<info1 param1="xxx" version="xxx" size="xxx" notes="xxx"/>
<info2 param1="xxx" version="xxx" size="xxx" notes="xxx"/>
</request>

How can I do this? Thank you in advance

like image 843
Art Pol Avatar asked Sep 25 '22 11:09

Art Pol


1 Answers

I keep my bodies in the resources directory, and read them into a string using the following method:

public static String generateStringFromResource(String path) throws IOException {

    return new String(Files.readAllBytes(Paths.get(path)));

}

Then in my request I can say

String myRequest = generateStringFromResource("path/to/xml.xml")

        given()
            .contentType("application/xml")
            .body(myRequest)
        .when()
            .put("my.url/endpoint/")
        .then()
            statusCode(200)
like image 122
Luke D. Smith Avatar answered Oct 11 '22 14:10

Luke D. Smith