top of page
Search

Working on Public Rest API example with Rest Assured

Updated: Jan 5, 2021

Before diving into working on a public API, if you haven't created your first Junit test for Rest Assured, Create your first test for testing Rest Assured.



We are going to work on a public API "deck of cards" which we already have used in testing Rest in soap UI. For detailed understanding of scenarios check Working with rest in soap UI tool.


https://deckofcardsapi.com/

As we already know how to create a Junit test, Let us dive into testing scenarios using Rest Assured.


Scenario 1: A Brand New Deck


A Deck has 52 cards, and by passing the following request, you can get a new deck with 52 cards created/generated in the response.


https://deckofcardsapi.com/api/deck/new/

Now, let's pass this URL in our code and see the response.


Code:

package test;
import static com.jayway.restassured.RestAssured.given;
import org.junit.Test;
public class FirstRestAssuredClass {
	
	@Test
	public void getResponse()
	{
given().when().get("http://deckofcardsapi.com/api/deck/new/").then().log().all();
	}

}

Understanding the code:


The syntax of Rest Assured is very BDD like and understandable.

Given(). 
        param("x", "y"). 
        header("z", "w").
when().
Method().
Then(). 
        statusCode(XXX).
        body("x, ”y", equalTo("z"));

Given() : 'Given' keyword, lets you set a background, here, you pass the request headers, query and path param, body, cookies. This is optional if these items are not needed in the request

When() : 'when' keyword marks the premise of your scenario. For example, 'when' you get/post/put something, do something else.

Method() : Substitute this with any of the CRUD operations(get/post/put/delete)

Then() : Your assert conditions go here.



Here we passed an URL to create a new deck of cards, the response we got has created a deck with "deck_id" and remaining "52" cards.


{
    "success": true,
    "deck_id": "e194cvgwvgf8",
    "remaining": 52,
    "shuffled": false
}

Scenario 2: Shuffle the Cards


This following URI will help you to create a new deck and shuffle.

https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1

in the end if you see, "deck_count" is the parameter passed, to denote the number of decks to be created.


For example, if we pass deck_count as 1, we get deck of 52 cards with unique deck_id in response, same way, if we pass deck_count as 2, we get deck of 104 cards with unique deck_id in response.

Here, we are using the QUERY parameter to pass the deck_count.


Code:


package test;
import static com.jayway.restassured.RestAssured.given;
import org.junit.Test;
public class FirstRestAssuredClass {
	
	@Test
	public void getResponse()
	{
		given().queryParam("deck_count","2")
		.when()
		.get("http://deckofcardsapi.com/api/deck/new/shuffle/")
		.then().log().all();
	}

}

Response:

HTTP/1.1 200 OK
Date: Tue, 05 Jan 2021 22:34:39 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=da559921dc7531c57794bc5a50b176c9b1609886079; expires=Thu, 04-Feb-21 22:34:39 GMT; path=/; domain=.deckofcardsapi.com; HttpOnly; SameSite=Lax
Access-Control-Allow-Origin: *
X-Frame-Options: SAMEORIGIN
Vary: Origin
CF-Cache-Status: DYNAMIC
cf-request-id: 0776493268000092e024335000000001
Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=xwyaYFN7R%2BAIjoT%2F99GDAi9lj%2BnqqwhBOri1mxL4Tnzb8zzj%2F2SEBDsz3AnO7U3d9kpX1hHAYVDXcxM%2BkkhR%2FftRfZZFBSzjMnkQ9KZTc5JdDPM%3D"}],"group":"cf-nel","max_age":604800}
NEL: {"report_to":"cf-nel","max_age":604800}
Server: cloudflare
CF-RAY: 60d0aafd7cdc92e0-SJC
Content-Encoding: gzip

{
    "success": true,
    "deck_id": "z41mjkd2tiy7",
    "remaining": 104,
    "shuffled": true
}

As expected, when we passed deck_count as 2, we have got a new deck created with 104 cards.

Scenario 3: Draw a Card


This scenario helps us to draw a card or cards from the already created deck. A deck will be identified by its deck_id.

https://deckofcardsapi.com/api/deck/<<deck_id>>/draw/?count=2

The count parameters defines how many cards should be drawn from the deck. Here we have to replace the <<deck_id>> with valid id in order to identify from which deck the card is drawn.

In our previous scenario, we have passed the "deck_count" as query parameter, the same way we are going to pass "count" as a query parameter since both of them are present after the '?' in URL.


But the "deck_id" is a part of URL path. So this cannot be considered as query parameter. Instead we are going to pass the value of "deck_id" as a PATH parameter. In this scenario, we are passing the deck_id from previously generated response. In our second Scenario, the deck_id we got in the response was "deck_id": "z41mjkd2tiy7",


Code:


package test;
import static com.jayway.restassured.RestAssured.given;
import org.junit.Test;
public class FirstRestAssuredClass {
	
	@Test
	public void getResponse()
	{
	
given().pathParam("deck_id", "z41mjkd2tiy7").queryParam("count","2")
.when()		.get("http://deckofcardsapi.com/api/deck/{deck_id}/draw/")
.then().log().all();

	}

}

Response:


{
    "success": true,
    "deck_id": "z41mjkd2tiy7",
    "cards": [
        {
            "code": "3S",
            "image": "https://deckofcardsapi.com/static/img/3S.png",
            "images": {
                "svg": "https://deckofcardsapi.com/static/img/3S.svg",
                "png": "https://deckofcardsapi.com/static/img/3S.png"
            },
            "value": "3",
            "suit": "SPADES"
        },
        {
            "code": "8S",
            "image": "https://deckofcardsapi.com/static/img/8S.png",
            "images": {
                "svg": "https://deckofcardsapi.com/static/img/8S.svg",
                "png": "https://deckofcardsapi.com/static/img/8S.png"
            },
            "value": "8",
            "suit": "SPADES"
        }
    ],
    "remaining": 102
}

The deck_id is been passed as a path parameter and In response 2 cards are drawn as expected as the "count" was passed as "2"


Conclusion:

We have used query param and path param in this tutorial to parameterize the Rest API URL.


Happy Testing!!!


17 views0 comments

Recent Posts

See All

Minimum Deletions to Make Character Frequencies Unique

A string s is called good if there are no two different characters in s that have the same frequency. Given a string s, return the minimum number of characters you need to delete to make s good. The f

Smallest String With A Given Numeric Value

The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.

bottom of page