scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace GuzzleHttp\Tests\Subscriber; |
||
4 | |||
5 | use GuzzleHttp\Adapter\Transaction; |
||
6 | use GuzzleHttp\Client; |
||
7 | use GuzzleHttp\Event\CompleteEvent; |
||
8 | use GuzzleHttp\Event\ErrorEvent; |
||
9 | use GuzzleHttp\Exception\RequestException; |
||
10 | use GuzzleHttp\Message\Request; |
||
11 | use GuzzleHttp\Message\Response; |
||
12 | use GuzzleHttp\Stream\Stream; |
||
13 | use GuzzleHttp\Subscriber\History; |
||
14 | use GuzzleHttp\Subscriber\Mock; |
||
15 | |||
16 | /** |
||
17 | * @covers GuzzleHttp\Subscriber\History |
||
18 | */ |
||
19 | class HistoryTest extends \PHPUnit_Framework_TestCase |
||
20 | { |
||
21 | public function testAddsForErrorEvent() |
||
22 | { |
||
23 | $request = new Request('GET', '/'); |
||
24 | $response = new Response(400); |
||
25 | $t = new Transaction(new Client(), $request); |
||
26 | $t->setResponse($response); |
||
27 | $e = new RequestException('foo', $request, $response); |
||
28 | $ev = new ErrorEvent($t, $e); |
||
29 | $h = new History(2); |
||
30 | $h->onError($ev); |
||
31 | // Only tracks when no response is present |
||
32 | $this->assertEquals([], $h->getRequests()); |
||
33 | } |
||
34 | |||
35 | public function testLogsConnectionErrors() |
||
36 | { |
||
37 | $request = new Request('GET', '/'); |
||
38 | $t = new Transaction(new Client(), $request); |
||
39 | $e = new RequestException('foo', $request); |
||
40 | $ev = new ErrorEvent($t, $e); |
||
41 | $h = new History(); |
||
42 | $h->onError($ev); |
||
43 | $this->assertEquals([$request], $h->getRequests()); |
||
44 | } |
||
45 | |||
46 | public function testMaintainsLimitValue() |
||
47 | { |
||
48 | $request = new Request('GET', '/'); |
||
49 | $response = new Response(200); |
||
50 | $t = new Transaction(new Client(), $request); |
||
51 | $t->setResponse($response); |
||
52 | $ev = new CompleteEvent($t); |
||
53 | $h = new History(2); |
||
54 | $h->onComplete($ev); |
||
55 | $h->onComplete($ev); |
||
56 | $h->onComplete($ev); |
||
57 | $this->assertEquals(2, count($h)); |
||
58 | $this->assertSame($request, $h->getLastRequest()); |
||
59 | $this->assertSame($response, $h->getLastResponse()); |
||
60 | foreach ($h as $trans) { |
||
61 | $this->assertInstanceOf('GuzzleHttp\Message\RequestInterface', $trans['request']); |
||
62 | $this->assertInstanceOf('GuzzleHttp\Message\ResponseInterface', $trans['response']); |
||
63 | } |
||
64 | return $h; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @depends testMaintainsLimitValue |
||
69 | */ |
||
70 | public function testClearsHistory($h) |
||
71 | { |
||
72 | $this->assertEquals(2, count($h)); |
||
73 | $h->clear(); |
||
74 | $this->assertEquals(0, count($h)); |
||
75 | } |
||
76 | |||
77 | public function testCanCastToString() |
||
78 | { |
||
79 | $client = new Client(['base_url' => 'http://localhost/']); |
||
80 | $h = new History(); |
||
81 | $client->getEmitter()->attach($h); |
||
82 | |||
83 | $mock = new Mock(array( |
||
84 | new Response(301, array('Location' => '/redirect1', 'Content-Length' => 0)), |
||
85 | new Response(307, array('Location' => '/redirect2', 'Content-Length' => 0)), |
||
86 | new Response(200, array('Content-Length' => '2'), Stream::factory('HI')) |
||
87 | )); |
||
88 | |||
89 | $client->getEmitter()->attach($mock); |
||
90 | $request = $client->createRequest('GET', '/'); |
||
91 | $client->send($request); |
||
92 | $this->assertEquals(3, count($h)); |
||
93 | |||
94 | $h = str_replace("\r", '', $h); |
||
95 | $this->assertContains("> GET / HTTP/1.1\nHost: localhost\nUser-Agent:", $h); |
||
96 | $this->assertContains("< HTTP/1.1 301 Moved Permanently\nLocation: /redirect1", $h); |
||
97 | $this->assertContains("< HTTP/1.1 307 Temporary Redirect\nLocation: /redirect2", $h); |
||
98 | $this->assertContains("< HTTP/1.1 200 OK\nContent-Length: 2\n\nHI", $h); |
||
99 | } |
||
100 | } |