Install Axios
Axios is a nodejs package that is used to call HTTP Request Methods.
We can call the following HTTP Request Methods:
1. GET
2. POST
3. PUT
4. PATCH
4. DELETE
At first, create a blank folder named axios-demo-api-test
Open the folder with Visual Studio Code. If you do not have installed Visual Studio Code, download from here
Go to Terminal > New Terminal
Now hit the following command: npm init -y
Then,npm i axios mocha chai
After installing successfully, you will get the packages has been installed successfully
Also you can check from package.json that the dependencies has been installed
Calling a simple GET API
Create a file named book.test.js
Write following cods:
describe("Get all book list",async()=>{
it("Get books",async()=>{
const response = await axios.get('https://demoqa.com/bookstore/v1/books', { //calling the get API
headers: {
'Content-Type': 'application/json',
}
});
console.log(response.data);
expect(response.status).equals(200); //asserting if the response code is 200
})
})
Here, ‘describe‘ means test scenario and ‘it‘ means individual test cases
Then hit the following command:
npx mocha .\book.test.js --timeout=30000
Then the HTTP Request will be executed and you will get the response
Now we will do an interesting job that is,
- Call login API to generate token for authorization
- Get user list by calling customer API
Calling POST API
First of all we will call a POST API for user login. Create a file named user.test.js
API URL: http://dmoney.roadtocareer.net/user/login
API Body:
{
"email":"salman@roadtocareer.net",
"password":"1234"
}
Write following code:
const { expect } = require('chai');
const axios = require('axios');
describe("User can do login", () => {
it("User can login successfully", async () => {
var response = await axios.post(`http://dmoney.roadtocareer.net/user/login/user/login`,
{
"email": "salman@roadtocareer.net",
"password": "1234"
},
{
headers: {
"Content-Type": "application/json",
}
})
console.log(response.data)
expect(response.data.message).contains("Login successfully")
})
})
Now hit following command:
npx mocha --timeout=30000 .\test\user.test.js
Response:
We see that a token is returned in the response body. We will store this token in a JSON file so that we can use it multiple times. Also we will keep the base URL to a JSON file
Setting up Environment Variables
Create a file named env.json
write following code:
{
"baseUrl":"http://dmoney.roadtocareer.net",
"token":""
}
Then install following package
npm i fs
Then, update the above code as below:
const { expect } = require('chai');
const axios = require('axios');
const jsonData = require('./env.json');
const fs = require('fs')
it("User can login successfully", async () => {
var response = await axios.post(`${jsonData.baseUrl}/user/login`,
{
"email": "salman@roadtocareer.net",
"password": "1234"
},
{
headers: {
"Content-Type": "application/json",
}
})
console.log(response.data)
expect(response.data.message).contains("Login successfully")
let token_value = response.data.token;
jsonData.token = token_value;
fs.writeFileSync('env.json', JSON.stringify(jsonData))
})
Execute code by following command:
npx mocha .\user.test.js --timeout=30000
You will see that the token has been written to the env.json file
Getting the customer list
Now, write following code. Ofcourse your code should be withing describe codeblock.
it("Admin can get user list", async () => {
var response = await axios.get(`${jsonData.baseUrl}/user/list`,
{
headers: {
"Content-Type": "application/json"
}
})
console.log(response.data.users);
})
Execute the code by this command:
npx mocha .\user.test.js --timeout=30000
You will see that response has an error containing the 401 response code. It means Authorization error.
Configure Header Authorization
So now, you have to pass the token from the header
Add this line to the header object
‘Authorization': jsonData.token
So, the updated code will be:
it("Admin can get user list", async () => {
var response = await axios.get(`${jsonData.baseUrl}/user/list`,
{
headers: {
"Content-Type": "application/json",
"Authorization": jsonData.token
}
})
console.log(response.data.users);
})
Execute the code by this command:
npx mocha .\user.test.js --timeout=30000
Now the response is containing the customer list having 2 passing
The Login API and The customer List API
In the response, we see that, customer list is showing in response.
Here is the full code:
const { expect } = require('chai');
const axios = require('axios');
const jsonData = require('./env.json');
const fs = require('fs')
describe("User can do login", () => {
it.on("User can login successfully", async () => {
var response = await axios.post(`${jsonData.baseUrl}/user/login`,
{
"email": "salman@roadtocareer.net",
"password": "1234"
},
{
headers: {
"Content-Type": "application/json",
}
})
console.log(response.data)
expect(response.data.message).contains("Login successfully")
let token_value = response.data.token;
jsonData.token = token_value;
fs.writeFileSync('env.json', JSON.stringify(jsonData))
})
it("Admin can get user list", async () => {
var response = await axios.get(`${jsonData.baseUrl}/user/list`,
{
headers: {
"Content-Type": "application/json",
"Authorization": jsonData.token
}
})
console.log(response.data.users);
})
})
Leave a Reply