Wednesday, September 19, 2018

What would cause the Goutte driver to not follow a redirect?

Leave a Comment

I have a PHPUnit Mink test that is ensuring that some HTTP redirects are in place.

This is whittled down, but the test essentially looks like, where testRedirect() is fed by a @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));   }  } 

This works fine for the redirects that are handled on the webserver itself (eg, mod_rewrite ones). However, some of the redirects I need to check are handled by the DNS provider (I don't control this, but I think it is NetNames).

If I test the redirect with wget, it looks fine

$ 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. 

However, when I dump the response from my test the headers are

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

and the response is

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

with a 200 status code.

Do I need to explicitly set a request header? I tried

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

but that didn't help.

What can cause this?

1 Answers

Answers 1

This turned out to be a weird situation with the Host header, I think on the receiving end.

My test provider had some hostnames with uppercase characters in them, eg "http://Example1.com/". I had to update the test function to be

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

to force the Host header to be lowercase.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment