Lluís Suñol
Lluís Suñol

Reputation: 3651

Check response header's value in Postman tests

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;
});

Upvotes: 43

Views: 44056

Answers (6)

rmuller
rmuller

Reputation: 12859

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");
});

Upvotes: 0

Mirza Sisic
Mirza Sisic

Reputation: 2429

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.

Upvotes: 0

Vallabha Vamaravelli
Vallabha Vamaravelli

Reputation: 1293

pm.test("Location value is correct", function () {
   pm.expect(pm.response.headers.get('Location')).to.eql('http://google.com');
});

Upvotes: 0

leonidv
leonidv

Reputation: 1412

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
})

Upvotes: 1

Irfan Makandar
Irfan Makandar

Reputation: 221

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.

Upvotes: 22

Lluís Suñol
Lluís Suñol

Reputation: 3651

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");
});

Upvotes: 98

Related Questions