DEV Community

Cover image for Write API tests for HTTP GET method
Dilpreet Johal
Dilpreet Johal

Posted on

Write API tests for HTTP GET method

Let's take a look at how to write API tests using JavaScript for HTTP GET method.

So in the previous post, we wrote a basic GET test to get us started, and now we'll take a step further and write a couple more of GET tests to get a good understanding of them.

Accessing an individual resource

In this test, we'll try to access data for an individual user.

 // access the user data based on the id provided
 it('GET /users/:id', () => {
    return request.get(`users/1?access-token=${TOKEN}`).then((res) => {
// validate the data being returned is of the expected user
Enter fullscreen mode Exit fullscreen mode

Working with query parameters

Sometimes, you need to provide query params to filter out the data being requested.

// Filtering data based on page, gender and status
 it('GET /users with query params', () => {
    const url = `users?access-token=${TOKEN}&page=5&gender=Female&status=Active`;

    return request.get(url).then((res) => {
// validate all the data being returned are as per the query params provided => {
Enter fullscreen mode Exit fullscreen mode

Alright, so this covers pretty much the majority of the scenarios that you will work with when writing API tests for HTTP GET methods. 🙌

Check out this video to see a detailed explanation on how to work with HTTP GET method:

You can also clone the GitHub repo to access this code

To learn more about API testing, check out my free tutorial series here -

I hope this post helped you out, let me know in the comments below!

Happy testing! 😄


Subscribe to my YouTube channel
Support my work -
Follow @automationbro on Twitter

Discussion (0)