Backup of database 9 Nov 17
[yaffs-website] / vendor / fabpot / goutte / Goutte / Tests / ClientTest.php
1 <?php
2
3 /*
4  * This file is part of the Goutte package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Goutte\Tests;
13
14 use Goutte\Client;
15 use GuzzleHttp\Client as GuzzleClient;
16 use GuzzleHttp\Exception\RequestException;
17 use GuzzleHttp\Handler\MockHandler;
18 use GuzzleHttp\HandlerStack;
19 use GuzzleHttp\Psr7\Response as GuzzleResponse;
20 use GuzzleHttp\Middleware;
21 use Symfony\Component\BrowserKit\Cookie;
22
23 /**
24  * Goutte Client Test.
25  *
26  * @author Michael Dowling <michael@guzzlephp.org>
27  * @author Charles Sarrazin <charles@sarraz.in>
28  */
29 class ClientTest extends \PHPUnit_Framework_TestCase
30 {
31     protected $history;
32     /** @var MockHandler */
33     protected $mock;
34
35     protected function getGuzzle(array $responses = [])
36     {
37         if (empty($responses)) {
38             $responses = [new GuzzleResponse(200, [], '<html><body><p>Hi</p></body></html>')];
39         }
40         $this->mock = new MockHandler($responses);
41         $handlerStack = HandlerStack::create($this->mock);
42         $this->history = [];
43         $handlerStack->push(Middleware::history($this->history));
44         $guzzle = new GuzzleClient(array('redirect.disable' => true, 'base_uri' => '', 'handler' => $handlerStack));
45
46         return $guzzle;
47     }
48
49     public function testCreatesDefaultClient()
50     {
51         $client = new Client();
52         $this->assertInstanceOf('GuzzleHttp\\ClientInterface', $client->getClient());
53     }
54
55     public function testUsesCustomClient()
56     {
57         $guzzle = new GuzzleClient();
58         $client = new Client();
59         $this->assertSame($client, $client->setClient($guzzle));
60         $this->assertSame($guzzle, $client->getClient());
61     }
62
63     public function testUsesCustomHeaders()
64     {
65         $guzzle = $this->getGuzzle();
66         $client = new Client();
67         $client->setClient($guzzle);
68         $client->setHeader('X-Test', 'test');
69         $client->request('GET', 'http://www.example.com/');
70         $this->assertEquals('test', end($this->history)['request']->getHeaderLine('X-Test'));
71     }
72
73     public function testCustomUserAgent()
74     {
75         $guzzle = $this->getGuzzle();
76         $client = new Client();
77         $client->setClient($guzzle);
78         $client->setHeader('User-Agent', 'foo');
79         $client->request('GET', 'http://www.example.com/');
80         $this->assertEquals('foo', end($this->history)['request']->getHeaderLine('User-Agent'));
81     }
82
83     public function testUsesAuth()
84     {
85         $guzzle = $this->getGuzzle();
86         $client = new Client();
87         $client->setClient($guzzle);
88         $client->setAuth('me', '**');
89         $client->request('GET', 'http://www.example.com/');
90         $request = end($this->history)['request'];
91         $this->assertEquals('Basic bWU6Kio=', $request->getHeaderLine('Authorization'));
92     }
93
94     public function testResetsAuth()
95     {
96         $guzzle = $this->getGuzzle();
97         $client = new Client();
98         $client->setClient($guzzle);
99         $client->setAuth('me', '**');
100         $client->resetAuth();
101         $client->request('GET', 'http://www.example.com/');
102         $request = end($this->history)['request'];
103         $this->assertEquals('', $request->getHeaderLine('authorization'));
104     }
105
106     public function testUsesCookies()
107     {
108         $guzzle = $this->getGuzzle();
109         $client = new Client();
110         $client->setClient($guzzle);
111         $client->getCookieJar()->set(new Cookie('test', '123'));
112         $client->request('GET', 'http://www.example.com/');
113         $request = end($this->history)['request'];
114         $this->assertEquals('test=123', $request->getHeaderLine('Cookie'));
115     }
116
117     public function testUsesCookiesWithCustomPort()
118     {
119         $guzzle = $this->getGuzzle();
120         $client = new Client();
121         $client->setClient($guzzle);
122         $client->getCookieJar()->set(new Cookie('test', '123'));
123         $client->request('GET', 'http://www.example.com:8000/');
124         $request = end($this->history)['request'];
125         $this->assertEquals('test=123', $request->getHeaderLine('Cookie'));
126     }
127
128     public function testUsesPostFiles()
129     {
130         $guzzle = $this->getGuzzle();
131         $client = new Client();
132         $client->setClient($guzzle);
133         $files = array(
134             'test' => array(
135                 'name' => 'test.txt',
136                 'tmp_name' => __DIR__.'/fixtures.txt',
137             ),
138         );
139
140         $client->request('POST', 'http://www.example.com/', array(), $files);
141         $request = end($this->history)['request'];
142
143         $stream = $request->getBody();
144         $boundary = $stream->getBoundary();
145         $this->assertEquals(
146             "--$boundary\r\nContent-Disposition: form-data; name=\"test\"; filename=\"test.txt\"\r\nContent-Length: 4\r\n"
147             ."Content-Type: text/plain\r\n\r\nfoo\n\r\n--$boundary--\r\n",
148             $stream->getContents()
149         );
150     }
151
152     public function testUsesPostNamedFiles()
153     {
154         $guzzle = $this->getGuzzle();
155         $client = new Client();
156         $client->setClient($guzzle);
157         $files = array(
158             'test' => __DIR__.'/fixtures.txt',
159         );
160
161         $client->request('POST', 'http://www.example.com/', array(), $files);
162         $request = end($this->history)['request'];
163
164         $stream = $request->getBody();
165         $boundary = $stream->getBoundary();
166         $this->assertEquals(
167             "--$boundary\r\nContent-Disposition: form-data; name=\"test\"; filename=\"fixtures.txt\"\r\nContent-Length: 4\r\n"
168             ."Content-Type: text/plain\r\n\r\nfoo\n\r\n--$boundary--\r\n",
169             $stream->getContents()
170         );
171     }
172
173     public function testUsesPostFilesNestedFields()
174     {
175         $guzzle = $this->getGuzzle();
176         $client = new Client();
177         $client->setClient($guzzle);
178         $files = array(
179             'form' => array(
180                 'test' => array(
181                     'name' => 'test.txt',
182                     'tmp_name' => __DIR__.'/fixtures.txt',
183                 ),
184             ),
185         );
186
187         $client->request('POST', 'http://www.example.com/', array(), $files);
188         $request = end($this->history)['request'];
189
190         $stream = $request->getBody();
191         $boundary = $stream->getBoundary();
192         $this->assertEquals(
193             "--$boundary\r\nContent-Disposition: form-data; name=\"form[test]\"; filename=\"test.txt\"\r\nContent-Length: 4\r\n"
194             ."Content-Type: text/plain\r\n\r\nfoo\n\r\n--$boundary--\r\n",
195             $stream->getContents()
196         );
197     }
198
199     public function testPostFormWithFiles()
200     {
201         $guzzle = $this->getGuzzle();
202         $client = new Client();
203         $client->setClient($guzzle);
204         $files = array(
205             'test' => __DIR__.'/fixtures.txt',
206         );
207         $params = array(
208             'foo' => 'bar',
209         );
210
211         $client->request('POST', 'http://www.example.com/', $params, $files);
212         $request = end($this->history)['request'];
213
214         $stream = $request->getBody();
215         $boundary = $stream->getBoundary();
216         $this->assertEquals(
217             "--$boundary\r\nContent-Disposition: form-data; name=\"foo\"\r\nContent-Length: 3\r\n"
218             ."\r\nbar\r\n"
219             ."--$boundary\r\nContent-Disposition: form-data; name=\"test\"; filename=\"fixtures.txt\"\r\nContent-Length: 4\r\n"
220             ."Content-Type: text/plain\r\n\r\nfoo\n\r\n--$boundary--\r\n",
221         $stream->getContents());
222     }
223
224     public function testPostEmbeddedFormWithFiles()
225     {
226         $guzzle = $this->getGuzzle();
227         $client = new Client();
228         $client->setClient($guzzle);
229         $files = array(
230             'test' => __DIR__.'/fixtures.txt',
231         );
232         $params = array(
233             'foo' => array(
234                 'bar' => 'baz',
235             ),
236         );
237
238         $client->request('POST', 'http://www.example.com/', $params, $files);
239         $request = end($this->history)['request'];
240
241         $stream = $request->getBody();
242         $boundary = $stream->getBoundary();
243         $this->assertEquals(
244             "--$boundary\r\nContent-Disposition: form-data; name=\"foo[bar]\"\r\nContent-Length: 3\r\n"
245             ."\r\nbaz\r\n"
246             ."--$boundary\r\nContent-Disposition: form-data; name=\"test\"; filename=\"fixtures.txt\"\r\nContent-Length: 4\r\n"
247             ."Content-Type: text/plain\r\n\r\nfoo\n\r\n--$boundary--\r\n",
248         $stream->getContents());
249     }
250
251     public function testUsesPostFilesOnClientSide()
252     {
253         $guzzle = $this->getGuzzle();
254         $client = new Client();
255         $client->setClient($guzzle);
256         $files = array(
257             'test' => __DIR__.'/fixtures.txt',
258         );
259
260         $client->request('POST', 'http://www.example.com/', array(), $files);
261         $request = end($this->history)['request'];
262
263         $stream = $request->getBody();
264         $boundary = $stream->getBoundary();
265         $this->assertEquals(
266             "--$boundary\r\nContent-Disposition: form-data; name=\"test\"; filename=\"fixtures.txt\"\r\nContent-Length: 4\r\n"
267             ."Content-Type: text/plain\r\n\r\nfoo\n\r\n--$boundary--\r\n",
268             $stream->getContents()
269         );
270     }
271
272     public function testUsesPostFilesUploadError()
273     {
274         $guzzle = $this->getGuzzle();
275         $client = new Client();
276         $client->setClient($guzzle);
277         $files = array(
278             'test' => array(
279                 'name' => '',
280                 'type' => '',
281                 'tmp_name' => '',
282                 'error' => 4,
283                 'size' => 0,
284             ),
285         );
286
287         $client->request('POST', 'http://www.example.com/', array(), $files);
288         $request = end($this->history)['request'];
289         $stream = $request->getBody();
290         $boundary = $stream->getBoundary();
291
292         $this->assertEquals("--$boundary--\r\n", $stream->getContents());
293     }
294
295     public function testCreatesResponse()
296     {
297         $guzzle = $this->getGuzzle();
298         $client = new Client();
299         $client->setClient($guzzle);
300         $crawler = $client->request('GET', 'http://www.example.com/');
301         $this->assertEquals('Hi', $crawler->filter('p')->text());
302     }
303
304     public function testHandlesRedirectsCorrectly()
305     {
306         $guzzle = $this->getGuzzle([
307             new GuzzleResponse(301, array(
308                 'Location' => 'http://www.example.com/',
309             )),
310             new GuzzleResponse(200, [], '<html><body><p>Test</p></body></html>'),
311         ]);
312
313         $client = new Client();
314         $client->setClient($guzzle);
315
316         $crawler = $client->request('GET', 'http://www.example.com/');
317         $this->assertEquals('Test', $crawler->filter('p')->text());
318
319         // Ensure that two requests were sent
320         $this->assertEquals(2, count($this->history));
321     }
322
323     public function testConvertsGuzzleHeadersToArrays()
324     {
325         $guzzle = $this->getGuzzle([
326             new GuzzleResponse(200, array(
327                 'Date' => 'Tue, 04 Jun 2013 13:22:41 GMT',
328             )),
329         ]);
330
331         $client = new Client();
332         $client->setClient($guzzle);
333         $client->request('GET', 'http://www.example.com/');
334         $response = $client->getResponse();
335         $headers = $response->getHeaders();
336
337         $this->assertInternalType('array', array_shift($headers), 'Header not converted from Guzzle\Http\Message\Header to array');
338     }
339
340     public function testNullResponseException()
341     {
342         $this->setExpectedException('GuzzleHttp\Exception\RequestException');
343         $guzzle = $this->getGuzzle([
344             new RequestException('', $this->getMock('Psr\Http\Message\RequestInterface')),
345         ]);
346         $client = new Client();
347         $client->setClient($guzzle);
348         $client->request('GET', 'http://www.example.com/');
349         $client->getResponse();
350     }
351
352     public function testHttps()
353     {
354         $guzzle = $this->getGuzzle([
355             new GuzzleResponse(200, [], '<html><body><p>Test</p></body></html>'),
356         ]);
357
358         $client = new Client();
359         $client->setClient($guzzle);
360         $crawler = $client->request('GET', 'https://www.example.com/');
361         $this->assertEquals('Test', $crawler->filter('p')->text());
362     }
363
364     public function testCustomUserAgentConstructor()
365     {
366         $guzzle = $this->getGuzzle();
367         $client = new Client([
368           'HTTP_HOST' => '1.2.3.4',
369           'HTTP_USER_AGENT' => 'SomeHost',
370         ]);
371         $client->setClient($guzzle);
372         $client->request('GET', 'http://www.example.com/');
373         $this->assertEquals('SomeHost', end($this->history)['request']->getHeaderLine('User-Agent'));
374     }
375
376     public function testResetHeaders()
377     {
378         $client = new Client();
379         $client->setHeader('X-Test', 'test');
380
381         $reflectionProperty = new \ReflectionProperty('Goutte\Client', 'headers');
382         $reflectionProperty->setAccessible(true);
383         $this->assertEquals(array('x-test' => 'test'), $reflectionProperty->getValue($client));
384
385         $client->resetHeaders();
386         $this->assertEquals([], $reflectionProperty->getValue($client));
387     }
388
389     public function testRestart()
390     {
391         $client = new Client();
392         $client->setHeader('X-Test', 'test');
393         $client->setAuth('foo', 'bar');
394
395         $headersReflectionProperty = new \ReflectionProperty('Goutte\Client', 'headers');
396         $headersReflectionProperty->setAccessible(true);
397         $this->assertEquals(array('x-test' => 'test'), $headersReflectionProperty->getValue($client));
398
399         $authReflectionProperty = new \ReflectionProperty('Goutte\Client', 'auth');
400         $authReflectionProperty->setAccessible(true);
401         $this->assertEquals(array('foo', 'bar', 'basic'), $authReflectionProperty->getValue($client));
402
403         $client->restart();
404         $this->assertEquals([], $headersReflectionProperty->getValue($client));
405         $this->assertNull($authReflectionProperty->getValue($client));
406     }
407 }