Chapter Exercise

Design and Build Great Web APIs — by Mike Amundsen (74 / 127)

The Pragmatic Programmers
The Pragmatic Programmers
2 min readApr 28, 2021

--

👈 What’s Next? | TOC | Chapter 10 Securing APIs 👉

For this exercise, we’ll create a Postman test collection for the credit-check API you built in the previous chapter. After creating a new collection and adding both happy- and sad-path tests, we’ll run them to validate the API.

For extra credit, you can export your API and run it locally using the newman utility to produce an HTML report.

Write the Credit-Check Postman Test Collection

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

Happy-Path Test

  • 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

  • Test the from 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 ChaiJS assertion library as we did earlier in this chapter. 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.

Extra Credit: Run the Tests with the Newman Client

After you are sure all tests pass as expected, export your test collection and save it to disk. Then use the newman CLI utility to run the tests locally. Be sure to use the htmlextra reporting option to produce an HTML report of your test results.

See Appendix 2 (Solution for Chapter 9: Testing APIs) for the solution.

Footnotes

[70] https://dannorth.net/introducing-bdd

[71] https://dannorth.net/whats-in-a-story

[72] https://www.postman.com

[73] https://www.chaijs.com

[74] https://en.wikipedia.org/wiki/Fluent_interface

[75] https://tools.ietf.org/html/rfc7807

Copyright © 2020, The Pragmatic Bookshelf.

👈 What’s Next? | TOC | Chapter 10 Securing APIs 👉

Design and Build Great Web APIs by Mike Amundsen can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

Unlisted

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.