Get Started with REST API Testing


In this tutorial, we will demonstrate how RESTful APIs or web services can be tested using Testsigma. In this demo, we are going to use JSONPlaceholder which is a free online REST service that you can use whenever you need some mock data.


API resources and routes

Resources

API Entry point - https://jsonplaceholder.typicode.com/

These are the resources or mock data available for inspection via the API. The left column specifies the URL for the request and right column specifies the number of JSON object items returned in the response.

/posts 100 items
/comments 500 items
/albums 100 items
/photos 5000 items
/todos 200 items
/users 10 items

We can append any of the above resource names to the API entry point to form a valid GET request.

Routes

A few of the available routes are given below:

https://jsonplaceholder.typicode.com/posts will return a response containing 100 posts
https://jsonplaceholder.typicode.com/posts?userId=1 will return a response containing the posts for userid=1
https://jsonplaceholder.typicode.com/posts/1 will return a response containing the post with id=1
https://jsonplaceholder.typicode.com/posts/1/comments will return a response containing the comments for post with id=1
https://jsonplaceholder.typicode.com/comments will return a response containing 500 comment items
https://jsonplaceholder.typicode.com/comments?postId=1 will return a response containing the comments for post with id=1
https://jsonplaceholder.typicode.com/users will return a response containing 10 users
https://jsonplaceholder.typicode.com/users/1 will return a response containing the user details for id=1

All of the most common HTTP verbs such as GET, POST, PUT, PATCH, and DELETE are supported.

If you are new to Web Service Testing, see How to add Test Steps for Web Service/API Testing.


Demo Test

Now, let us test the above API using Testsigma. We will perform a total of 8 tests as given below:

  1. Test the Expected Response Status Code for a simple GET request.
  2. Test the Expected Response Header for a GET request.
  3. Test the Expected Response Body for a GET request.
  4. Create a user using POST request and test the Expected Status Code, Header, and Body.
  5. Create a user using PUT request and test the Expected Status Code, Header, and Body.
  6. Delete a user using DELETE request and test the Expected Response.

1. Test the Expected Response Status Code for a GET request

This test checks for the Expected Status Code of the Response for a simple GET request (no request header provided). The following entries are to be made in Testsigma:

URL: https://jsonplaceholder.typicode.com/users

HTTP Method: GET

Test: Status Code

Expected Status Code: 200

Create a Rest API Step for a GET request

Verify Response Status Code for a GET request


2. Test the Expected Response Header for a GET request

This test checks for the Expected Header of the Response for a simple GET request (no request header provided). The following entries are to be made in Testsigma:

URL: https://jsonplaceholder.typicode.com/users

HTTP Method: GET

Test: Header Test

Expected Header Content: {"content-type":"application/json; charset=utf-8"}

Verify Response Header for a GET Request


3. Test the Expected Response Body for a GET request

This test checks for the Expected Body Content of the Response for a simple GET request (no request header provided). The following entries are to be made in Testsigma:

URL: https://jsonplaceholder.typicode.com/users/1

HTTP Method: GET

Test: Validate User data

Expected Body Content: {"id": 1,"name": "Leanne Graham","username": "Bret","email": "Sincere@april.biz"}

Body Comparison Type: Lenient

Get request to validate user data

Verify Response Body for a GET Request


4. Create a post using POST request and test the Expected Status Code, Header, and Body

This test creates a post using POST request and checks for the Header, Body and Status Code of Expected Response with no request header. The following entries are to be made in Testsigma:

URL: https://jsonplaceholder.typicode.com/posts

HTTP Method: POST

Payload: {"title":"foo","body":"bar","userId":1}

Expected Status Code: 201

Expected Header: {"content-type":"application/json; charset=utf-8"}

Expected Body: {"id":101,"title":"foo","body":"bar","userId":1}

Comparison Type: Lenient

Create a REST API Step for POST Request

Verify Header and Body for POST Request


5. Update a post using PUT request and test the Expected Status Code, Header, and Body

This test updates a post using PUT request and checks for the Header, Body and Status Code of Expected Response with no request header. The following entries are to be made in Testsigma:

URL: https://jsonplaceholder.typicode.com/posts

HTTP Method: PUT

Payload: {"title":"foo","body":"bar","userId":1}

Expected Status Code: 200(updated)

Expected Header: {"content-type":"application/json; charset=utf-8"}

Comparison Type: Lenient

Expected Body: {"id":101,"title":"foo","body":"bar","userId":1}

Comparison Type: Lenient

Create a REST API Step for PUT Request

Verify Response Header for PUT Request


6. Delete a user using DELETE request and test the Expected Response

This test deletes a user and checks the Expected Header Response for a simple DELETE request with no request header. The following entries are to be made in Testsigma:

URL: https://jsonplaceholder.typicode.com/users/1

HTTP Method: DELETE

Test: Status Code

Expected Status Code: 200

Expected Header: {"content-type":"application/json; charset=utf-8"}

Comparison Type: Lenient

Create a REST API Step for DELETE Request

Verify the Response Status for DELETE Request