HistoryTest.php
4.36 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace GuzzleHttp\Tests\Subscriber;
use GuzzleHttp\Transaction;
use GuzzleHttp\Client;
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Subscriber\History;
use GuzzleHttp\Subscriber\Mock;
/**
* @covers GuzzleHttp\Subscriber\History
*/
class HistoryTest extends \PHPUnit_Framework_TestCase
{
public function testAddsForErrorEvent()
{
$request = new Request('GET', '/');
$response = new Response(400);
$t = new Transaction(new Client(), $request);
$t->response = $response;
$e = new RequestException('foo', $request, $response);
$ev = new ErrorEvent($t, $e);
$h = new History(2);
$h->onError($ev);
// Only tracks when no response is present
$this->assertEquals([], $h->getRequests());
}
public function testLogsConnectionErrors()
{
$request = new Request('GET', '/');
$t = new Transaction(new Client(), $request);
$e = new RequestException('foo', $request);
$ev = new ErrorEvent($t, $e);
$h = new History();
$h->onError($ev);
$this->assertEquals([$request], $h->getRequests());
}
public function testMaintainsLimitValue()
{
$request = new Request('GET', '/');
$response = new Response(200);
$t = new Transaction(new Client(), $request);
$t->response = $response;
$ev = new CompleteEvent($t);
$h = new History(2);
$h->onComplete($ev);
$h->onComplete($ev);
$h->onComplete($ev);
$this->assertEquals(2, count($h));
$this->assertSame($request, $h->getLastRequest());
$this->assertSame($response, $h->getLastResponse());
foreach ($h as $trans) {
$this->assertInstanceOf('GuzzleHttp\Message\RequestInterface', $trans['request']);
$this->assertInstanceOf('GuzzleHttp\Message\ResponseInterface', $trans['response']);
}
return $h;
}
/**
* @depends testMaintainsLimitValue
*/
public function testClearsHistory($h)
{
$this->assertEquals(2, count($h));
$h->clear();
$this->assertEquals(0, count($h));
}
public function testWorksWithMock()
{
$client = new Client(['base_url' => 'http://localhost/']);
$h = new History();
$client->getEmitter()->attach($h);
$mock = new Mock([new Response(200), new Response(201), new Response(202)]);
$client->getEmitter()->attach($mock);
$request = $client->createRequest('GET', '/');
$client->send($request);
$request->setMethod('PUT');
$client->send($request);
$request->setMethod('POST');
$client->send($request);
$this->assertEquals(3, count($h));
$result = implode("\n", array_map(function ($line) {
return strpos($line, 'User-Agent') === 0
? 'User-Agent:'
: trim($line);
}, explode("\n", $h)));
$this->assertEquals("> GET / HTTP/1.1
Host: localhost
User-Agent:
< HTTP/1.1 200 OK
> PUT / HTTP/1.1
Host: localhost
User-Agent:
< HTTP/1.1 201 Created
> POST / HTTP/1.1
Host: localhost
User-Agent:
< HTTP/1.1 202 Accepted
", $result);
}
public function testCanCastToString()
{
$client = new Client(['base_url' => 'http://localhost/']);
$h = new History();
$client->getEmitter()->attach($h);
$mock = new Mock(array(
new Response(301, array('Location' => '/redirect1', 'Content-Length' => 0)),
new Response(307, array('Location' => '/redirect2', 'Content-Length' => 0)),
new Response(200, array('Content-Length' => '2'), Stream::factory('HI'))
));
$client->getEmitter()->attach($mock);
$request = $client->createRequest('GET', '/');
$client->send($request);
$this->assertEquals(3, count($h));
$h = str_replace("\r", '', $h);
$this->assertContains("> GET / HTTP/1.1\nHost: localhost\nUser-Agent:", $h);
$this->assertContains("< HTTP/1.1 301 Moved Permanently\nLocation: /redirect1", $h);
$this->assertContains("< HTTP/1.1 307 Temporary Redirect\nLocation: /redirect2", $h);
$this->assertContains("< HTTP/1.1 200 OK\nContent-Length: 2\n\nHI", $h);
}
}