scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Tests\Adapter\Curl;
4  
5 use GuzzleHttp\Adapter\Curl\MultiAdapter;
6 use GuzzleHttp\Adapter\Curl\RequestMediator;
7 use GuzzleHttp\Adapter\Transaction;
8 use GuzzleHttp\Client;
9 use GuzzleHttp\Event\HeadersEvent;
10 use GuzzleHttp\Message\MessageFactory;
11 use GuzzleHttp\Message\Request;
12 use GuzzleHttp\Message\Response;
13 use GuzzleHttp\Stream\Stream;
14 use GuzzleHttp\Tests\Server;
15  
16 /**
17 * @covers GuzzleHttp\Adapter\Curl\RequestMediator
18 */
19 class RequestMediatorTest extends \PHPUnit_Framework_TestCase
20 {
21 public function testSetsResponseBodyForDownload()
22 {
23 $body = Stream::factory();
24 $request = new Request('GET', 'http://httbin.org');
25 $ee = null;
26 $request->getEmitter()->on(
27 'headers',
28 function (HeadersEvent $e) use (&$ee) {
29 $ee = $e;
30 }
31 );
32 $t = new Transaction(new Client(), $request);
33 $m = new RequestMediator($t, new MessageFactory());
34 $m->setResponseBody($body);
35 $this->assertEquals(18, $m->receiveResponseHeader(null, "HTTP/1.1 202 FOO\r\n"));
36 $this->assertEquals(10, $m->receiveResponseHeader(null, "Foo: Bar\r\n"));
37 $this->assertEquals(11, $m->receiveResponseHeader(null, "Baz : Bam\r\n"));
38 $this->assertEquals(19, $m->receiveResponseHeader(null, "Content-Length: 3\r\n"));
39 $this->assertEquals(2, $m->receiveResponseHeader(null, "\r\n"));
40 $this->assertNotNull($ee);
41 $this->assertEquals(202, $t->getResponse()->getStatusCode());
42 $this->assertEquals('FOO', $t->getResponse()->getReasonPhrase());
43 $this->assertEquals('Bar', $t->getResponse()->getHeader('Foo'));
44 $this->assertEquals('Bam', $t->getResponse()->getHeader('Baz'));
45 $m->writeResponseBody(null, 'foo');
46 $this->assertEquals('foo', (string) $body);
47 $this->assertEquals('3', $t->getResponse()->getHeader('Content-Length'));
48 }
49  
50 public function testSendsToNewBodyWhenNot2xxResponse()
51 {
52 $body = Stream::factory();
53 $request = new Request('GET', 'http://httbin.org');
54 $t = new Transaction(new Client(), $request);
55 $m = new RequestMediator($t, new MessageFactory());
56 $m->setResponseBody($body);
57 $this->assertEquals(27, $m->receiveResponseHeader(null, "HTTP/1.1 304 Not Modified\r\n"));
58 $this->assertEquals(2, $m->receiveResponseHeader(null, "\r\n"));
59 $this->assertEquals(304, $t->getResponse()->getStatusCode());
60 $m->writeResponseBody(null, 'foo');
61 $this->assertEquals('', (string) $body);
62 $this->assertEquals('foo', (string) $t->getResponse()->getBody());
63 }
64  
65 public function testUsesDefaultBodyIfNoneSet()
66 {
67 $t = new Transaction(new Client(), new Request('GET', 'http://httbin.org'));
68 $t->setResponse(new Response(200));
69 $m = new RequestMediator($t, new MessageFactory());
70 $this->assertEquals(3, $m->writeResponseBody(null, 'foo'));
71 $this->assertEquals('foo', (string) $t->getResponse()->getBody());
72 }
73  
74 public function testCanUseResponseBody()
75 {
76 $body = Stream::factory();
77 $t = new Transaction(new Client(), new Request('GET', 'http://httbin.org'));
78 $t->setResponse(new Response(200, [], $body));
79 $m = new RequestMediator($t, new MessageFactory());
80 $this->assertEquals(3, $m->writeResponseBody(null, 'foo'));
81 $this->assertEquals('foo', (string) $body);
82 }
83  
84 public function testHandlesTransactionWithNoResponseWhenWritingBody()
85 {
86 $t = new Transaction(new Client(), new Request('GET', 'http://httbin.org'));
87 $m = new RequestMediator($t, new MessageFactory());
88 $this->assertEquals(0, $m->writeResponseBody(null, 'test'));
89 }
90  
91 public function testReadsFromRequestBody()
92 {
93 $body = Stream::factory('foo');
94 $t = new Transaction(new Client(), new Request('PUT', 'http://httbin.org', [], $body));
95 $m = new RequestMediator($t, new MessageFactory());
96 $this->assertEquals('foo', $m->readRequestBody(null, null, 3));
97 }
98  
99 public function testEmitsHeadersEventForHeadRequest()
100 {
101 Server::enqueue(["HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK"]);
102 $ee = null;
103 $client = new Client(['adapter' => new MultiAdapter(new MessageFactory())]);
104 $client->head(Server::$url, [
105 'events' => [
106 'headers' => function (HeadersEvent $e) use (&$ee) {
107 $ee = $e;
108 }
109 ]
110 ]);
111 $this->assertInstanceOf('GuzzleHttp\\Event\\HeadersEvent', $ee);
112 }
113 }