1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehlernamespace Sabre\HTTP; 4*a1a3b679SAndreas Boehler 5*a1a3b679SAndreas Boehlerclass ClientTest extends \PHPUnit_Framework_TestCase { 6*a1a3b679SAndreas Boehler 7*a1a3b679SAndreas Boehler protected $client; 8*a1a3b679SAndreas Boehler 9*a1a3b679SAndreas Boehler function testCreateCurlSettingsArrayGET() { 10*a1a3b679SAndreas Boehler 11*a1a3b679SAndreas Boehler $client = new ClientMock(); 12*a1a3b679SAndreas Boehler $client->addCurlSetting(CURLOPT_POSTREDIR, 0); 13*a1a3b679SAndreas Boehler 14*a1a3b679SAndreas Boehler $request = new Request('GET', 'http://example.org/', ['X-Foo' => 'bar']); 15*a1a3b679SAndreas Boehler 16*a1a3b679SAndreas Boehler $settings = [ 17*a1a3b679SAndreas Boehler CURLOPT_RETURNTRANSFER => true, 18*a1a3b679SAndreas Boehler CURLOPT_HEADER => true, 19*a1a3b679SAndreas Boehler CURLOPT_POSTREDIR => 0, 20*a1a3b679SAndreas Boehler CURLOPT_HTTPHEADER => ['X-Foo: bar'], 21*a1a3b679SAndreas Boehler CURLOPT_NOBODY => false, 22*a1a3b679SAndreas Boehler CURLOPT_URL => 'http://example.org/', 23*a1a3b679SAndreas Boehler CURLOPT_CUSTOMREQUEST => 'GET', 24*a1a3b679SAndreas Boehler CURLOPT_POSTFIELDS => null, 25*a1a3b679SAndreas Boehler CURLOPT_PUT => false, 26*a1a3b679SAndreas Boehler ]; 27*a1a3b679SAndreas Boehler 28*a1a3b679SAndreas Boehler // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM 29*a1a3b679SAndreas Boehler // at least if this unit test fails in the future we know it is :) 30*a1a3b679SAndreas Boehler if (defined('HHVM_VERSION') === false) { 31*a1a3b679SAndreas Boehler $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; 32*a1a3b679SAndreas Boehler $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; 33*a1a3b679SAndreas Boehler } 34*a1a3b679SAndreas Boehler 35*a1a3b679SAndreas Boehler 36*a1a3b679SAndreas Boehler $this->assertEquals($settings, $client->createCurlSettingsArray($request)); 37*a1a3b679SAndreas Boehler 38*a1a3b679SAndreas Boehler } 39*a1a3b679SAndreas Boehler 40*a1a3b679SAndreas Boehler function testCreateCurlSettingsArrayHEAD() { 41*a1a3b679SAndreas Boehler 42*a1a3b679SAndreas Boehler $client = new ClientMock(); 43*a1a3b679SAndreas Boehler $request = new Request('HEAD', 'http://example.org/', ['X-Foo' => 'bar']); 44*a1a3b679SAndreas Boehler 45*a1a3b679SAndreas Boehler 46*a1a3b679SAndreas Boehler $settings = [ 47*a1a3b679SAndreas Boehler CURLOPT_RETURNTRANSFER => true, 48*a1a3b679SAndreas Boehler CURLOPT_HEADER => true, 49*a1a3b679SAndreas Boehler CURLOPT_NOBODY => true, 50*a1a3b679SAndreas Boehler CURLOPT_CUSTOMREQUEST => 'HEAD', 51*a1a3b679SAndreas Boehler CURLOPT_HTTPHEADER => ['X-Foo: bar'], 52*a1a3b679SAndreas Boehler CURLOPT_URL => 'http://example.org/', 53*a1a3b679SAndreas Boehler CURLOPT_POSTFIELDS => '', 54*a1a3b679SAndreas Boehler CURLOPT_PUT => false, 55*a1a3b679SAndreas Boehler ]; 56*a1a3b679SAndreas Boehler 57*a1a3b679SAndreas Boehler // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM 58*a1a3b679SAndreas Boehler // at least if this unit test fails in the future we know it is :) 59*a1a3b679SAndreas Boehler if (defined('HHVM_VERSION') === false) { 60*a1a3b679SAndreas Boehler $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; 61*a1a3b679SAndreas Boehler $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; 62*a1a3b679SAndreas Boehler } 63*a1a3b679SAndreas Boehler 64*a1a3b679SAndreas Boehler $this->assertEquals($settings, $client->createCurlSettingsArray($request)); 65*a1a3b679SAndreas Boehler 66*a1a3b679SAndreas Boehler } 67*a1a3b679SAndreas Boehler 68*a1a3b679SAndreas Boehler function testCreateCurlSettingsArrayGETAfterHEAD() { 69*a1a3b679SAndreas Boehler 70*a1a3b679SAndreas Boehler $client = new ClientMock(); 71*a1a3b679SAndreas Boehler $request = new Request('HEAD', 'http://example.org/', ['X-Foo' => 'bar']); 72*a1a3b679SAndreas Boehler 73*a1a3b679SAndreas Boehler // Parsing the settings for this method, and discarding the result. 74*a1a3b679SAndreas Boehler // This will cause the client to automatically persist previous 75*a1a3b679SAndreas Boehler // settings and will help us detect problems. 76*a1a3b679SAndreas Boehler $client->createCurlSettingsArray($request); 77*a1a3b679SAndreas Boehler 78*a1a3b679SAndreas Boehler // This is the real request. 79*a1a3b679SAndreas Boehler $request = new Request('GET', 'http://example.org/', ['X-Foo' => 'bar']); 80*a1a3b679SAndreas Boehler 81*a1a3b679SAndreas Boehler $settings = [ 82*a1a3b679SAndreas Boehler CURLOPT_CUSTOMREQUEST => 'GET', 83*a1a3b679SAndreas Boehler CURLOPT_RETURNTRANSFER => true, 84*a1a3b679SAndreas Boehler CURLOPT_HEADER => true, 85*a1a3b679SAndreas Boehler CURLOPT_HTTPHEADER => ['X-Foo: bar'], 86*a1a3b679SAndreas Boehler CURLOPT_NOBODY => false, 87*a1a3b679SAndreas Boehler CURLOPT_URL => 'http://example.org/', 88*a1a3b679SAndreas Boehler CURLOPT_POSTFIELDS => '', 89*a1a3b679SAndreas Boehler CURLOPT_PUT => false, 90*a1a3b679SAndreas Boehler ]; 91*a1a3b679SAndreas Boehler 92*a1a3b679SAndreas Boehler // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM 93*a1a3b679SAndreas Boehler // at least if this unit test fails in the future we know it is :) 94*a1a3b679SAndreas Boehler if (defined('HHVM_VERSION') === false) { 95*a1a3b679SAndreas Boehler $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; 96*a1a3b679SAndreas Boehler $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; 97*a1a3b679SAndreas Boehler } 98*a1a3b679SAndreas Boehler 99*a1a3b679SAndreas Boehler $this->assertEquals($settings, $client->createCurlSettingsArray($request)); 100*a1a3b679SAndreas Boehler 101*a1a3b679SAndreas Boehler } 102*a1a3b679SAndreas Boehler 103*a1a3b679SAndreas Boehler function testCreateCurlSettingsArrayPUTStream() { 104*a1a3b679SAndreas Boehler 105*a1a3b679SAndreas Boehler $client = new ClientMock(); 106*a1a3b679SAndreas Boehler 107*a1a3b679SAndreas Boehler $h = fopen('php://memory', 'r+'); 108*a1a3b679SAndreas Boehler fwrite($h, 'booh'); 109*a1a3b679SAndreas Boehler $request = new Request('PUT', 'http://example.org/', ['X-Foo' => 'bar'], $h); 110*a1a3b679SAndreas Boehler 111*a1a3b679SAndreas Boehler $settings = [ 112*a1a3b679SAndreas Boehler CURLOPT_RETURNTRANSFER => true, 113*a1a3b679SAndreas Boehler CURLOPT_HEADER => true, 114*a1a3b679SAndreas Boehler CURLOPT_PUT => true, 115*a1a3b679SAndreas Boehler CURLOPT_INFILE => $h, 116*a1a3b679SAndreas Boehler CURLOPT_NOBODY => false, 117*a1a3b679SAndreas Boehler CURLOPT_CUSTOMREQUEST => 'PUT', 118*a1a3b679SAndreas Boehler CURLOPT_HTTPHEADER => ['X-Foo: bar'], 119*a1a3b679SAndreas Boehler CURLOPT_URL => 'http://example.org/', 120*a1a3b679SAndreas Boehler ]; 121*a1a3b679SAndreas Boehler 122*a1a3b679SAndreas Boehler // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM 123*a1a3b679SAndreas Boehler // at least if this unit test fails in the future we know it is :) 124*a1a3b679SAndreas Boehler if (defined('HHVM_VERSION') === false) { 125*a1a3b679SAndreas Boehler $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; 126*a1a3b679SAndreas Boehler $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; 127*a1a3b679SAndreas Boehler } 128*a1a3b679SAndreas Boehler 129*a1a3b679SAndreas Boehler $this->assertEquals($settings, $client->createCurlSettingsArray($request)); 130*a1a3b679SAndreas Boehler 131*a1a3b679SAndreas Boehler } 132*a1a3b679SAndreas Boehler 133*a1a3b679SAndreas Boehler function testCreateCurlSettingsArrayPUTString() { 134*a1a3b679SAndreas Boehler 135*a1a3b679SAndreas Boehler $client = new ClientMock(); 136*a1a3b679SAndreas Boehler $request = new Request('PUT', 'http://example.org/', ['X-Foo' => 'bar'], 'boo'); 137*a1a3b679SAndreas Boehler 138*a1a3b679SAndreas Boehler $settings = [ 139*a1a3b679SAndreas Boehler CURLOPT_RETURNTRANSFER => true, 140*a1a3b679SAndreas Boehler CURLOPT_HEADER => true, 141*a1a3b679SAndreas Boehler CURLOPT_NOBODY => false, 142*a1a3b679SAndreas Boehler CURLOPT_POSTFIELDS => 'boo', 143*a1a3b679SAndreas Boehler CURLOPT_CUSTOMREQUEST => 'PUT', 144*a1a3b679SAndreas Boehler CURLOPT_HTTPHEADER => ['X-Foo: bar'], 145*a1a3b679SAndreas Boehler CURLOPT_URL => 'http://example.org/', 146*a1a3b679SAndreas Boehler ]; 147*a1a3b679SAndreas Boehler 148*a1a3b679SAndreas Boehler // FIXME: CURLOPT_PROTOCOLS and CURLOPT_REDIR_PROTOCOLS are currently unsupported by HHVM 149*a1a3b679SAndreas Boehler // at least if this unit test fails in the future we know it is :) 150*a1a3b679SAndreas Boehler if (defined('HHVM_VERSION') === false) { 151*a1a3b679SAndreas Boehler $settings[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; 152*a1a3b679SAndreas Boehler $settings[CURLOPT_REDIR_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; 153*a1a3b679SAndreas Boehler } 154*a1a3b679SAndreas Boehler 155*a1a3b679SAndreas Boehler $this->assertEquals($settings, $client->createCurlSettingsArray($request)); 156*a1a3b679SAndreas Boehler 157*a1a3b679SAndreas Boehler } 158*a1a3b679SAndreas Boehler 159*a1a3b679SAndreas Boehler function testSend() { 160*a1a3b679SAndreas Boehler 161*a1a3b679SAndreas Boehler $client = new ClientMock(); 162*a1a3b679SAndreas Boehler $request = new Request('GET', 'http://example.org/'); 163*a1a3b679SAndreas Boehler 164*a1a3b679SAndreas Boehler $client->on('doRequest', function($request, &$response) { 165*a1a3b679SAndreas Boehler $response = new Response(200); 166*a1a3b679SAndreas Boehler }); 167*a1a3b679SAndreas Boehler 168*a1a3b679SAndreas Boehler $response = $client->send($request); 169*a1a3b679SAndreas Boehler 170*a1a3b679SAndreas Boehler $this->assertEquals(200, $response->getStatus()); 171*a1a3b679SAndreas Boehler 172*a1a3b679SAndreas Boehler } 173*a1a3b679SAndreas Boehler 174*a1a3b679SAndreas Boehler function testSendClientError() { 175*a1a3b679SAndreas Boehler 176*a1a3b679SAndreas Boehler $client = new ClientMock(); 177*a1a3b679SAndreas Boehler $request = new Request('GET', 'http://example.org/'); 178*a1a3b679SAndreas Boehler 179*a1a3b679SAndreas Boehler $client->on('doRequest', function($request, &$response) { 180*a1a3b679SAndreas Boehler throw new ClientException('aaah', 1); 181*a1a3b679SAndreas Boehler }); 182*a1a3b679SAndreas Boehler $called = false; 183*a1a3b679SAndreas Boehler $client->on('exception', function() use (&$called) { 184*a1a3b679SAndreas Boehler $called = true; 185*a1a3b679SAndreas Boehler }); 186*a1a3b679SAndreas Boehler 187*a1a3b679SAndreas Boehler try { 188*a1a3b679SAndreas Boehler $client->send($request); 189*a1a3b679SAndreas Boehler $this->fail('send() should have thrown an exception'); 190*a1a3b679SAndreas Boehler } catch (ClientException $e) { 191*a1a3b679SAndreas Boehler 192*a1a3b679SAndreas Boehler } 193*a1a3b679SAndreas Boehler $this->assertTrue($called); 194*a1a3b679SAndreas Boehler 195*a1a3b679SAndreas Boehler } 196*a1a3b679SAndreas Boehler 197*a1a3b679SAndreas Boehler function testSendHttpError() { 198*a1a3b679SAndreas Boehler 199*a1a3b679SAndreas Boehler $client = new ClientMock(); 200*a1a3b679SAndreas Boehler $request = new Request('GET', 'http://example.org/'); 201*a1a3b679SAndreas Boehler 202*a1a3b679SAndreas Boehler $client->on('doRequest', function($request, &$response) { 203*a1a3b679SAndreas Boehler $response = new Response(404); 204*a1a3b679SAndreas Boehler }); 205*a1a3b679SAndreas Boehler $called = 0; 206*a1a3b679SAndreas Boehler $client->on('error', function() use (&$called) { 207*a1a3b679SAndreas Boehler $called++; 208*a1a3b679SAndreas Boehler }); 209*a1a3b679SAndreas Boehler $client->on('error:404', function() use (&$called) { 210*a1a3b679SAndreas Boehler $called++; 211*a1a3b679SAndreas Boehler }); 212*a1a3b679SAndreas Boehler 213*a1a3b679SAndreas Boehler $client->send($request); 214*a1a3b679SAndreas Boehler $this->assertEquals(2, $called); 215*a1a3b679SAndreas Boehler 216*a1a3b679SAndreas Boehler } 217*a1a3b679SAndreas Boehler 218*a1a3b679SAndreas Boehler function testSendRetry() { 219*a1a3b679SAndreas Boehler 220*a1a3b679SAndreas Boehler $client = new ClientMock(); 221*a1a3b679SAndreas Boehler $request = new Request('GET', 'http://example.org/'); 222*a1a3b679SAndreas Boehler 223*a1a3b679SAndreas Boehler $called = 0; 224*a1a3b679SAndreas Boehler $client->on('doRequest', function($request, &$response) use (&$called) { 225*a1a3b679SAndreas Boehler $called++; 226*a1a3b679SAndreas Boehler if ($called < 3) { 227*a1a3b679SAndreas Boehler $response = new Response(404); 228*a1a3b679SAndreas Boehler } else { 229*a1a3b679SAndreas Boehler $response = new Response(200); 230*a1a3b679SAndreas Boehler } 231*a1a3b679SAndreas Boehler }); 232*a1a3b679SAndreas Boehler 233*a1a3b679SAndreas Boehler $errorCalled = 0; 234*a1a3b679SAndreas Boehler $client->on('error', function($request, $response, &$retry, $retryCount) use (&$errorCalled) { 235*a1a3b679SAndreas Boehler 236*a1a3b679SAndreas Boehler $errorCalled++; 237*a1a3b679SAndreas Boehler $retry = true; 238*a1a3b679SAndreas Boehler 239*a1a3b679SAndreas Boehler }); 240*a1a3b679SAndreas Boehler 241*a1a3b679SAndreas Boehler $response = $client->send($request); 242*a1a3b679SAndreas Boehler $this->assertEquals(3, $called); 243*a1a3b679SAndreas Boehler $this->assertEquals(2, $errorCalled); 244*a1a3b679SAndreas Boehler $this->assertEquals(200, $response->getStatus()); 245*a1a3b679SAndreas Boehler 246*a1a3b679SAndreas Boehler } 247*a1a3b679SAndreas Boehler 248*a1a3b679SAndreas Boehler function testHttpErrorException() { 249*a1a3b679SAndreas Boehler 250*a1a3b679SAndreas Boehler $client = new ClientMock(); 251*a1a3b679SAndreas Boehler $client->setThrowExceptions(true); 252*a1a3b679SAndreas Boehler $request = new Request('GET', 'http://example.org/'); 253*a1a3b679SAndreas Boehler 254*a1a3b679SAndreas Boehler $client->on('doRequest', function($request, &$response) { 255*a1a3b679SAndreas Boehler $response = new Response(404); 256*a1a3b679SAndreas Boehler }); 257*a1a3b679SAndreas Boehler 258*a1a3b679SAndreas Boehler try { 259*a1a3b679SAndreas Boehler $client->send($request); 260*a1a3b679SAndreas Boehler $this->fail('An exception should have been thrown'); 261*a1a3b679SAndreas Boehler } catch (ClientHttpException $e) { 262*a1a3b679SAndreas Boehler $this->assertEquals(404, $e->getHttpStatus()); 263*a1a3b679SAndreas Boehler $this->assertInstanceOf('Sabre\HTTP\Response', $e->getResponse()); 264*a1a3b679SAndreas Boehler } 265*a1a3b679SAndreas Boehler 266*a1a3b679SAndreas Boehler } 267*a1a3b679SAndreas Boehler 268*a1a3b679SAndreas Boehler function testParseCurlResult() { 269*a1a3b679SAndreas Boehler 270*a1a3b679SAndreas Boehler $client = new ClientMock(); 271*a1a3b679SAndreas Boehler $client->on('curlStuff', function(&$return) { 272*a1a3b679SAndreas Boehler 273*a1a3b679SAndreas Boehler $return = [ 274*a1a3b679SAndreas Boehler [ 275*a1a3b679SAndreas Boehler 'header_size' => 33, 276*a1a3b679SAndreas Boehler 'http_code' => 200, 277*a1a3b679SAndreas Boehler ], 278*a1a3b679SAndreas Boehler 0, 279*a1a3b679SAndreas Boehler '', 280*a1a3b679SAndreas Boehler ]; 281*a1a3b679SAndreas Boehler 282*a1a3b679SAndreas Boehler }); 283*a1a3b679SAndreas Boehler 284*a1a3b679SAndreas Boehler $body = "HTTP/1.1 200 OK\r\nHeader1:Val1\r\n\r\nFoo"; 285*a1a3b679SAndreas Boehler $result = $client->parseCurlResult($body, 'foobar'); 286*a1a3b679SAndreas Boehler 287*a1a3b679SAndreas Boehler $this->assertEquals(Client::STATUS_SUCCESS, $result['status']); 288*a1a3b679SAndreas Boehler $this->assertEquals(200, $result['http_code']); 289*a1a3b679SAndreas Boehler $this->assertEquals(200, $result['response']->getStatus()); 290*a1a3b679SAndreas Boehler $this->assertEquals(['Header1' => ['Val1']], $result['response']->getHeaders()); 291*a1a3b679SAndreas Boehler $this->assertEquals('Foo', $result['response']->getBodyAsString()); 292*a1a3b679SAndreas Boehler 293*a1a3b679SAndreas Boehler } 294*a1a3b679SAndreas Boehler 295*a1a3b679SAndreas Boehler function testParseCurlError() { 296*a1a3b679SAndreas Boehler 297*a1a3b679SAndreas Boehler $client = new ClientMock(); 298*a1a3b679SAndreas Boehler $client->on('curlStuff', function(&$return) { 299*a1a3b679SAndreas Boehler 300*a1a3b679SAndreas Boehler $return = [ 301*a1a3b679SAndreas Boehler [], 302*a1a3b679SAndreas Boehler 1, 303*a1a3b679SAndreas Boehler 'Curl error', 304*a1a3b679SAndreas Boehler ]; 305*a1a3b679SAndreas Boehler 306*a1a3b679SAndreas Boehler }); 307*a1a3b679SAndreas Boehler 308*a1a3b679SAndreas Boehler $body = "HTTP/1.1 200 OK\r\nHeader1:Val1\r\n\r\nFoo"; 309*a1a3b679SAndreas Boehler $result = $client->parseCurlResult($body, 'foobar'); 310*a1a3b679SAndreas Boehler 311*a1a3b679SAndreas Boehler $this->assertEquals(Client::STATUS_CURLERROR, $result['status']); 312*a1a3b679SAndreas Boehler $this->assertEquals(1, $result['curl_errno']); 313*a1a3b679SAndreas Boehler $this->assertEquals('Curl error', $result['curl_errmsg']); 314*a1a3b679SAndreas Boehler 315*a1a3b679SAndreas Boehler } 316*a1a3b679SAndreas Boehler 317*a1a3b679SAndreas Boehler function testDoRequest() { 318*a1a3b679SAndreas Boehler 319*a1a3b679SAndreas Boehler $client = new ClientMock(); 320*a1a3b679SAndreas Boehler $request = new Request('GET', 'http://example.org/'); 321*a1a3b679SAndreas Boehler $client->on('curlExec', function(&$return) { 322*a1a3b679SAndreas Boehler 323*a1a3b679SAndreas Boehler $return = "HTTP/1.1 200 OK\r\nHeader1:Val1\r\n\r\nFoo"; 324*a1a3b679SAndreas Boehler 325*a1a3b679SAndreas Boehler }); 326*a1a3b679SAndreas Boehler $client->on('curlStuff', function(&$return) { 327*a1a3b679SAndreas Boehler 328*a1a3b679SAndreas Boehler $return = [ 329*a1a3b679SAndreas Boehler [ 330*a1a3b679SAndreas Boehler 'header_size' => 33, 331*a1a3b679SAndreas Boehler 'http_code' => 200, 332*a1a3b679SAndreas Boehler ], 333*a1a3b679SAndreas Boehler 0, 334*a1a3b679SAndreas Boehler '', 335*a1a3b679SAndreas Boehler ]; 336*a1a3b679SAndreas Boehler 337*a1a3b679SAndreas Boehler }); 338*a1a3b679SAndreas Boehler $response = $client->doRequest($request); 339*a1a3b679SAndreas Boehler $this->assertEquals(200, $response->getStatus()); 340*a1a3b679SAndreas Boehler $this->assertEquals(['Header1' => ['Val1']], $response->getHeaders()); 341*a1a3b679SAndreas Boehler $this->assertEquals('Foo', $response->getBodyAsString()); 342*a1a3b679SAndreas Boehler 343*a1a3b679SAndreas Boehler } 344*a1a3b679SAndreas Boehler 345*a1a3b679SAndreas Boehler function testDoRequestCurlError() { 346*a1a3b679SAndreas Boehler 347*a1a3b679SAndreas Boehler $client = new ClientMock(); 348*a1a3b679SAndreas Boehler $request = new Request('GET', 'http://example.org/'); 349*a1a3b679SAndreas Boehler $client->on('curlExec', function(&$return) { 350*a1a3b679SAndreas Boehler 351*a1a3b679SAndreas Boehler $return = ""; 352*a1a3b679SAndreas Boehler 353*a1a3b679SAndreas Boehler }); 354*a1a3b679SAndreas Boehler $client->on('curlStuff', function(&$return) { 355*a1a3b679SAndreas Boehler 356*a1a3b679SAndreas Boehler $return = [ 357*a1a3b679SAndreas Boehler [], 358*a1a3b679SAndreas Boehler 1, 359*a1a3b679SAndreas Boehler 'Curl error', 360*a1a3b679SAndreas Boehler ]; 361*a1a3b679SAndreas Boehler 362*a1a3b679SAndreas Boehler }); 363*a1a3b679SAndreas Boehler 364*a1a3b679SAndreas Boehler try { 365*a1a3b679SAndreas Boehler $response = $client->doRequest($request); 366*a1a3b679SAndreas Boehler $this->fail('This should have thrown an exception'); 367*a1a3b679SAndreas Boehler } catch (ClientException $e) { 368*a1a3b679SAndreas Boehler $this->assertEquals(1, $e->getCode()); 369*a1a3b679SAndreas Boehler $this->assertEquals('Curl error', $e->getMessage()); 370*a1a3b679SAndreas Boehler } 371*a1a3b679SAndreas Boehler 372*a1a3b679SAndreas Boehler } 373*a1a3b679SAndreas Boehler 374*a1a3b679SAndreas Boehler} 375*a1a3b679SAndreas Boehler 376*a1a3b679SAndreas Boehlerclass ClientMock extends Client { 377*a1a3b679SAndreas Boehler 378*a1a3b679SAndreas Boehler protected $persistedSettings = []; 379*a1a3b679SAndreas Boehler 380*a1a3b679SAndreas Boehler /** 381*a1a3b679SAndreas Boehler * Making this method public. 382*a1a3b679SAndreas Boehler * 383*a1a3b679SAndreas Boehler * We are also going to persist all settings this method generates. While 384*a1a3b679SAndreas Boehler * the underlying object doesn't behave exactly the same, it helps us 385*a1a3b679SAndreas Boehler * simulate what curl does internally, and helps us identify problems with 386*a1a3b679SAndreas Boehler * settings that are set by _some_ methods and not correctly reset by other 387*a1a3b679SAndreas Boehler * methods after subsequent use. 388*a1a3b679SAndreas Boehler * forces 389*a1a3b679SAndreas Boehler */ 390*a1a3b679SAndreas Boehler function createCurlSettingsArray(RequestInterface $request) { 391*a1a3b679SAndreas Boehler 392*a1a3b679SAndreas Boehler $settings = parent::createCurlSettingsArray($request); 393*a1a3b679SAndreas Boehler $settings = $settings + $this->persistedSettings; 394*a1a3b679SAndreas Boehler $this->persistedSettings = $settings; 395*a1a3b679SAndreas Boehler return $settings; 396*a1a3b679SAndreas Boehler 397*a1a3b679SAndreas Boehler } 398*a1a3b679SAndreas Boehler /** 399*a1a3b679SAndreas Boehler * Making this method public. 400*a1a3b679SAndreas Boehler */ 401*a1a3b679SAndreas Boehler function parseCurlResult($response, $curlHandle) { 402*a1a3b679SAndreas Boehler 403*a1a3b679SAndreas Boehler return parent::parseCurlResult($response, $curlHandle); 404*a1a3b679SAndreas Boehler 405*a1a3b679SAndreas Boehler } 406*a1a3b679SAndreas Boehler 407*a1a3b679SAndreas Boehler /** 408*a1a3b679SAndreas Boehler * This method is responsible for performing a single request. 409*a1a3b679SAndreas Boehler * 410*a1a3b679SAndreas Boehler * @param RequestInterface $request 411*a1a3b679SAndreas Boehler * @return ResponseInterface 412*a1a3b679SAndreas Boehler */ 413*a1a3b679SAndreas Boehler function doRequest(RequestInterface $request) { 414*a1a3b679SAndreas Boehler 415*a1a3b679SAndreas Boehler $response = null; 416*a1a3b679SAndreas Boehler $this->emit('doRequest', [$request, &$response]); 417*a1a3b679SAndreas Boehler 418*a1a3b679SAndreas Boehler // If nothing modified $response, we're using the default behavior. 419*a1a3b679SAndreas Boehler if (is_null($response)) { 420*a1a3b679SAndreas Boehler return parent::doRequest($request); 421*a1a3b679SAndreas Boehler } else { 422*a1a3b679SAndreas Boehler return $response; 423*a1a3b679SAndreas Boehler } 424*a1a3b679SAndreas Boehler 425*a1a3b679SAndreas Boehler } 426*a1a3b679SAndreas Boehler 427*a1a3b679SAndreas Boehler /** 428*a1a3b679SAndreas Boehler * Returns a bunch of information about a curl request. 429*a1a3b679SAndreas Boehler * 430*a1a3b679SAndreas Boehler * This method exists so it can easily be overridden and mocked. 431*a1a3b679SAndreas Boehler * 432*a1a3b679SAndreas Boehler * @param resource $curlHandle 433*a1a3b679SAndreas Boehler * @return array 434*a1a3b679SAndreas Boehler */ 435*a1a3b679SAndreas Boehler protected function curlStuff($curlHandle) { 436*a1a3b679SAndreas Boehler 437*a1a3b679SAndreas Boehler $return = null; 438*a1a3b679SAndreas Boehler $this->emit('curlStuff', [&$return]); 439*a1a3b679SAndreas Boehler 440*a1a3b679SAndreas Boehler // If nothing modified $return, we're using the default behavior. 441*a1a3b679SAndreas Boehler if (is_null($return)) { 442*a1a3b679SAndreas Boehler return parent::curlStuff($curlHandle); 443*a1a3b679SAndreas Boehler } else { 444*a1a3b679SAndreas Boehler return $return; 445*a1a3b679SAndreas Boehler } 446*a1a3b679SAndreas Boehler 447*a1a3b679SAndreas Boehler } 448*a1a3b679SAndreas Boehler 449*a1a3b679SAndreas Boehler /** 450*a1a3b679SAndreas Boehler * Calls curl_exec 451*a1a3b679SAndreas Boehler * 452*a1a3b679SAndreas Boehler * This method exists so it can easily be overridden and mocked. 453*a1a3b679SAndreas Boehler * 454*a1a3b679SAndreas Boehler * @param resource $curlHandle 455*a1a3b679SAndreas Boehler * @return string 456*a1a3b679SAndreas Boehler */ 457*a1a3b679SAndreas Boehler protected function curlExec($curlHandle) { 458*a1a3b679SAndreas Boehler 459*a1a3b679SAndreas Boehler $return = null; 460*a1a3b679SAndreas Boehler $this->emit('curlExec', [&$return]); 461*a1a3b679SAndreas Boehler 462*a1a3b679SAndreas Boehler // If nothing modified $return, we're using the default behavior. 463*a1a3b679SAndreas Boehler if (is_null($return)) { 464*a1a3b679SAndreas Boehler return parent::curlExec($curlHandle); 465*a1a3b679SAndreas Boehler } else { 466*a1a3b679SAndreas Boehler return $return; 467*a1a3b679SAndreas Boehler } 468*a1a3b679SAndreas Boehler 469*a1a3b679SAndreas Boehler } 470*a1a3b679SAndreas Boehler 471*a1a3b679SAndreas Boehler} 472