Challenge: Testing APIs

Exercise

For this exercise, you’re required to create a Postman test collection for the Credit-Check API we built previously. After creating a new collection and adding both happy- and sad-path tests, run them to validate the API.

Write the Credit-Check Postman test collection

First, fire up the Postman client and create a new collection called “Credit-Check”. Then create the following two test entries:

Happy-path: Test the home resource

  • Request URL: http://localhost:8181/
  • Method: GET
  • Body: None
  • Protocol Tests
    • HTTP status equals 200
    • content-type includes application/json
  • Structure Tests
    • home object at the root is an array
    • home has id, name, href, and rel properties
  • Content Tests
    • home.id includes list
    • home.name includes credit-check
    • home.rel includes credit-check and collection

Sad-path: Test the form resource

  • Request URL: http://localhost:8181/form/
  • Method: POST
  • Body: None
  • Protocol Tests
    • HTTP status equals 400
    • content-type includes application/problem+json
  • Structure Tests
    • error object at the root is an array
    • error has type, title, detail, status, and instance properties
  • Value Tests
    • type contains error
    • title contains Missing companyName detail contains Missing companyName status contains 400

Use the Postman client to add these two requests and write the test script using the Chai.js assertion library like we did earlier. Save the tests to Postman.

Run the test with the Postman client

After writing and saving the tests, use the Postman client to run the tests and review the results.

Terminal

  1. Click the “Run” button in the widget below.
  2. Execute the npm run dev command in the terminal.
  3. Copy the link provided (for example, https://6eqqkm56g2ewy.educative.run) instead of localhost:8181/ to use as a request URL in Postman.

Get hands-on with 1400+ tech skills courses.