Search code examples
postmanpostman-newman

Variable, relative resource path for binary request body in Postman/Newman


Context

I am developing a web application that avails of a suite of REST tests built for Postman.

The idea is that you can run the tests manually with Postman as a REST client against the application runtime, and the Maven pom configures newman to run them automatically in the CI pipelines as integration tests when ever a build is triggered.

This has run fairly well in the past.

Requirement

However, due to an overhaul in business logic, many of those tests now require a binary body as a file resource in POST requests (mostly zip archives).

I need those tests to work in 3 scenarios:

  1. Manually when running individual tests with Postman locally, against a runtime of the web application
  2. Semi-manually as above, but by triggering a runner in Postman
  3. Automatically, when newman is started by maven during the integration tests phase of our pipeline

In order to make sure the path to the file in each request would work regardless of the way the tests are run, I have added a Postman environment variable in each profile. The variable would then be used by the collection in the relevant requests, e.g.:

"body": {
          "mode": "file",
          "file": {
            "src": "{{postman_resources_path}}/empty.zip"
          }
        },

The idea would be that:

  • locally, you manually overwrite the value of postman_resources_path in the profile, in order to point to an absolute path in your machine (e.g. simply where you have the resources in source control) - this would then resolve it both for manual tests and a local runner
  • for the CI pipelines, the same would apply with a default value pointing to a path relative to the --working-dir value, which would be set in the newman command-line parametrization in the exec-maven-plugin already in use to run newman

Problem

While I haven't had a chance to test the pipeline yet with those assumptions, I can already notice that this isn't working locally.

Looking at a request, I can see the environment variable is not being resolved:

resource path not resolved in request body

Conversely, here is the value that I manually set in the profile I'm running the request against:

resource path variable

TL;DR The request fails, since the resource is not found.

The most relevant literature I've found does not address my use case entirely, but the solution given seems to follow a similar direction: "variabilize" the path - see here.

I could not find anything specific enough in the Postman reference.


Solution

  • I think I'm onto something here, but I won't accept my own answer yet.

    TL;DR it may be simpler than it seemed initially.

    This Postman doc page states:

    When you send a form-data or binary file with a request body, Postman saves a path to the file as part of the collection. The file path is relative to your working directory.

    If I modify the raw collection json to ensure only the file name (or any relative path) is the value of the "src" key in the file definition, and set up the working directory manually in my Postman client, it seems to resolve the file correctly --> no need for (non-working) variables in the file path.

    The working directory setting does not seem to be saved in the collection, meaning a manual one-time setup for local clients and the usage of --working-dir with newman should do the trick altogether.

    Will self-accept once I've successfully tested with newman.