Automation Bro

6 Followers
·
Follow

Image for post
Image for post
https://youtu.be/d2mGuL4VlgA

Similar to the HTTP POST method, the HTTP PUT method also takes in the request payload but instead of creating a new resource, it lets the user update the existing resource.

The difference between the PUT and POST method is that PUT is idempotent i.e you can keep calling the same request multiple times but it won't have any side effects, unlike a POST request.

Let’s take a look at an example of a PUT request below -

it('PUT /users/:id', () => {
// data to update
const data = {
status: 'Active',
name: `Luffy - ${Math.floor(Math.random() * 9999)}`,
};
return request
.put('users/132') …

Image for post
Image for post
https://youtu.be/CuTLcnSna2E

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

So when working with the POST method, one of the key things to remember is to send the request data along with the request. Let’s take a look at an example of creating a new user using the POST method -

it('POST /users', () => {
// data to send with the request
const data = {
email: `test-${Math.floor(Math.random() * 9999)}@mail.ca`,
name: 'Test name',
gender: 'Male',
status: 'Inactive',
};
return request
.post('users') // hitting the POST route
.set('Authorization', `Bearer ${TOKEN}`) // setting token for authentication
.send(data)
.then((res) …

Image for post
Image for post
https://youtu.be/VrsMlnX6pUY

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 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
expect(res.body.data.id).to.be.eq(1);
});
});

Working with query parameters

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

About

Automation Bro

I enjoy discussing topics related to Test Automation | Quality Assurance | Web Dev. Check out my Youtube channel — AutomationBro

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store