Test Overview
This test sends requests using different HTTP methods (GET, POST, PUT, DELETE) to an API endpoint. It verifies that the API responds correctly according to the intent of each HTTP method.
This test sends requests using different HTTP methods (GET, POST, PUT, DELETE) to an API endpoint. It verifies that the API responds correctly according to the intent of each HTTP method.
pm.test("GET request returns 200 and data", function () { pm.sendRequest({ url: pm.environment.get("apiUrl") + "/items", method: 'GET' }, function (err, res) { pm.expect(err).to.eql(null); pm.expect(res).to.have.property('code', 200); pm.expect(res.json()).to.be.an('array'); }); }); pm.test("POST request creates new item", function () { pm.sendRequest({ url: pm.environment.get("apiUrl") + "/items", method: 'POST', header: {'Content-Type': 'application/json'}, body: { mode: 'raw', raw: JSON.stringify({name: "NewItem"}) } }, function (err, res) { pm.expect(err).to.eql(null); pm.expect(res).to.have.property('code', 201); pm.expect(res.json()).to.have.property('name', 'NewItem'); }); }); pm.test("PUT request updates existing item", function () { pm.sendRequest({ url: pm.environment.get("apiUrl") + "/items/1", method: 'PUT', header: {'Content-Type': 'application/json'}, body: { mode: 'raw', raw: JSON.stringify({name: "UpdatedItem"}) } }, function (err, res) { pm.expect(err).to.eql(null); pm.expect(res).to.have.property('code', 200); pm.expect(res.json()).to.have.property('name', 'UpdatedItem'); }); }); pm.test("DELETE request removes item", function () { pm.sendRequest({ url: pm.environment.get("apiUrl") + "/items/1", method: 'DELETE' }, function (err, res) { pm.expect(err).to.eql(null); pm.expect(res).to.have.property('code', 204); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Send GET request to /items endpoint | API server is running and has items data | Response code is 200 and response body is an array | PASS |
| 2 | Send POST request to /items with new item data | API server accepts new item creation | Response code is 201 and response body contains the new item name | PASS |
| 3 | Send PUT request to /items/1 to update item name | API server updates item with id 1 | Response code is 200 and response body contains updated item name | PASS |
| 4 | Send DELETE request to /items/1 to remove item | API server deletes item with id 1 | Response code is 204 with no content | PASS |