Check response header's value in Postman tests
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");
});
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.