【问题标题】:Get redirected URL from Postman从 Postman 获取重定向的 URL
【发布时间】:2019-08-29 18:02:23
【问题描述】:

我目前正在将旧网站迁移到新网站,并且我刚刚创建了一些 url 重定向规则来将旧链接重定向到新网站上的页面。

为了测试这些重定向规则,我正在使用 Postman,但我找不到任何从 Postman 的 scripts documentation 获取重定向 url 的方法。 REDIRECTED_URL是重定向规则处理后的url。

这是我目前的测试:

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

有没有办法在邮递员中对此进行测试,还是应该使用其他应用程序?

【问题讨论】:

  • 我不确定你的具体问题,但你得到什么状态码(或返回,因为它是你的网站)?
  • 我收到了状态码 200 和 404,因为尚未构建所有页面。
  • 我正在将example.com/test-page/test01 之类的页面重定向到example.com/tests/test01,所以无论页面是否存在,我都想检查网址是否正确重定向
  • 当你重定向的时候是状态码 302?你能在另一个重定向的网站上试试吗?
  • @Maayao 我在 chrome 中调试并保留日志时返回状态代码 301(永久重定向),但在邮递员中它只显示状态代码 200

标签: javascript postman url-redirection postman-pre-request-script postman-testcase


【解决方案1】:
  1. 在 Postman 中关闭 Automatically follow redirects 设置。
  2. example.com/test-page/test01发送请求
  3. 在测试选项卡中,检查https状态码和重定向标头是否正确:
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");
});

【讨论】:

  • 完美!太感谢了。 “自动跟随重定向”设置为“开启”是导致问题的原因
猜你喜欢
  • 2013-02-24
  • 2015-07-09
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 2021-09-15
  • 2021-03-28
  • 2013-06-06
  • 1970-01-01
相关资源
最近更新 更多