Challenge: Testing APIs
Practice writing and running the Credit-Check Postman test collection.
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
includesapplication/json
- HTTP status equals
- Structure Tests
home
object at the root is an arrayhome
hasid
,name
,href
, andrel
properties
- Content Tests
home.id
includeslist
home.name
includescredit-check
home.rel
includescredit-check
andcollection
Sad-path: Test the form
resource
- Request URL:
http://localhost:8181/form/
- Method:
POST
- Body: None
- Protocol Tests
- HTTP status equals
400
content-type
includesapplication/problem+json
- HTTP status equals
- Structure Tests
error
object at the root is an arrayerror
hastype
,title
,detail
,status
, andinstance
properties
- Value Tests
type
containserror
title
containsMissing companyName
detail
containsMissing companyName
status
contains400
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
- Click the “Run” button in the widget below.
- Execute the
npm run dev
command in the terminal. - Copy the link provided (for example,
https://6eqqkm56g2ewy.educative.run
) instead oflocalhost:8181/
to use as a request URL in Postman.
Get hands-on with 1200+ tech skills courses.