I’m currently migrating my old website to a new one and I have just created some url redirect rules to redirect old links to their pages on the new website.
In order to test these redirect rules I’m using Postman but I can’t find any way of getting the redirected url from Postman’s scripts documentation. REDIRECTED_URL
is the url after being processed by the redirect rule.
Here is my current test:
var root_url = postman.getEnvironmentVariable('root_url'); var oldurl = root_url + postman.getEnvironmentVariable('old_page'); var newurl = root_url + postman.getEnvironmentVariable('new_page'); if (REDIRECTED_URL == newurl) { tests[oldurl + " redirected"] = true; } else { tests[oldurl + " failed to redirect"] = false; }
Is there a way to test this in postman or should I be using another application?
Advertisement
Answer
- Switch off the setting
Automatically follow redirects
in Postman. - Do a request to
example.com/test-page/test01
- In the test tab, check if the https status code and the re-direct header are correct:
pm.test("Status Code is 301", function () { pm.response.to.have.status(301); }); pm.test("Location-header exists", function () { pm.expect(postman.getResponseHeader("Location")).to.eq("example.com/tests/test01"); });