2

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?

MrPickles2009
  • 117
  • 4
  • 10
  • I'm not sure about your specific question but what status code do you get (or return since it's your website)? – Maayao Apr 08 '19 at 14:56
  • i'm getting status code 200s and 404s back because not all the pages are built yet. – MrPickles2009 Apr 08 '19 at 15:00
  • I'm redirecting a page like `example.com/test-page/test01` to `example.com/tests/test01` so regardless if the page exists or not I want to check if the url is correctly redirecting – MrPickles2009 Apr 08 '19 at 15:02
  • and when you redirect is the status code 302? can you try it on another website that redirects? – Maayao Apr 08 '19 at 15:05
  • @Maayao I am getting status code 301 (permanant redirects) back when debugging in chrome and preserving the log but in postman it only shows me status code 200 – MrPickles2009 Apr 08 '19 at 19:12

1 Answers1

1
  1. Switch off the setting Automatically follow redirects in Postman.
  2. Do a request to example.com/test-page/test01
  3. 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");
});
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37