SDET Unicorns

Write API Tests for HTTP GET Method

Table of Content

Let’s take a look at how to write API tests using JavaScript for the 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 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
    expect(res.body.data.id).to.be.eq(1);
  });
});

Working with query parameters

You can also provide query params in the request URL.

// 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) => {
    expect(res.body.data).to.not.be.empty;
    // validate all the data being returned are as per the query params provided
    res.body.data.forEach((data) => {
      expect(data.gender).to.eq('Female');
      expect(data.status).to.eq('Active');
    });
  });
});

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 –

JavaScript API Automation Testing Tutorial Series

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster

5/5

Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

Differences Between CI/CD and DevOps

Key Differences Between CI/CD and DevOps

Software development is far from a simple journey that involves merely executing code and launching applications. It often presents challenges, as it’s rare to get everything right from the start. Each time you implement new or frequent code changes, you inadvertently introduce potential issues that can be time-consuming to resolve. This complexity necessitates a structured approach to ensure smooth production.

Read More »
image

Common Types of Software Bugs

Dealing with bugs is one of the biggest headaches in the software development lifecycle. No matter how carefully someone writes code, it’s impossible to create a software product that works perfectly every time. Skipping detailed testing often causes major issues later on.

Read More »