From b81294cbb1542bfe0a29682c257783e588ee82be Mon Sep 17 00:00:00 2001 From: moznion Date: Fri, 18 Nov 2016 12:25:40 +0900 Subject: [PATCH 1/9] Append `Content-Length: 0` to request header when body of POST is empty Fix #35 --- src/LINEBot/HTTPClient/CurlHTTPClient.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/LINEBot/HTTPClient/CurlHTTPClient.php b/src/LINEBot/HTTPClient/CurlHTTPClient.php index 2bd47eb9..e63e7252 100644 --- a/src/LINEBot/HTTPClient/CurlHTTPClient.php +++ b/src/LINEBot/HTTPClient/CurlHTTPClient.php @@ -95,8 +95,13 @@ private function sendRequest($method, $url, array $additionalHeader, array $reqB CURLOPT_HEADER => true, ]; - if ($method === 'POST' && !empty($reqBody)) { - $options[CURLOPT_POSTFIELDS] = json_encode($reqBody); + if ($method === 'POST') { + if (empty($reqBody)) { + // Rel: https://github.com/line/line-bot-sdk-php/issues/35 + $option[CURLOPT_HTTPHEADER][] = 'Content-Length: 0'; + } else { + $options[CURLOPT_POSTFIELDS] = json_encode($reqBody); + } } $curl->setoptArray($options); From b26583eea8b2add9ad497aebf3f5068909404423 Mon Sep 17 00:00:00 2001 From: moznion Date: Mon, 21 Nov 2016 13:34:50 +0900 Subject: [PATCH 2/9] Add bootstrap for req_mirror --- .gitignore | 1 + Makefile | 5 ++++- devtool/download_req_mirror.sh | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100755 devtool/download_req_mirror.sh diff --git a/.gitignore b/.gitignore index b35a9758..d3eaf6d0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ composer.phar /docs/ *.iml vendor/ +devtool/req_mirror diff --git a/Makefile b/Makefile index 2ff1bfd8..4e40c819 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ COMPOSER_BIN = ./vendor/bin -.PHONY: default test doc phpcs phpmd check +.PHONY: default test doc phpcs phpmd check install-devtool default: check @@ -26,3 +26,6 @@ copyright: check: test copyright phpcs phpmd +install-devtool: + bash ./devtool/download_req_mirror.sh + diff --git a/devtool/download_req_mirror.sh b/devtool/download_req_mirror.sh new file mode 100755 index 00000000..e4c0c0eb --- /dev/null +++ b/devtool/download_req_mirror.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +set -eu + +function die() { + echo 'Unsupported machine environment, please try go build your self: https://github.com/moznion/req_mirror' + exit 1 +} + +VER='0.0.1' +OS="$(tr '[A-Z]' '[a-z]' <<<$(uname))" +ARCH="$(tr '[A-Z]' '[a-z]' <<<$(arch))" + +if [ ${ARCH} = 'i386' ] ; then + ARCH='386' +elif [ ${ARCH} = 'x86_64' ] ; then + ARCH='amd64' +fi + +BIN_URL="https://github.com/moznion/req_mirror/releases/download/${VER}/req_mirror_${OS}_${ARCH}_${VER}" +BIN="$(pwd)/$(git rev-parse --show-cdup)/devtool/req_mirror" + +curl --fail --location --output ${BIN} ${BIN_URL} + +chmod 755 ${BIN} + From ab8a511c3fd784c10fc8356063406fdd2655a11a Mon Sep 17 00:00:00 2001 From: moznion Date: Mon, 21 Nov 2016 14:58:09 +0900 Subject: [PATCH 3/9] Oops, fix typo --- src/LINEBot/HTTPClient/CurlHTTPClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LINEBot/HTTPClient/CurlHTTPClient.php b/src/LINEBot/HTTPClient/CurlHTTPClient.php index e63e7252..8b6eb45e 100644 --- a/src/LINEBot/HTTPClient/CurlHTTPClient.php +++ b/src/LINEBot/HTTPClient/CurlHTTPClient.php @@ -98,7 +98,7 @@ private function sendRequest($method, $url, array $additionalHeader, array $reqB if ($method === 'POST') { if (empty($reqBody)) { // Rel: https://github.com/line/line-bot-sdk-php/issues/35 - $option[CURLOPT_HTTPHEADER][] = 'Content-Length: 0'; + $options[CURLOPT_HTTPHEADER][] = 'Content-Length: 0'; } else { $options[CURLOPT_POSTFIELDS] = json_encode($reqBody); } From db17b21a3143ed0f8679c0939b2524bbae32583a Mon Sep 17 00:00:00 2001 From: moznion Date: Mon, 21 Nov 2016 14:58:19 +0900 Subject: [PATCH 4/9] Add tests for CurlHTTPClient --- .../LINEBot/HTTPClient/CurlHTTPClientTest.php | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 tests/LINEBot/HTTPClient/CurlHTTPClientTest.php diff --git a/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php b/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php new file mode 100644 index 00000000..d2817948 --- /dev/null +++ b/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php @@ -0,0 +1,108 @@ + /dev/null & echo $!', + CurlHTTPClientTest::$reqMirrorPath, + CurlHTTPClientTest::$reqMirrorPort + ); + exec($cmd, $out); + CurlHTTPClientTest::$reqMirrorPID = $out[0]; + sleep(1); // XXX + return; + } + + echo <<<"EOS" +\n[WARN] req_mirror does not exist. Please try to execute `make install-devtool` +[WARN] Skip CurlHTTPClientTest\n +EOS; + } + + public static function tearDownAfterClass() + { + if (!empty(CurlHTTPClientTest::$reqMirrorPID)) { + posix_kill(CurlHTTPClientTest::$reqMirrorPID, SIGKILL); + } + } + + protected function setUp() + { + if (!file_exists(CurlHTTPClientTest::$reqMirrorPath) || empty(CurlHTTPClientTest::$reqMirrorPID)) { + $this->markTestSkipped('req_mirror is not available'); + } + } + + public function testGet() + { + $curl = new CurlHTTPClient("channel-token"); + $res = $curl->get('127.0.0.1:' . CurlHTTPClientTest::$reqMirrorPort . '/foo/bar?buz=qux'); + $body = $res->getJSONDecodedBody(); + + $this->assertEquals('GET', $body['Method']); + $this->assertEquals('/foo/bar', $body['URL']['Path']); + $this->assertEquals('buz=qux', $body['URL']['RawQuery']); + $this->assertEquals('Bearer channel-token', $body['Header']['Authorization'][0]); + $this->assertEquals('LINE-BotSDK-PHP/' . Meta::VERSION, $body['Header']['User-Agent'][0]); + } + + public function testPost() + { + $curl = new CurlHTTPClient("channel-token"); + $res = $curl->post('127.0.0.1:' . CurlHTTPClientTest::$reqMirrorPort, ['foo' => 'bar']); + $body = $res->getJSONDecodedBody(); + $this->assertEquals('POST', $body['Method']); + $this->assertEquals('/', $body['URL']['Path']); + $this->assertEquals('{"foo":"bar"}', $body['Body']); + $this->assertEquals(13, $body['Header']['Content-Length'][0]); + $this->assertEquals('Bearer channel-token', $body['Header']['Authorization'][0]); + $this->assertEquals('LINE-BotSDK-PHP/' . Meta::VERSION, $body['Header']['User-Agent'][0]); + } + + public function testPostWithEmptyBody() + { + $curl = new CurlHTTPClient("channel-token"); + $res = $curl->post('127.0.0.1:' . CurlHTTPClientTest::$reqMirrorPort, []); + $body = $res->getJSONDecodedBody(); + $this->assertEquals('POST', $body['Method']); + $this->assertEquals('/', $body['URL']['Path']); + $this->assertEquals('', $body['Body']); + $this->assertEquals(0, $body['Header']['Content-Length'][0]); + $this->assertEquals('Bearer channel-token', $body['Header']['Authorization'][0]); + $this->assertEquals('LINE-BotSDK-PHP/' . Meta::VERSION, $body['Header']['User-Agent'][0]); + } +} From 7a5cf3427301a522823751d0b77841c578534478 Mon Sep 17 00:00:00 2001 From: moznion Date: Mon, 21 Nov 2016 14:59:03 +0900 Subject: [PATCH 5/9] Update travis configuration --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 235dc1e2..c91c11d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,9 @@ language: php php: - '5.6' - '7.0' -install: composer update +install: + - ./devtool/download_req_mirror.sh + - composer update script: ./vendor/bin/phpunit ./tests sudo: false From bd0b20f409e91d6dcb1f5caeaaaa3a2f38aad7e5 Mon Sep 17 00:00:00 2001 From: moznion Date: Mon, 21 Nov 2016 15:03:37 +0900 Subject: [PATCH 6/9] Remove unused function --- devtool/download_req_mirror.sh | 5 ----- 1 file changed, 5 deletions(-) diff --git a/devtool/download_req_mirror.sh b/devtool/download_req_mirror.sh index e4c0c0eb..21d4c0ff 100755 --- a/devtool/download_req_mirror.sh +++ b/devtool/download_req_mirror.sh @@ -2,11 +2,6 @@ set -eu -function die() { - echo 'Unsupported machine environment, please try go build your self: https://github.com/moznion/req_mirror' - exit 1 -} - VER='0.0.1' OS="$(tr '[A-Z]' '[a-z]' <<<$(uname))" ARCH="$(tr '[A-Z]' '[a-z]' <<<$(arch))" From 04ab4e7d9014f26609f5f1e9926e0882c3e88289 Mon Sep 17 00:00:00 2001 From: moznion Date: Mon, 21 Nov 2016 15:14:33 +0900 Subject: [PATCH 7/9] Avoid to substitute static members many times --- .../LINEBot/HTTPClient/CurlHTTPClientTest.php | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php b/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php index d2817948..63b17fee 100644 --- a/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php +++ b/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php @@ -30,20 +30,25 @@ class CurlHTTPClientTest extends \PHPUnit_Framework_TestCase public static function setUpBeforeClass() { if (file_exists(CurlHTTPClientTest::$reqMirrorPath)) { - $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); - socket_bind($sock, '127.0.0.1', 0); - socket_getsockname($sock, $address, CurlHTTPClientTest::$reqMirrorPort); - socket_close($sock); + if (empty(CurlHTTPClientTest::$reqMirrorPort)) { + $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + socket_bind($sock, '127.0.0.1', 0); + socket_getsockname($sock, $address, CurlHTTPClientTest::$reqMirrorPort); + socket_close($sock); + } + + if (empty(CurlHTTPClientTest::$reqMirrorPID)) { + $out = []; + $cmd = sprintf( + 'nohup %s --port %d > /dev/null & echo $!', + CurlHTTPClientTest::$reqMirrorPath, + CurlHTTPClientTest::$reqMirrorPort + ); + exec($cmd, $out); + CurlHTTPClientTest::$reqMirrorPID = $out[0]; + sleep(1); // XXX + } - $out = []; - $cmd = sprintf( - 'nohup %s --port %d > /dev/null & echo $!', - CurlHTTPClientTest::$reqMirrorPath, - CurlHTTPClientTest::$reqMirrorPort - ); - exec($cmd, $out); - CurlHTTPClientTest::$reqMirrorPID = $out[0]; - sleep(1); // XXX return; } From 6c403f9cb1b9b7a9759fad3fa666a8f8f769b30a Mon Sep 17 00:00:00 2001 From: moznion Date: Mon, 21 Nov 2016 16:24:26 +0900 Subject: [PATCH 8/9] Use PHP_EOL --- tests/LINEBot/HTTPClient/CurlHTTPClientTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php b/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php index 63b17fee..a1523558 100644 --- a/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php +++ b/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php @@ -52,10 +52,8 @@ public static function setUpBeforeClass() return; } - echo <<<"EOS" -\n[WARN] req_mirror does not exist. Please try to execute `make install-devtool` -[WARN] Skip CurlHTTPClientTest\n -EOS; + echo PHP_EOL, '[WARN] req_mirror does not exist. Please try to execute `make install-devtool`', PHP_EOL; + echo '[WARN] Skip CurlHTTPClientTest', PHP_EOL; } public static function tearDownAfterClass() From b47c68070cd72a49ea558f622e18925d4ee41f6c Mon Sep 17 00:00:00 2001 From: moznion Date: Mon, 21 Nov 2016 17:32:40 +0900 Subject: [PATCH 9/9] Change behavior of tests when req_mirror isn't available Make them to be failed --- tests/LINEBot/HTTPClient/CurlHTTPClientTest.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php b/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php index a1523558..41e6f28e 100644 --- a/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php +++ b/tests/LINEBot/HTTPClient/CurlHTTPClientTest.php @@ -48,12 +48,7 @@ public static function setUpBeforeClass() CurlHTTPClientTest::$reqMirrorPID = $out[0]; sleep(1); // XXX } - - return; } - - echo PHP_EOL, '[WARN] req_mirror does not exist. Please try to execute `make install-devtool`', PHP_EOL; - echo '[WARN] Skip CurlHTTPClientTest', PHP_EOL; } public static function tearDownAfterClass() @@ -66,7 +61,7 @@ public static function tearDownAfterClass() protected function setUp() { if (!file_exists(CurlHTTPClientTest::$reqMirrorPath) || empty(CurlHTTPClientTest::$reqMirrorPID)) { - $this->markTestSkipped('req_mirror is not available'); + $this->fail('req_mirror server is not available. Please try to execute `make install-devtool`'); } }