Serve yourself combining Postman and Express

Alee
Alee
May 29 · 3 min read
Image for post
Image for post
Photo by Vincent Riszdorfer on Unsplash

Through this post we are going to explore one of the easiest ways to extend Postman functionalities by making your API testing easier and fun. The idea is running a basic Express server locally, that responds to Postman test scripts requests.

This idea allows you to:

  • Work around limitations, or missing features of Postman.
  • Avoid some repetitive tasks, thus saving time.
  • Have fun while testing APIs.

Some of the prerequisites to implement this, are :

  • Node.js
  • Some basic knowledge of JavaScript.

The setup

  1. Create a folder with any name you want. Navigate into it in the terminal, and run npm init, hit Enter each step of the way, to keep the default choices. A package.json should be created in your repository.
  2. In the same directory run : npm install express open this will install express , and another package we will use afterwards.

First example

To briefly provide the context of this example. While testing an API’s endpoint, we generate loan contracts PDF files encoded in Base64. While the number of use cases increases, so does these repetitive tasks :

  1. Copy the long Base64 text from postman
  2. Paste it in a text editor
  3. Use a plugin to decode it from Base64
  4. Save it in PDF format
  5. Open the file and verify the data

Assuming your API returns a response similar to :

{
"reference": "CFR2020ABX94",
"contract": "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg...."
}
  1. Create a script.js file with the following content :

2. Run the commandnode script.js, which starts a local server listening on port:3000 with a POST /saveContractroute. This decodes the file from Base64 and saves it in PDF format on your local disk.

3. Go back to Postman, in your request’s test script. Send a request to the running local server using the pm.sendRequest()method like shown in the code snippet below :

Second example

This project’s entry point is a session initialized from an REST API endpoint. Basically the API returns a url embedding a JWT token. So each time I had to copy/paste the url and open it in the browser, which was a tedious repetitive task I had to get rid off. Plus, the numerous requests from colleagues, and the client-side asking me to generate those recurring urls for different scenarios.

The idea is simple, use the open package to open the url in the default browser. Supposing your API returns a payload that contains a redirectionUrlfield among other data, same as below :

{
.....
"redirectionUrl": "https://www.medium.com/@alee37",
.....
}

In the script.js created before, add a GET route that fetches the query parameter and opens the link in the default browser. Below a code snippet demonstrating how to :

Run the commandnode script.js in the Terminal to start the server 💡

Similarly to the first example, in the request’s test script call the created GET route :

As an agile tester, it is important to continuously improve practices for an efficient delivery. The demonstrated method saves me a lot of time and effort while testing APIs for critical projects.

Please note that the demonstrated examples are simple, it can be considered as a first pillar to automate much more tasks and help you in the future. Especially with a simple and intuitive framework like Express and Postman.

YounitedTech

Le blog Tech de Younited, où l’on parle de développement…

Alee

Written by

Alee

QA Engineer @ Younited Credit

YounitedTech

Le blog Tech de Younited, où l’on parle de développement, d’architecture, de microservices, de cloud, de data… Et de comment on s’organise pour faire tout ça. Ah, et on recrute aussi, on vous a dit ?

Alee

Written by

Alee

QA Engineer @ Younited Credit

YounitedTech

Le blog Tech de Younited, où l’on parle de développement, d’architecture, de microservices, de cloud, de data… Et de comment on s’organise pour faire tout ça. Ah, et on recrute aussi, on vous a dit ?

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store