postman-workshop

banner-image

Day - 03: Streamlining API Development with Mock Servers, Newman, and Data Flows

Mock Server

A mock server is a simulated server that mimics the behavior of a real server or API. It generates pre-defined responses to HTTP requests based on predefined scenarios or rules, without actually executing any backend logic. Mock servers are commonly used during the development and testing phases of software projects.

You can use a Postman Collection to set up a mock server. When you send a request to the mock server, Postman returns a real-world response using data from your collection. By adding a mock server to your collection and adding examples to your requests, you can simulate the behavior of a real API. Before you set up a new mock server, create a collection with the API requests you want to mock, then add saved examples to each request. The examples have the data you want the mock server to return in response to each request. When you send a request to a mock server, Postman matches the request to a saved example in your collection. Postman then responds with the data you added to the example.

Steps to create a sample Mock Server from the Collection

  1. In Postman, send a request to any API. Your request must be saved to a collection.
  2. In the response pane, select Save as Example. Postman automatically populates the example with the response you received when you sent the request.
  3. Select Collections in the sidebar. Select the More Actions icon next to the collection where the request was saved and select Mock collection.
  4. Give your mock a name and leave the other settings at their defaults. Select Create Mock Server.
  5. Copy the mock URL and go back into your request. Replace the base part of the URL with the mock server URL (everything before the path, for example up to /customers).
  6. Select Send. Postman returns the example response you saved for the request, this time from the mock server.
  7. Open the example and change the response, then save the example and send the request again. Postman returns your edited mock response.

When creating a mock server, it’s often necessary to understand the request body, headers, and other details. With the assistance of template helpers, you can effectively parse data from the request to ensure accurate and contextually appropriate responses.

Contextual response example using Template helper

With template support, Postman mock servers can generate responses that vary based on the incoming request. Template helpers give you access to data from the incoming request, such as the body, query parameters, path segments, and headers. You can include that data in the response sent by the mock server.

image

image image

Automation

Utilizing Postman CLI or Newman, you can seamlessly execute your Postman collection either from your terminal or within your CI pipeline. In this scenario, we leverage Newman as our command-line tool and GitHub Actions as our CI. Below, you’ll find an example of how we create a GitHub Action, utilizing Newman alongside htmlextra for generating a comprehensive report for your collection.

Newman cli

Newman is a command-line collection runner for Postman. It allows you to effortlessly run and test a Postman collection directly from the command line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems.

GitHub Action file

name: Automated API tests using Postman CLI

on:
  push:
    branches:
      - main

jobs:
  automated-api-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Use Node.js 20.x
        uses: actions/setup-node@v3
        with:
          node-version: "20.x"

      - name: Install Newman
        run: npm install -g newman newman-reporter-htmlextra

      - name: Run API Collection
        run: newman run "$" --env-var="baseUrl=$"

      - name: Generate HTML Report
        run: newman run "$" --env-var="baseUrl=$" -r htmlextra --reporter-htmlextra-export "newman/report.html"
      - name: see
        run: ls

      - name: Upload Report
        uses: actions/upload-artifact@v3
        with:
          name: newman-report
          path: newman/report.html

Once this action is executed, you can perform tests on your collection. If any tests fail within your collection, the action will also fail. Upon successful testing, you’ll discover a downloadable zip file containing the report of your collection.

Postman Flows

Now, let’s explore Postman Flows. Occasionally, we desire visual tools where we can interact with our API requests and responses, creating a business environment within the API data. Postman Flow provides precisely this opportunity.

Postman Flows is a visual editor that lets you directly manipulate APIs and observe the data flowing between them. You can use Flows to chain requests, handle data, and create real-world workflows in your Postman workspace. Flows are the ultimate way to take data from an API-enabled product, manipulate it to meet your needs, and see the results. Or you could take data from one API enabled product, make decisions, or process the data, then send it to another API-enabled product. This means you can build real-time integrations between two API-enabled products. To create a Flow, navigate to “Flows” from the left sidebar of Postman.

Postman Flows has different types of blocks:

(1) Task (2) Logic (3) Looping (4) Output

However, within these categories, there are additional flows to explore:

Below, I have provided some examples of Postman Flows for different APIs

Random User API: https://randomuser.me/api

image

Handle Pagination Using Flows Pokémon APIs : https://pokeapi.co/api/v2/pokemon?limit=200

image

Inventory Flow with Auth APIs: image