Tuesday, June 14, 2011

REST-assured Tricks How to test HTTP 201 Created ?

How to test HTTP 201 Created ?

This is two testcases a) Status Code b) Location header

// Prepare data to be posted    
 String xmlBody = "<orderData><orderType>BUY</orderType><quantity>44</quantity><symbol>YHO</symbol></orderData>";  
String location = given()  
  .body(xmlBody)  
  .contentType(ContentType.XML).body(xmlBody)  
 // a) test status code  
   .expect().statusCode(307)  
   .when()  
   .post("/RestFullDayTrader/resources/pending_orders/100").getHeader("Location");  
 // b) test Location Header  
 assertTrue(location.contains("pending_orders/100")); 

Wednesday, June 08, 2011

REST-assured Tricks How to test Array of values -XML

RESTful web services often return multiple values. Some times in form of an array.To test for multiple values we can use .hasItems()
hasItems() works only for JSON. For XML we have to use hasXPath()
// test multiple values
String result= given().header("accept", "application/xml")
   .expect()
   .body(hasXPath("//symbol[text()='SYM9']"),hasXPath("//symbol[text()='SYM1']"))
   .when()
   .get("/RestFullDayTrader/resources/quotes").asString();

REST-assured Tricks How to test Array of values -JSON

RESTful web services often return multiple values. Some times in form of an array.
To test for multiple values we can use .hasItems()
hasItems() works only for JSON
// test multiple values
String result=  given().header("accept", "application/json")
  .expect().body("quote.symbol", hasItems("SYMBOLICIC9","SYMBOLICIC1","SYMBOLICIC9"))
  .when()
  .get("/RestFullDayTrader/resources/quotes").asString();

REST-assured Tricks How to test custom content type?

The .contentType(ContentType.JSON) supports limited set of contentypes.ie,. ANY,TEXT,JSON,XML,HTML,URLENC and BINARY.
In RestFul applications we also create custom types. for Eg I created application/vnd.useraccountprofile+json
REST-assured can be used to test any content. The trick is the use .header()
result= given().contentType(ContentType.JSON)
    .body(userAccountProfile.toString())
    .header("accept", "application/vnd.useraccountprofile+json")                
    .header("Content-Type", "application/vnd.useraccountprofile+json")               
    .when()
    .put("/RestFullDayTrader/resources/acct/"+useraccountid+";profile")
    .expect().body("openbalance", equalTo(121),"useraccountid",notNullValue())
    .asString();

REST-assured Tricks How to test a value in JSON

Use equalTo()
When the value is a string use single quotes '121'
In the below example the openbalance is a property of useraccount. useraccount is the JSON object name in the JSON.
Some implementation does not return the object name. In such cases replace "useraccount.openbalance" with "openbalance"

String result = given().header("accept", "application/json")
 .contentType(ContentType.XML).body(xmlBody)
 .expect().body("useraccount.openbalance", equalTo(121),"useraccount.useraccountid",notNullValue())
                .when().post("/RestFullDayTrader/resources/acct").asString();

REST-assured Tricks How to test for 307 Temporary Redirect ?

Testing for 307 has two parts a) test http status code b) test http header Location
String xmlBody = "<orderData><orderType>BUY</orderType><quantity>44</quantity><useraccountid>10</useraccountid></orderData>";
//xml payload
String location = given()
        .body(xmlBody)
        .contentType(ContentType.XML).body(xmlBody)
 // a) test http status code 307
 .expect().statusCode(307)
       .when()
       .post("/RestFullDayTrader/resources/pending_orders/100").getHeader("Location");
        System.out.println(" Location " + location);      
 // b) test http header Location   
  assertTrue(location1.contains("pending_orders/100"));

Tuesday, June 07, 2011

Test code using prettify

public void testReliablePostBuy() throws Exception {
        System.out.println("testReliablePostBuy");
        String xmlBody = "BUY44SYMBOLICIC110";
        //xml
    
How to use prettify with blogger/blogspot?