Step 1
It is always good practice to write tests, but perhaps essential before refactoring our code. Having tests enables us to continuously test the code as we refactor it, to ensure the changes we make don't break the expected behavior.
For testing APIs, we can use Postman. In its simplest form, we can save the API requests, group them in "collections" and run them at a later time. This arrangement would serve similar to having a suite of tests.
You can do a lot more with Postman, such as Automated Testing and API Monitoring. Please refer to their documentation for more details.
For this lecture, ensure you have all the following API requests "saved" in a "collection". To create a collection, simply click on + New Collection
and give it a name.
To save an API (HTTP) Request in Postman, simply press
Command
+S
(orCtrl
+S
if on Windows), give it a name and select which collection it should be saved to.
Here are the API requests we have made in the previous module, as we have developed YouNote API:
Get all notes
Get notes by a given author
Get a note given its ID
Create a note
Delete a note given its ID
Delete note (invalid ID)
Update note
Update note (invalid ID)
Update note (invalid attributes)
A nice feature in Postman is that you can "run" all the requests saved in a collection (which is similar to running all your "tests"), and you can share your collection with others, in various forms! I have, for instance, exported the collection into a JSON file and you can download it from here and import it to your Postman application.
To import the collection, click on the big Import button on the top left side of the Postman application.