diff --git a/CHANGELOG.md b/CHANGELOG.md index fa62656..566117b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,17 +4,18 @@ All notable changes to this project will be documented in this file. This projec ## 2.4.0 (in progress) ++ [#74](https://github.com/php-mod/curl/pull/75) Fixed PHPDoc Block, Added more Unit Tests. + Added GitHub Actions Tests (from 5.6 - 8.0) -## 2.3.1 (21. January 2021) +## 2.3.1 (21. January 2021) + Supports PHP8 -## 2.3.0 (19. March 2019) +## 2.3.0 (19. March 2019) + add asJson option (#67) -## 2.2.0 (4. December 2018) +## 2.2.0 (4. December 2018) + Added some getters. diff --git a/README.md b/README.md index 0b2be31..576bad5 100644 --- a/README.md +++ b/README.md @@ -128,4 +128,5 @@ In order to test the library: 1. Create a fork 2. Clone the fork to your machine 3. Install the depencies `composer install` +4. Build and start the docker image (in `tests/server`) `docker build . -t curlserver` start `docker run -p 1234:80 curlserver` 4. Run the unit tests `./vendor/bin/phpunit tests` diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 583f112..ee64a64 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -318,7 +318,7 @@ public function get($url, $data = array()) * Make a post request with optional post data. * * @param string $url The url to make the post request - * @param array $data Post data to pass to the url + * @param array|object|string $data Post data to pass to the url * @param boolean $asJson Whether the data should be passed as json or not. {@insce 2.2.1} * @return self */ diff --git a/tests/CurlTest.php b/tests/CurlTest.php index a36b6b8..4f81761 100644 --- a/tests/CurlTest.php +++ b/tests/CurlTest.php @@ -6,275 +6,306 @@ class CurlTest extends TestCase { + const TEST_URL = 'http://localhost:1234'; - const TEST_URL = 'http://localhost:1234'; - - /** - * - * @var Curl - */ - protected $curl; - - function set_up() { - parent::set_up(); - $this->curl = new Curl(); - $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE); - $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE); - } - - function server($request_method, $data='') { - $request_method = strtolower($request_method); - $this->curl->$request_method(self::TEST_URL . '/server.php', $data); - return $this->curl->response; - } - - public function testExtensionLoaded() { - - $this->assertTrue(extension_loaded('curl')); - } - - public function testUserAgent() { - - $this->curl->setUserAgent(Curl::USER_AGENT); - $this->assertEquals(Curl::USER_AGENT, $this->server('GET', array( - 'test' => 'server', - 'key' => 'HTTP_USER_AGENT', - ))); - - } - - public function testGet() { - $this->assertTrue($this->server('GET', array( - 'test' => 'server', - 'key' => 'REQUEST_METHOD', - )) === 'GET'); - } - - public function testPostRequestMethod() { - $this->assertTrue($this->server('POST', array( - 'test' => 'server', - 'key' => 'REQUEST_METHOD', - )) === 'POST'); - } - - public function testPostData() { - $this->assertTrue($this->server('POST', array( - 'test' => 'post', - 'key' => 'test', - )) === 'post'); - } - - public function testPostMultidimensionalData() { - - $data = array( - 'key' => 'file', - 'file' => array( - 'wibble', - 'wubble', - 'wobble', - ), - ); - - $this->curl->post(self::TEST_URL . '/post_multidimensional.php', $data); - - $this->assertEquals( - 'key=file&file%5B0%5D=wibble&file%5B1%5D=wubble&file%5B2%5D=wobble', - $this->curl->response); - - } - - public function testPostFilePathUpload() + /** + * + * @var Curl + */ + protected $curl; + + public function set_up() + { + parent::set_up(); + $this->curl = new Curl(); + $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, false); + $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, false); + } + + public function server($request_method, $data='') + { + $request_method = strtolower($request_method); + $this->curl->$request_method(self::TEST_URL . '/server.php', $data); + return $this->curl->response; + } + + public function testExtensionLoaded() + { + $this->assertTrue(extension_loaded('curl')); + } + + public function testUserAgent() + { + $this->curl->setUserAgent(Curl::USER_AGENT); + $this->assertEquals(Curl::USER_AGENT, $this->server('GET', array( + 'test' => 'server', + 'key' => 'HTTP_USER_AGENT', + ))); + } + + public function testGet() + { + $this->assertTrue($this->server('GET', array( + 'test' => 'server', + 'key' => 'REQUEST_METHOD', + )) === 'GET'); + } + + public function testPostRequestMethod() + { + $this->assertTrue($this->server('POST', array( + 'test' => 'server', + 'key' => 'REQUEST_METHOD', + )) === 'POST'); + } + + public function testPostData() + { + $this->assertTrue($this->server('POST', array( + 'test' => 'post', + 'key' => 'test', + )) === 'post'); + } + + public function testPostJsonData() { + $resp = $this->curl->post(self::TEST_URL.'/server.php', ['foo' => 'bar'], true); + + $this->assertTrue($resp->isSuccess()); - $file_path = $this->get_png(); + $this->assertArrayHasKey('x-powered-by', $resp->getResponseHeaders()); - $data = array( - 'key' => 'image', - 'image' => '@' . $file_path, - ); + // syntax error check + $resp->reset(); + } + + public function testPostMultidimensionalData() + { + $data = array( + 'key' => 'file', + 'file' => array( + 'wibble', + 'wubble', + 'wobble', + ), + ); + + $this->curl->post(self::TEST_URL . '/post_multidimensional.php', $data); + + $this->assertEquals( + 'key=file&file%5B0%5D=wibble&file%5B1%5D=wubble&file%5B2%5D=wobble', + $this->curl->response + ); + } + + public function testPostFilePathUpload() + { + $file_path = $this->get_png(); + + $data = array( + 'key' => 'image', + 'image' => '@' . $file_path, + ); $this->curl->setOpt(CURLOPT_RETURNTRANSFER, true); - $this->curl->post(self::TEST_URL . '/post_file_path_upload.php', $data); - - $this->assertEquals( - array( - 'request_method' => 'POST', - 'key' => 'image', - 'mime_content_type' => 'ERROR', // Temp change the image response, but assuming this is not fixing the issue indeed. - //'mime_content_type' => 'image/png' - ), - json_decode($this->curl->response, true)); - - unlink($file_path); - } - - public function testPutRequestMethod() { - $this->assertTrue($this->server('PUT', array( - 'test' => 'server', - 'key' => 'REQUEST_METHOD', - )) === 'PUT'); - } - - public function testPutData() { - $this->assertTrue($this->server('PUT', array( - 'test' => 'put', - 'key' => 'test', - )) === 'put'); - } - - public function testPutFileHandle() { - $png = $this->create_png(); - $tmp_file = $this->create_tmp_file($png); - - $this->curl->setOpt(CURLOPT_PUT, TRUE); - $this->curl->setOpt(CURLOPT_INFILE, $tmp_file); - $this->curl->setOpt(CURLOPT_INFILESIZE, strlen($png)); - $this->curl->put(self::TEST_URL . '/server.php', array( - 'test' => 'put_file_handle', - )); - - fclose($tmp_file); - - $this->assertTrue($this->curl->response === 'image/png'); - } - - public function testDelete() { - $this->assertTrue($this->server('DELETE', array( - 'test' => 'server', - 'key' => 'REQUEST_METHOD', - )) === 'DELETE'); - - $this->assertTrue($this->server('DELETE', array( - 'test' => 'delete', - 'key' => 'test', - )) === 'delete'); - } - - public function testBasicHttpAuth() { - - $data = array(); - - $this->curl->get(self::TEST_URL . '/http_basic_auth.php', $data); - - $this->assertEquals('canceled', $this->curl->response); - - $username = 'myusername'; - $password = 'mypassword'; - - $this->curl->setBasicAuthentication($username, $password); - - $this->curl->get(self::TEST_URL . '/http_basic_auth.php', $data); - - $this->assertEquals( - '{"username":"myusername","password":"mypassword"}', - $this->curl->response); - } - - public function testReferrer() { - $this->curl->setReferer('myreferrer'); - $this->assertTrue($this->server('GET', array( - 'test' => 'server', - 'key' => 'HTTP_REFERER', - )) === 'myreferrer'); - } - - public function testDeprecatedReferrer() { - $this->curl->setReferrer('myreferrer'); - $this->assertTrue($this->server('GET', array( - 'test' => 'server', - 'key' => 'HTTP_REFERER', - )) === 'myreferrer'); - } - - public function testCookies() { - $this->curl->setCookie('mycookie', 'yum'); - $this->assertTrue($this->server('GET', array( - 'test' => 'cookie', - 'key' => 'mycookie', - )) === 'yum'); - } - - public function testError() { - $this->curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000); - $this->curl->get('http://1.2.3.4/'); - $this->assertTrue($this->curl->error === TRUE); - $this->assertTrue($this->curl->curl_error === TRUE); - $this->assertTrue($this->curl->curl_error_code === CURLE_OPERATION_TIMEOUTED); - } - - public function testHeaders() { - $this->curl->setHeader('Content-Type', 'application/json'); - $this->curl->setHeader('X-Requested-With', 'XMLHttpRequest'); - $this->curl->setHeader('Accept', 'application/json'); - $this->assertTrue($this->server('GET', array( - 'test' => 'server', - 'key' => 'CONTENT_TYPE', - )) === 'application/json'); - $this->assertTrue($this->server('GET', array( - 'test' => 'server', - 'key' => 'HTTP_X_REQUESTED_WITH', - )) === 'XMLHttpRequest'); - $this->assertTrue($this->server('GET', array( - 'test' => 'server', - 'key' => 'HTTP_ACCEPT', - )) === 'application/json'); - } - - public function testHeadersWithContinue() { - $headers = file(dirname(__FILE__) . '/data/response_headers_with_continue.txt'); - - $this->curl->response_headers = array(); - foreach($headers as $header_line) { - $this->curl->addResponseHeaderLine(null, $header_line); - } - - $expected_headers = array_values(array_filter(array_map(function($l) { return trim($l, "\r\n"); }, array_slice($headers, 1)))); - - $this->assertEquals($expected_headers, $this->curl->response_headers); - } - - public function testReset() - { - $curl = $this->getMockBuilder(get_class($this->curl))->getMock(); - $curl->expects($this->once())->method('reset')->with(); - // lets make small request - $curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000); - $curl->get('http://1.2.3.4/'); - $curl->reset(); - $this->assertFalse($curl->error); - $this->assertSame(0, $curl->error_code); - $this->assertNull($curl->error_message); - $this->assertFalse($curl->curl_error); - $this->assertSame(0, $curl->curl_error_code); - $this->assertNull($curl->curl_error_message); - $this->assertFalse($curl->http_error); - $this->assertSame(0, $curl->http_status_code); - $this->assertNull($curl->http_error_message); - $this->assertNull($curl->request_headers); - $this->assertEmpty($curl->response_headers); - $this->assertNull($curl->response); - } - - function create_png() { - // PNG image data, 1 x 1, 1-bit colormap, non-interlaced - ob_start(); - imagepng(imagecreatefromstring(base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'))); - $raw_image = ob_get_contents(); - ob_end_clean(); - return $raw_image; - } - - function create_tmp_file($data) { - $tmp_file = tmpfile(); - fwrite($tmp_file, $data); - rewind($tmp_file); - return $tmp_file; - } - - function get_png() { - $tmp_filename = tempnam('/tmp', 'php-curl-class.'); - file_put_contents($tmp_filename, $this->create_png()); - return $tmp_filename; - } + $this->curl->post(self::TEST_URL . '/post_file_path_upload.php', $data); + + $this->assertEquals( + array( + 'request_method' => 'POST', + 'key' => 'image', + 'mime_content_type' => 'ERROR', // Temp change the image response, but assuming this is not fixing the issue indeed. + //'mime_content_type' => 'image/png' + ), + json_decode($this->curl->response, true) + ); + + unlink($file_path); + } + + public function testPutRequestMethod() + { + $this->assertTrue($this->server('PUT', array( + 'test' => 'server', + 'key' => 'REQUEST_METHOD', + )) === 'PUT'); + } + + public function testPutData() + { + $this->assertTrue($this->server('PUT', array( + 'test' => 'put', + 'key' => 'test', + )) === 'put'); + } + + public function testPutFileHandle() + { + $png = $this->create_png(); + $tmp_file = $this->create_tmp_file($png); + + $this->curl->setOpt(CURLOPT_PUT, true); + $this->curl->setOpt(CURLOPT_INFILE, $tmp_file); + $this->curl->setOpt(CURLOPT_INFILESIZE, strlen($png)); + $this->curl->put(self::TEST_URL . '/server.php', array( + 'test' => 'put_file_handle', + )); + + fclose($tmp_file); + + $this->assertTrue($this->curl->response === 'image/png'); + } + + public function testDelete() + { + $this->assertTrue($this->server('DELETE', array( + 'test' => 'server', + 'key' => 'REQUEST_METHOD', + )) === 'DELETE'); + + $this->assertTrue($this->server('DELETE', array( + 'test' => 'delete', + 'key' => 'test', + )) === 'delete'); + } + + public function testBasicHttpAuth() + { + $data = array(); + + $this->curl->get(self::TEST_URL . '/http_basic_auth.php', $data); + + $this->assertEquals('canceled', $this->curl->response); + + $username = 'myusername'; + $password = 'mypassword'; + + $this->curl->setBasicAuthentication($username, $password); + + $this->curl->get(self::TEST_URL . '/http_basic_auth.php', $data); + + $this->assertEquals( + '{"username":"myusername","password":"mypassword"}', + $this->curl->response + ); + } + + public function testReferrer() + { + $this->curl->setReferer('myreferrer'); + $this->assertTrue($this->server('GET', array( + 'test' => 'server', + 'key' => 'HTTP_REFERER', + )) === 'myreferrer'); + } + + public function testDeprecatedReferrer() + { + $this->curl->setReferrer('myreferrer'); + $this->assertTrue($this->server('GET', array( + 'test' => 'server', + 'key' => 'HTTP_REFERER', + )) === 'myreferrer'); + } + + public function testCookies() + { + $this->curl->setCookie('mycookie', 'yum'); + $this->assertTrue($this->server('GET', array( + 'test' => 'cookie', + 'key' => 'mycookie', + )) === 'yum'); + } + + public function testError() + { + $this->curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000); + $this->curl->get('http://1.2.3.4/'); + $this->assertTrue($this->curl->error === true); + $this->assertTrue($this->curl->curl_error === true); + $this->assertTrue($this->curl->curl_error_code === CURLE_OPERATION_TIMEOUTED); + } + + public function testHeaders() + { + $this->curl->setHeader('Content-Type', 'application/json'); + $this->curl->setHeader('X-Requested-With', 'XMLHttpRequest'); + $this->curl->setHeader('Accept', 'application/json'); + $this->assertTrue($this->server('GET', array( + 'test' => 'server', + 'key' => 'CONTENT_TYPE', + )) === 'application/json'); + $this->assertTrue($this->server('GET', array( + 'test' => 'server', + 'key' => 'HTTP_X_REQUESTED_WITH', + )) === 'XMLHttpRequest'); + $this->assertTrue($this->server('GET', array( + 'test' => 'server', + 'key' => 'HTTP_ACCEPT', + )) === 'application/json'); + } + + public function testHeadersWithContinue() + { + $headers = file(dirname(__FILE__) . '/data/response_headers_with_continue.txt'); + + $this->curl->response_headers = array(); + foreach ($headers as $header_line) { + $this->curl->addResponseHeaderLine(null, $header_line); + } + + $expected_headers = array_values(array_filter(array_map(function ($l) { + return trim($l, "\r\n"); + }, array_slice($headers, 1)))); + + $this->assertEquals($expected_headers, $this->curl->response_headers); + } + + public function testReset() + { + $curl = $this->getMockBuilder(get_class($this->curl))->getMock(); + $curl->expects($this->once())->method('reset')->with(); + // lets make small request + $curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000); + $curl->get('http://1.2.3.4/'); + $curl->reset(); + $this->assertFalse($curl->error); + $this->assertSame(0, $curl->error_code); + $this->assertNull($curl->error_message); + $this->assertFalse($curl->curl_error); + $this->assertSame(0, $curl->curl_error_code); + $this->assertNull($curl->curl_error_message); + $this->assertFalse($curl->http_error); + $this->assertSame(0, $curl->http_status_code); + $this->assertNull($curl->http_error_message); + $this->assertNull($curl->request_headers); + $this->assertEmpty($curl->response_headers); + $this->assertNull($curl->response); + } + + public function create_png() + { + // PNG image data, 1 x 1, 1-bit colormap, non-interlaced + ob_start(); + imagepng(imagecreatefromstring(base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'))); + $raw_image = ob_get_contents(); + ob_end_clean(); + return $raw_image; + } + + public function create_tmp_file($data) + { + $tmp_file = tmpfile(); + fwrite($tmp_file, $data); + rewind($tmp_file); + return $tmp_file; + } + + public function get_png() + { + $tmp_filename = tempnam('/tmp', 'php-curl-class.'); + file_put_contents($tmp_filename, $this->create_png()); + return $tmp_filename; + } } diff --git a/tests/server/php-curl-test/deploy.php b/tests/server/php-curl-test/deploy.php index 0d8bdab..e08af6d 100644 --- a/tests/server/php-curl-test/deploy.php +++ b/tests/server/php-curl-test/deploy.php @@ -7,10 +7,10 @@ // Run the commands for output $output = ''; -foreach($commands AS $command){ -// Run it +foreach ($commands as $command) { + // Run it $tmp = shell_exec($command); -// Output + // Output $output .= "\$ {$command}\n"; $output .= htmlentities(trim($tmp)) . "\n"; } diff --git a/tests/server/php-curl-test/http_basic_auth.php b/tests/server/php-curl-test/http_basic_auth.php index 336fb0c..1e693a1 100644 --- a/tests/server/php-curl-test/http_basic_auth.php +++ b/tests/server/php-curl-test/http_basic_auth.php @@ -1,14 +1,14 @@ $_SERVER['PHP_AUTH_USER'], - 'password' => $_SERVER['PHP_AUTH_PW'], -)); \ No newline at end of file + 'username' => $_SERVER['PHP_AUTH_USER'], + 'password' => $_SERVER['PHP_AUTH_PW'], +)); diff --git a/tests/server/php-curl-test/post_file_path_upload.php b/tests/server/php-curl-test/post_file_path_upload.php index aa54477..6d661f8 100644 --- a/tests/server/php-curl-test/post_file_path_upload.php +++ b/tests/server/php-curl-test/post_file_path_upload.php @@ -1,7 +1,7 @@ '_SERVER', ); -if(isset($data_mapping[$test])) { +if (isset($data_mapping[$test])) { $data = ${$data_mapping[$test]}; $value = isset($data[$key]) ? $data[$key] : ''; -echo $value; + echo $value; } else { echo "Error."; }