scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Tests\Adapter\Curl;
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\Tests\Server;
13 use GuzzleHttp\Url;
14  
15 abstract class AbstractCurl extends \PHPUnit_Framework_TestCase
16 {
17 abstract protected function getAdapter($factory = null, $options = []);
18  
19 public function testSendsRequest()
20 {
21 Server::flush();
22 Server::enqueue("HTTP/1.1 200 OK\r\nFoo: bar\r\nContent-Length: 0\r\n\r\n");
23 $t = new Transaction(new Client(), new Request('GET', Server::$url));
24 $a = $this->getAdapter();
25 $response = $a->send($t);
26 $this->assertEquals(200, $response->getStatusCode());
27 $this->assertEquals('bar', $response->getHeader('Foo'));
28 }
29  
30 /**
31 * @expectedException \GuzzleHttp\Exception\RequestException
32 */
33 public function testCatchesErrorWhenPreparing()
34 {
35 $r = new Request('GET', Server::$url);
36 $f = $this->getMockBuilder('GuzzleHttp\Adapter\Curl\CurlFactory')
37 ->setMethods(['__invoke'])
38 ->getMock();
39 $f->expects($this->once())
40 ->method('__invoke')
41 ->will($this->throwException(new RequestException('foo', $r)));
42  
43 $t = new Transaction(new Client(), $r);
44 $a = $this->getAdapter(null, ['handle_factory' => $f]);
45 $a->send($t);
46 }
47  
48 public function testDispatchesAfterSendEvent()
49 {
50 Server::flush();
51 Server::enqueue("HTTP/1.1 201 OK\r\nContent-Length: 0\r\n\r\n");
52 $r = new Request('GET', Server::$url);
53 $t = new Transaction(new Client(), $r);
54 $a = $this->getAdapter();
55 $ev = null;
56 $r->getEmitter()->on('complete', function (CompleteEvent $e) use (&$ev) {
57 $ev = $e;
58 $e->intercept(new Response(200, ['Foo' => 'bar']));
59 });
60 $response = $a->send($t);
61 $this->assertEquals(200, $response->getStatusCode());
62 $this->assertEquals('bar', $response->getHeader('Foo'));
63 }
64  
65 public function testDispatchesErrorEventAndRecovers()
66 {
67 Server::flush();
68 Server::enqueue("HTTP/1.1 201 OK\r\nContent-Length: 0\r\n\r\n");
69 $r = new Request('GET', Server::$url);
70 $t = new Transaction(new Client(), $r);
71 $a = $this->getAdapter();
72 $r->getEmitter()->once('complete', function (CompleteEvent $e) {
73 throw new RequestException('Foo', $e->getRequest());
74 });
75 $r->getEmitter()->on('error', function (ErrorEvent $e) {
76 $e->intercept(new Response(200, ['Foo' => 'bar']));
77 });
78 $response = $a->send($t);
79 $this->assertEquals(200, $response->getStatusCode());
80 $this->assertEquals('bar', $response->getHeader('Foo'));
81 }
82  
83 public function testStripsFragmentFromHost()
84 {
85 Server::flush();
86 Server::enqueue("HTTP/1.1 200 OK\r\n\r\nContent-Length: 0\r\n\r\n");
87 // This will fail if the removal of the #fragment is not performed
88 $url = Url::fromString(Server::$url)->setPath(null)->setFragment('foo');
89 $client = new Client();
90 $client->get($url);
91 }
92 }