Code: Select all
const newPet= {
"name": "jkysssssss",
"data": {
"year": 2017,
"price": 4589,
"CPU model": "Intel eeeeeee",
"Hard disk size": "1 TB"
}
}
export function makeRequest(requestOptions :any) {
return new Promise((resolve, reject) => {
// Make the request using cy.request with the provided requestOptions
cy.request(requestOptions).then((res) => {
if (!res || !res.body) {
return reject(new Error('No response body returned from request.'));
}
// Ensure response body is an object
if (typeof res.body !== 'object' || res.body === null) {
res.body = { data: res.body };
}
// Add status to the response
res.body.status = res.status;
// Log the response
cy.log(JSON.stringify(res.body));
// Resolve the promise with the response body
resolve(res);
})
});
}
describe('API Tests', () => {
it('should make a POST request and handle the response', async () => {
const requestOptions = {
method: 'POST', // HTTP method
url: 'https://api.restful-api.dev/objects', // API URL
headers: {
'Content-Type': 'application/json',
},
body: newPet,
};
//request calls
await makeRequest(requestOptions).then((res :any)=>{
expect(res.body.status).to.eq(200);
});
await makeRequest(requestOptions).then((res :any)=>{
expect(res.body.status).to.eq(200);
});
});
});