skipIf(!function_exists('curl_init'), 'Skipping as ext/curl is not installed.'); $this->curl = new Curl(); $this->caFile = CORE_PATH . 'config' . DIRECTORY_SEPARATOR . 'cacert.pem'; } /** * Test the send method * * @return void */ public function testSendLive() { $request = new Request('http://localhost', 'GET', [ 'User-Agent' => 'CakePHP TestSuite', 'Cookie' => 'testing=value' ]); try { $responses = $this->curl->send($request, []); } catch (\Cake\Core\Exception\Exception $e) { $this->markTestSkipped('Could not connect to localhost, skipping'); } $this->assertCount(1, $responses); $response = $responses[0]; $this->assertInstanceOf(Response::class, $response); $this->assertNotEmpty($response->getHeaders()); $this->assertNotEmpty($response->getBody()->getContents()); } /** * Test the send method * * @return void */ public function testSendLiveResponseCheck() { $request = new Request('https://api.cakephp.org/3.0/', 'GET', [ 'User-Agent' => 'CakePHP TestSuite', ]); try { $responses = $this->curl->send($request, []); } catch (\Cake\Core\Exception\Exception $e) { $this->markTestSkipped('Could not connect to book.cakephp.org, skipping'); } $this->assertCount(1, $responses); $response = $responses[0]; $this->assertInstanceOf(Response::class, $response); $this->assertTrue($response->hasHeader('Date')); $this->assertTrue($response->hasHeader('Content-type')); $this->assertContains('getBody()->getContents()); } /** * Test converting client options into curl ones. * * @return void */ public function testBuildOptionsGet() { $options = [ 'timeout' => 5 ]; $request = new Request( 'http://localhost/things', 'GET', ['Cookie' => 'testing=value'] ); $result = $this->curl->buildOptions($request, $options); $expected = [ CURLOPT_URL => 'http://localhost/things', CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => [ 'Cookie: testing=value', 'Connection: close', 'User-Agent: CakePHP', ], CURLOPT_HTTPGET => true, CURLOPT_TIMEOUT => 5, CURLOPT_CAINFO => $this->caFile, ]; $this->assertSame($expected, $result); } /** * Test converting client options into curl ones. * * @return void */ public function testBuildOptionsPost() { $options = []; $request = new Request( 'http://localhost/things', 'POST', ['Cookie' => 'testing=value'], ['name' => 'cakephp', 'yes' => 1] ); $result = $this->curl->buildOptions($request, $options); $expected = [ CURLOPT_URL => 'http://localhost/things', CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => [ 'Cookie: testing=value', 'Connection: close', 'User-Agent: CakePHP', 'Content-Type: application/x-www-form-urlencoded', ], CURLOPT_POST => true, CURLOPT_POSTFIELDS => 'name=cakephp&yes=1', CURLOPT_CAINFO => $this->caFile, ]; $this->assertSame($expected, $result); } /** * Test converting client options into curl ones. * * @return void */ public function testBuildOptionsPut() { $options = []; $request = new Request( 'http://localhost/things', 'PUT', ['Cookie' => 'testing=value'] ); $result = $this->curl->buildOptions($request, $options); $expected = [ CURLOPT_URL => 'http://localhost/things', CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => [ 'Cookie: testing=value', 'Connection: close', 'User-Agent: CakePHP', ], CURLOPT_POST => true, CURLOPT_CUSTOMREQUEST => 'PUT', CURLOPT_CAINFO => $this->caFile, ]; $this->assertSame($expected, $result); } /** * Test converting client options into curl ones. * * @return void */ public function testBuildOptionsJsonPost() { $options = []; $content = json_encode(['a' => 1, 'b' => 2]); $request = new Request( 'http://localhost/things', 'POST', ['Content-type' => 'application/json'], $content ); $result = $this->curl->buildOptions($request, $options); $expected = [ CURLOPT_URL => 'http://localhost/things', CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => [ 'Content-type: application/json', 'Connection: close', 'User-Agent: CakePHP', ], CURLOPT_POST => true, CURLOPT_POSTFIELDS => $content, CURLOPT_CAINFO => $this->caFile, ]; $this->assertSame($expected, $result); } /** * Test converting client options into curl ones. * * @return void */ public function testBuildOptionsSsl() { $options = [ 'ssl_verify_host' => true, 'ssl_verify_peer' => true, 'ssl_verify_peer_name' => true, // These options do nothing in curl. 'ssl_verify_depth' => 9000, 'ssl_allow_self_signed' => false, ]; $request = new Request('http://localhost/things', 'GET'); $result = $this->curl->buildOptions($request, $options); $expected = [ CURLOPT_URL => 'http://localhost/things', CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => [ 'Connection: close', 'User-Agent: CakePHP', ], CURLOPT_HTTPGET => true, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_CAINFO => $this->caFile, ]; $this->assertSame($expected, $result); } /** * Test converting client options into curl ones. * * @return void */ public function testBuildOptionsProxy() { $options = [ 'proxy' => [ 'proxy' => '127.0.0.1:8080', 'username' => 'frodo', 'password' => 'one_ring', ] ]; $request = new Request('http://localhost/things', 'GET'); $result = $this->curl->buildOptions($request, $options); $expected = [ CURLOPT_URL => 'http://localhost/things', CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => [ 'Connection: close', 'User-Agent: CakePHP', ], CURLOPT_HTTPGET => true, CURLOPT_CAINFO => $this->caFile, CURLOPT_PROXY => '127.0.0.1:8080', CURLOPT_PROXYUSERPWD => 'frodo:one_ring', ]; $this->assertSame($expected, $result); } /** * Test converting client options into curl ones. * * @return void */ public function testBuildOptionsCurlOptions() { $options = [ 'curl' => [ CURLOPT_USERAGENT => 'Super-secret' ] ]; $request = new Request('http://localhost/things', 'GET'); $request = $request->withProtocolVersion('1.0'); $result = $this->curl->buildOptions($request, $options); $expected = [ CURLOPT_URL => 'http://localhost/things', CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => [ 'Connection: close', 'User-Agent: CakePHP', ], CURLOPT_HTTPGET => true, CURLOPT_CAINFO => $this->caFile, CURLOPT_USERAGENT => 'Super-secret' ]; $this->assertSame($expected, $result); } }