2015-08-17 17:00:26 -07:00
< ? php
/*
* This file is part of the Goutte package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Goutte\Tests ;
use Goutte\Client ;
use GuzzleHttp\Client as GuzzleClient ;
use GuzzleHttp\Exception\RequestException ;
2015-08-27 12:03:05 -07:00
use GuzzleHttp\Handler\MockHandler ;
use GuzzleHttp\HandlerStack ;
use GuzzleHttp\Psr7\Response as GuzzleResponse ;
use GuzzleHttp\Middleware ;
2015-08-17 17:00:26 -07:00
use Symfony\Component\BrowserKit\Cookie ;
/**
2015-08-27 12:03:05 -07:00
* Goutte Client Test .
2015-08-17 17:00:26 -07:00
*
* @ author Michael Dowling < michael @ guzzlephp . org >
2015-08-27 12:03:05 -07:00
* @ author Charles Sarrazin < charles @ sarraz . in >
2015-08-17 17:00:26 -07:00
*/
class ClientTest extends \PHPUnit_Framework_TestCase
{
protected $history ;
2015-08-27 12:03:05 -07:00
/** @var MockHandler */
2015-08-17 17:00:26 -07:00
protected $mock ;
2015-08-27 12:03:05 -07:00
protected function getGuzzle ( array $responses = [])
2015-08-17 17:00:26 -07:00
{
2015-08-27 12:03:05 -07:00
if ( empty ( $responses )) {
$responses = [ new GuzzleResponse ( 200 , [], '<html><body><p>Hi</p></body></html>' )];
}
$this -> mock = new MockHandler ( $responses );
$handlerStack = HandlerStack :: create ( $this -> mock );
$this -> history = [];
$handlerStack -> push ( Middleware :: history ( $this -> history ));
$guzzle = new GuzzleClient ( array ( 'redirect.disable' => true , 'base_uri' => '' , 'handler' => $handlerStack ));
2015-08-17 17:00:26 -07:00
return $guzzle ;
}
public function testCreatesDefaultClient ()
{
$client = new Client ();
$this -> assertInstanceOf ( 'GuzzleHttp\\ClientInterface' , $client -> getClient ());
}
public function testUsesCustomClient ()
{
$guzzle = new GuzzleClient ();
$client = new Client ();
$this -> assertSame ( $client , $client -> setClient ( $guzzle ));
$this -> assertSame ( $guzzle , $client -> getClient ());
}
public function testUsesCustomHeaders ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$client -> setHeader ( 'X-Test' , 'test' );
2015-08-27 12:03:05 -07:00
$client -> request ( 'GET' , 'http://www.example.com/' );
$this -> assertEquals ( 'test' , end ( $this -> history )[ 'request' ] -> getHeaderLine ( 'X-Test' ));
2015-08-17 17:00:26 -07:00
}
public function testCustomUserAgent ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$client -> setHeader ( 'User-Agent' , 'foo' );
2015-08-27 12:03:05 -07:00
$client -> request ( 'GET' , 'http://www.example.com/' );
$this -> assertEquals ( 'Symfony2 BrowserKit, foo' , end ( $this -> history )[ 'request' ] -> getHeaderLine ( 'User-Agent' ));
2015-08-17 17:00:26 -07:00
}
public function testUsesAuth ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$client -> setAuth ( 'me' , '**' );
2015-08-27 12:03:05 -07:00
$client -> request ( 'GET' , 'http://www.example.com/' );
$request = end ( $this -> history )[ 'request' ];
$this -> assertEquals ( 'Basic bWU6Kio=' , $request -> getHeaderLine ( 'Authorization' ));
2015-08-17 17:00:26 -07:00
}
public function testResetsAuth ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$client -> setAuth ( 'me' , '**' );
$client -> resetAuth ();
2015-08-27 12:03:05 -07:00
$client -> request ( 'GET' , 'http://www.example.com/' );
$request = end ( $this -> history )[ 'request' ];
$this -> assertEquals ( '' , $request -> getHeaderLine ( 'authorization' ));
2015-08-17 17:00:26 -07:00
}
public function testUsesCookies ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$client -> getCookieJar () -> set ( new Cookie ( 'test' , '123' ));
2015-08-27 12:03:05 -07:00
$client -> request ( 'GET' , 'http://www.example.com/' );
$request = end ( $this -> history )[ 'request' ];
$this -> assertEquals ( 'test=123' , $request -> getHeaderLine ( 'Cookie' ));
2015-08-17 17:00:26 -07:00
}
2015-10-08 11:40:12 -07:00
public function testUsesCookiesWithCustomPort ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$client -> getCookieJar () -> set ( new Cookie ( 'test' , '123' ));
$client -> request ( 'GET' , 'http://www.example.com:8000/' );
$request = end ( $this -> history )[ 'request' ];
$this -> assertEquals ( 'test=123' , $request -> getHeaderLine ( 'Cookie' ));
}
2015-08-17 17:00:26 -07:00
public function testUsesPostFiles ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$files = array (
'test' => array (
'name' => 'test.txt' ,
2015-08-27 12:03:05 -07:00
'tmp_name' => __DIR__ . '/fixtures.txt' ,
2015-08-17 17:00:26 -07:00
),
);
2015-08-27 12:03:05 -07:00
$client -> request ( 'POST' , 'http://www.example.com/' , array (), $files );
$request = end ( $this -> history )[ 'request' ];
2015-08-17 17:00:26 -07:00
2015-08-27 12:03:05 -07:00
$stream = $request -> getBody ();
$boundary = $stream -> getBoundary ();
$this -> assertEquals (
" -- $boundary\r\nContent -Disposition: form-data; name= \" test \" ; filename= \" test.txt \" \r \n Content-Length: 4 \r \n "
. " Content-Type: text/plain \r \n \r \n foo \n \r \n -- $boundary -- \r \n " ,
$stream -> getContents ()
);
2015-08-17 17:00:26 -07:00
}
public function testUsesPostNamedFiles ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$files = array (
2015-08-27 12:03:05 -07:00
'test' => __DIR__ . '/fixtures.txt' ,
2015-08-17 17:00:26 -07:00
);
2015-08-27 12:03:05 -07:00
$client -> request ( 'POST' , 'http://www.example.com/' , array (), $files );
$request = end ( $this -> history )[ 'request' ];
$stream = $request -> getBody ();
$boundary = $stream -> getBoundary ();
$this -> assertEquals (
" -- $boundary\r\nContent -Disposition: form-data; name= \" test \" ; filename= \" fixtures.txt \" \r \n Content-Length: 4 \r \n "
. " Content-Type: text/plain \r \n \r \n foo \n \r \n -- $boundary -- \r \n " ,
$stream -> getContents ()
);
2015-08-17 17:00:26 -07:00
}
public function testUsesPostFilesNestedFields ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$files = array (
'form' => array (
'test' => array (
'name' => 'test.txt' ,
2015-08-27 12:03:05 -07:00
'tmp_name' => __DIR__ . '/fixtures.txt' ,
2015-08-17 17:00:26 -07:00
),
),
);
2015-08-27 12:03:05 -07:00
$client -> request ( 'POST' , 'http://www.example.com/' , array (), $files );
$request = end ( $this -> history )[ 'request' ];
$stream = $request -> getBody ();
$boundary = $stream -> getBoundary ();
$this -> assertEquals (
" -- $boundary\r\nContent -Disposition: form-data; name= \" form[test] \" ; filename= \" test.txt \" \r \n Content-Length: 4 \r \n "
. " Content-Type: text/plain \r \n \r \n foo \n \r \n -- $boundary -- \r \n " ,
$stream -> getContents ()
);
}
public function testPostFormWithFiles ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$files = array (
'test' => __DIR__ . '/fixtures.txt' ,
);
$params = array (
'foo' => 'bar' ,
);
$client -> request ( 'POST' , 'http://www.example.com/' , $params , $files );
$request = end ( $this -> history )[ 'request' ];
$stream = $request -> getBody ();
$boundary = $stream -> getBoundary ();
$this -> assertEquals (
" -- $boundary\r\nContent -Disposition: form-data; name= \" foo \" \r \n Content-Length: 3 \r \n "
. " \r \n bar \r \n "
. " -- $boundary\r\nContent -Disposition: form-data; name= \" test \" ; filename= \" fixtures.txt \" \r \n Content-Length: 4 \r \n "
. " Content-Type: text/plain \r \n \r \n foo \n \r \n -- $boundary -- \r \n " ,
$stream -> getContents ());
}
public function testPostEmbeddedFormWithFiles ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$files = array (
'test' => __DIR__ . '/fixtures.txt' ,
);
$params = array (
'foo' => array (
'bar' => 'baz' ,
),
);
$client -> request ( 'POST' , 'http://www.example.com/' , $params , $files );
$request = end ( $this -> history )[ 'request' ];
$stream = $request -> getBody ();
$boundary = $stream -> getBoundary ();
$this -> assertEquals (
" -- $boundary\r\nContent -Disposition: form-data; name= \" foo[bar] \" \r \n Content-Length: 3 \r \n "
. " \r \n baz \r \n "
. " -- $boundary\r\nContent -Disposition: form-data; name= \" test \" ; filename= \" fixtures.txt \" \r \n Content-Length: 4 \r \n "
. " Content-Type: text/plain \r \n \r \n foo \n \r \n -- $boundary -- \r \n " ,
$stream -> getContents ());
2015-08-17 17:00:26 -07:00
}
public function testUsesPostFilesOnClientSide ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$files = array (
2015-08-27 12:03:05 -07:00
'test' => __DIR__ . '/fixtures.txt' ,
2015-08-17 17:00:26 -07:00
);
2015-08-27 12:03:05 -07:00
$client -> request ( 'POST' , 'http://www.example.com/' , array (), $files );
$request = end ( $this -> history )[ 'request' ];
$stream = $request -> getBody ();
$boundary = $stream -> getBoundary ();
$this -> assertEquals (
" -- $boundary\r\nContent -Disposition: form-data; name= \" test \" ; filename= \" fixtures.txt \" \r \n Content-Length: 4 \r \n "
. " Content-Type: text/plain \r \n \r \n foo \n \r \n -- $boundary -- \r \n " ,
$stream -> getContents ()
);
2015-08-17 17:00:26 -07:00
}
public function testUsesPostFilesUploadError ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$files = array (
'test' => array (
'name' => '' ,
'type' => '' ,
'tmp_name' => '' ,
'error' => 4 ,
'size' => 0 ,
),
);
2015-08-27 12:03:05 -07:00
$client -> request ( 'POST' , 'http://www.example.com/' , array (), $files );
$request = end ( $this -> history )[ 'request' ];
$stream = $request -> getBody ();
$boundary = $stream -> getBoundary ();
2015-08-17 17:00:26 -07:00
2015-08-27 12:03:05 -07:00
$this -> assertEquals ( " -- $boundary -- \r \n " , $stream -> getContents ());
2015-08-17 17:00:26 -07:00
}
public function testCreatesResponse ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ();
$client -> setClient ( $guzzle );
$crawler = $client -> request ( 'GET' , 'http://www.example.com/' );
$this -> assertEquals ( 'Hi' , $crawler -> filter ( 'p' ) -> text ());
}
public function testHandlesRedirectsCorrectly ()
{
2015-08-27 12:03:05 -07:00
$guzzle = $this -> getGuzzle ([
new GuzzleResponse ( 301 , array (
'Location' => 'http://www.example.com/' ,
)),
new GuzzleResponse ( 200 , [], '<html><body><p>Test</p></body></html>' ),
]);
2015-08-17 17:00:26 -07:00
$client = new Client ();
$client -> setClient ( $guzzle );
$crawler = $client -> request ( 'GET' , 'http://www.example.com/' );
$this -> assertEquals ( 'Test' , $crawler -> filter ( 'p' ) -> text ());
// Ensure that two requests were sent
$this -> assertEquals ( 2 , count ( $this -> history ));
}
public function testConvertsGuzzleHeadersToArrays ()
{
2015-08-27 12:03:05 -07:00
$guzzle = $this -> getGuzzle ([
new GuzzleResponse ( 200 , array (
'Date' => 'Tue, 04 Jun 2013 13:22:41 GMT' ,
)),
]);
2015-08-17 17:00:26 -07:00
$client = new Client ();
$client -> setClient ( $guzzle );
$client -> request ( 'GET' , 'http://www.example.com/' );
$response = $client -> getResponse ();
$headers = $response -> getHeaders ();
2015-08-27 12:03:05 -07:00
$this -> assertInternalType ( 'array' , array_shift ( $headers ), 'Header not converted from Guzzle\Http\Message\Header to array' );
2015-08-17 17:00:26 -07:00
}
public function testNullResponseException ()
{
$this -> setExpectedException ( 'GuzzleHttp\Exception\RequestException' );
2015-08-27 12:03:05 -07:00
$guzzle = $this -> getGuzzle ([
new RequestException ( '' , $this -> getMock ( 'Psr\Http\Message\RequestInterface' )),
]);
2015-08-17 17:00:26 -07:00
$client = new Client ();
$client -> setClient ( $guzzle );
$client -> request ( 'GET' , 'http://www.example.com/' );
2015-08-27 12:03:05 -07:00
$client -> getResponse ();
2015-08-17 17:00:26 -07:00
}
public function testHttps ()
{
2015-08-27 12:03:05 -07:00
$guzzle = $this -> getGuzzle ([
new GuzzleResponse ( 200 , [], '<html><body><p>Test</p></body></html>' ),
]);
2015-08-17 17:00:26 -07:00
$client = new Client ();
$client -> setClient ( $guzzle );
$crawler = $client -> request ( 'GET' , 'https://www.example.com/' );
$this -> assertEquals ( 'Test' , $crawler -> filter ( 'p' ) -> text ());
}
public function testCustomUserAgentConstructor ()
{
$guzzle = $this -> getGuzzle ();
$client = new Client ([
'HTTP_HOST' => '1.2.3.4' ,
'HTTP_USER_AGENT' => 'SomeHost' ,
]);
$client -> setClient ( $guzzle );
2015-08-27 12:03:05 -07:00
$client -> request ( 'GET' , 'http://www.example.com/' );
$this -> assertEquals ( 'SomeHost' , end ( $this -> history )[ 'request' ] -> getHeaderLine ( 'User-Agent' ));
2015-08-17 17:00:26 -07:00
}
}