是什么导致Goutte驱动程序不遵循重定向的?

时间:2018-09-06 15:47:00

标签: phpunit mink

我有一个PHPUnit Mink测试,可以确保某些HTTP重定向到位。

这已经减少了,但是测试实质上看起来像是testRedirect()@dataProvider进行馈送:

class Testbase extends BrowserTestCase {

  public static $browsers = [
    [
      'driver' => 'goutte',
    ],
  ];

  public function testRedirect($from, $to) {
    $session = $this->getSession();
    $session->visit($from);

    $this->assertEquals(200, $session->getDriver()->getStatusCode(), sprintf('Final destination from %s was a 200', $to));
    $this->assertEquals($to, $session->getCurrentUrl(), sprintf('Redirected from %s to %s', $from, $to));
  }

}

这对于在Web服务器本身上处理的重定向(例如,mod_rewrite的重定向)工作正常。但是,我需要检查的某些重定向是由DNS提供程序处理的(我不控制此操作,但我认为它是NetNames)。

如果我使用wget测试重定向,就可以了

$ wget --max-redirect=0 http://example1.com/
Resolving example1.com... A.B.C.D
Connecting to example1.com|A.B.C.D|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://example2.com/some/path?foo=bar [following]
0 redirections exceeded.

但是,当我转储测试中的响应时,标题是

Date: Thu, 06 Sep 2018 15:37:47 GMT
Content-Length: 94
X-Powered-By: Servlet/2.4 JSP/2.0

响应是

<head>
<title></title>
<meta name="revised" content="1.1.7">
</head>
<body></body>

具有200状态代码。

我是否需要显式设置请求标头?我尝试过

$session->setRequestHeader('Host', 'example1.com');

但这没有帮助。

是什么原因造成的?

1 个答案:

答案 0 :(得分:1)

在接收端,我认为这与Host标头很奇怪。

我的测试提供者有一些主机名,其中包含大写字符,例如“ http://Example1.com/”。我不得不将测试功能更新为

public function testRedirect($from, $to) {
  $parts = parse_url($from);
  $host = strtolower($parts['host']);

  $session = $this->getSession();
  $session->setRequestHeader('Host', $host);
  $session->visit($from);

  $this->assertEquals(200, $session->getDriver()->getStatusCode(), sprintf('Final destination from %s was a 200', $to));
  $this->assertEquals($to, $session->getCurrentUrl(), sprintf('Redirected from %s to %s', $from, $to));
}

强制将Host标头转换为小写。

相关问题