...

/

Solution: Testing Endpoints with Postman

Solution: Testing Endpoints with Postman

Review the solution to testing the profile and logout endpoints.

Challenge solution

Let's review the code for each task of the challenge exercise.

Import the collection

Follow the steps below to import the challenge collection:

Logged in profile request

The screenshot below illustrates the three test cases to verify the response data, status code, and authentication token:

Press + to interact
pm.test("given authorized user, returns 200 status", () => {
pm.response.to.have.status(200);
});
pm.test("given authorized user, returns expected response data", () => {
const RESPONSE = pm.response.json();
const USER = pm.collectionVariables.get("username");
pm.expect(RESPONSE['user']).to.eql(USER);
});
pm.test("given authorized user, returns 200 status", () => {
const TOKEN = pm.cookies.get('token');
pm.expect(TOKEN).to.be.a('string');
});

Let's explain the code above:

  • In lines 1–3, we verify the server responds with a 200 OK status.

  • In lines 5–10, we confirm the server sends an id and the username for the logged-in user.

  • In lines 12–15, we verify an authentication token exists in the response. This functionality is critical to verify because a user should not be able to access the profile endpoint ...

Access this course and 1400+ top-rated courses and projects.