41

I would like to check the value from a concrete response header ("Location") as Test Results in Postman. In the Postman's documentation I found examples of how to check the existance of headers with

pm.test("Content-Type is present", function () {
   pm.response.to.have.header("Content-Type");
});

But what I'm looking for is something like

pm.test("Location value is correct", function () {
   CODE HERE THAT CHECKS "Location" HEADER EQUALS TO SOMETHING;
});
Lluís Suñol
  • 3,466
  • 3
  • 22
  • 33

6 Answers6

94

I finally found the solution:

pm.test("Redirect location is correct", function () {
   pm.response.to.have.header("Location");
   pm.response.to.be.header("Location", "http://example.com/expected-redirect-url");
});
Lluís Suñol
  • 3,466
  • 3
  • 22
  • 33
21

Here is another way of fetching the specific response header in Tests section ...

loc = pm.response.headers.get("Location");

Just in case, if the subsequent request(s) need the specific info, like header value, then you can also store/set it as environment variable as below, and reuse further

pm.environment.set("redirURL", loc);

var loc = null;
pm.test("Collect redirect location", function () {
   pm.response.to.have.header("Location");
   loc = pm.response.headers.get("Location");
   if (loc !== undefined) {
      pm.environment.set("redirURL", loc);
   }
});

The advantage is - the value collected in the variable can be manipulated.

But it all depends on the situation. Like, you might want to extract and pre/post-process the redirect URL.

For example,

While running the test collection, you would like to collect the value in a variable and change it to point to the mock server's host:port.

Irfan Makandar
  • 211
  • 2
  • 3
1

HeadersList has the method has(item, valueopt) → {Boolean}, so easiest way to check the header is:

const base_url = pm.variables.get("base_url")

pm.test("Redirect to OAuth2 endpoint", () => {
    pm.expect(pm.response.headers.has("Location",`${base_url}/oauth2/`)).is.true
})
leonidv
  • 1,332
  • 13
  • 20
0

pm.test("Location value is correct", function () {
   pm.expect(pm.response.headers.get('Location')).to.eql('http://google.com');
});
Vallabha Vamaravelli
  • 1,153
  • 1
  • 9
  • 15
0

Postman also supports ES6/ES2015 syntax as well, allowing us to use arrow functions.

So here's how a simple test to verify that the common response headers are present:

pm.test("Verify response headers are present ", () => {
    pm.response.to.have.header("Date");
    pm.response.to.have.header("Content-Length");
    pm.response.to.have.header("Content-Type");
});

Of course, you can check for any custom headers you may have returned by your API.

Mirza Sisic
  • 2,401
  • 4
  • 24
  • 38
0

Using the BDD expect/should style:

pm.test("Redirect location is correct", () => {
   pm.expect(pm.response).to.include.header("Location");
   pm.expect(pm.response).to.have.header("Location", "http://example.com/expected-redirect-url");
});
rmuller
  • 12,062
  • 4
  • 64
  • 92