Skip to content

Commit

Permalink
Merge pull request #38 from line/fix/#35
Browse files Browse the repository at this point in the history
Append `Content-Length: 0` to request header when body of POST is empty
  • Loading branch information
Vaduz authored Nov 21, 2016
2 parents a15f237 + b47c680 commit 7438b25
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ composer.phar
/docs/
*.iml
vendor/
devtool/req_mirror
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -26,3 +26,6 @@ copyright:

check: test copyright phpcs phpmd

install-devtool:
bash ./devtool/download_req_mirror.sh

21 changes: 21 additions & 0 deletions devtool/download_req_mirror.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

set -eu

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}

9 changes: 7 additions & 2 deletions src/LINEBot/HTTPClient/CurlHTTPClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
$options[CURLOPT_HTTPHEADER][] = 'Content-Length: 0';
} else {
$options[CURLOPT_POSTFIELDS] = json_encode($reqBody);
}
}

$curl->setoptArray($options);
Expand Down
106 changes: 106 additions & 0 deletions tests/LINEBot/HTTPClient/CurlHTTPClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* Copyright 2016 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

namespace LINE\Tests\LINEBot\HTTPClient;

use LINE\LINEBot\Constant\Meta;
use LINE\LINEBot\HTTPClient\CurlHTTPClient;

class CurlHTTPClientTest extends \PHPUnit_Framework_TestCase
{
private static $reqMirrorPath = __DIR__ . '/../../../devtool/req_mirror';
private static $reqMirrorPort;
private static $reqMirrorPID;

public static function setUpBeforeClass()
{
if (file_exists(CurlHTTPClientTest::$reqMirrorPath)) {
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
}
}
}

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->fail('req_mirror server is not available. Please try to execute `make install-devtool`');
}
}

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]);
}
}

0 comments on commit 7438b25

Please sign in to comment.