CookieTest.php
2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace GuzzleHttp\Tests\Subscriber;
use GuzzleHttp\Transaction;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Subscriber\Cookie;
use GuzzleHttp\Subscriber\History;
use GuzzleHttp\Subscriber\Mock;
/**
* @covers GuzzleHttp\Subscriber\Cookie
*/
class CookieTest extends \PHPUnit_Framework_TestCase
{
public function testExtractsAndStoresCookies()
{
$request = new Request('GET', '/');
$response = new Response(200);
$mock = $this->getMockBuilder('GuzzleHttp\Cookie\CookieJar')
->setMethods(array('extractCookies'))
->getMock();
$mock->expects($this->exactly(1))
->method('extractCookies')
->with($request, $response);
$plugin = new Cookie($mock);
$t = new Transaction(new Client(), $request);
$t->response = $response;
$plugin->onComplete(new CompleteEvent($t));
}
public function testProvidesCookieJar()
{
$jar = new CookieJar();
$plugin = new Cookie($jar);
$this->assertSame($jar, $plugin->getCookieJar());
}
public function testCookiesAreExtractedFromRedirectResponses()
{
$jar = new CookieJar();
$cookie = new Cookie($jar);
$history = new History();
$mock = new Mock([
"HTTP/1.1 302 Moved Temporarily\r\n" .
"Set-Cookie: test=583551; Domain=www.foo.com; Expires=Wednesday, 23-Mar-2050 19:49:45 GMT; Path=/\r\n" .
"Location: /redirect\r\n\r\n",
"HTTP/1.1 200 OK\r\n" .
"Content-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\n" .
"Content-Length: 0\r\n\r\n"
]);
$client = new Client(['base_url' => 'http://www.foo.com']);
$client->getEmitter()->attach($cookie);
$client->getEmitter()->attach($mock);
$client->getEmitter()->attach($history);
$client->get();
$request = $client->createRequest('GET', '/');
$client->send($request);
$this->assertEquals('test=583551', $request->getHeader('Cookie'));
$requests = $history->getRequests();
// Confirm subsequent requests have the cookie.
$this->assertEquals('test=583551', $requests[2]->getHeader('Cookie'));
// Confirm the redirected request has the cookie.
$this->assertEquals('test=583551', $requests[1]->getHeader('Cookie'));
}
}