const mockServerUrl = pm.environment.get('mockServerUrl');
// Test GET /user
pm.test('GET /user returns 200 and expected JSON', async () => {
const getResponse = await pm.sendRequest({
url: `${mockServerUrl}/user`,
method: 'GET'
});
pm.expect(getResponse).to.have.property('code', 200);
pm.expect(getResponse.json()).to.deep.equal({"id": 1, "name": "John Doe"});
});
// Test POST /user unsupported method
pm.test('POST /user returns error status for unsupported method', async () => {
const postResponse = await pm.sendRequest({
url: `${mockServerUrl}/user`,
method: 'POST',
body: {
mode: 'raw',
raw: JSON.stringify({"id": 2, "name": "Jane"})
}
});
pm.expect(postResponse.code).to.be.oneOf([404, 405]);
});This Postman test script uses pm.sendRequest to send HTTP requests to the mock server URL stored in an environment variable.
First, it sends a GET request to /user and asserts the response status code is 200 and the JSON body matches the expected user data.
Second, it sends a POST request to the same endpoint, which is unsupported by the mock server, and asserts the response status code is either 404 or 405, indicating the method is not allowed.
Using pm.test groups assertions clearly, and pm.expect checks the response properties. This approach verifies mock server limitations on supported HTTP methods.