3
namespace GuzzleHttp\Tests\Subscriber;
5
use GuzzleHttp\Adapter\Transaction;
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;
17
* @covers GuzzleHttp\Subscriber\History
19
class HistoryTest extends \PHPUnit_Framework_TestCase
21
public function testAddsForErrorEvent()
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);
31
// Only tracks when no response is present
32
$this->assertEquals([], $h->getRequests());
35
public function testLogsConnectionErrors()
37
$request = new Request('GET', '/');
38
$t = new Transaction(new Client(), $request);
39
$e = new RequestException('foo', $request);
40
$ev = new ErrorEvent($t, $e);
43
$this->assertEquals([$request], $h->getRequests());
46
public function testMaintainsLimitValue()
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);
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']);
68
* @depends testMaintainsLimitValue
70
public function testClearsHistory($h)
72
$this->assertEquals(2, count($h));
74
$this->assertEquals(0, count($h));
77
public function testCanCastToString()
79
$client = new Client(['base_url' => 'http://localhost/']);
81
$client->getEmitter()->attach($h);
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'))
89
$client->getEmitter()->attach($mock);
90
$request = $client->createRequest('GET', '/');
91
$client->send($request);
92
$this->assertEquals(3, count($h));
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);