diff --git a/src/Client.php b/src/Client.php
index 3adbf4a..f7f46e1 100644
--- a/src/Client.php
+++ b/src/Client.php
@@ -72,7 +72,9 @@ protected function doRequestAsync($request)
$ch = $this->createCurl($request);
$content = (yield $ch);
yield CoInterface::RETURN_WITH => $this->processResult($content, $ch);
+ // @codeCoverageIgnoreStart
}
+ // @codeCoverageIgnoreEnd
protected function createCurl($request)
{
@@ -98,7 +100,7 @@ protected function createCurl($request)
}
$params = $request->getParameters();
$files = $request->getFiles();
- if (!$files) {
+ if (!$files && !self::containsCURLFile($params)) {
$content = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
return $ch;
@@ -143,6 +145,9 @@ protected function createHeaders($request)
continue;
}
}
+ if ($request->getContent() !== null && !preg_grep('/^content-type:/', $headers)) {
+ $headers[] = 'content-type: application/octet-stream';
+ }
return $headers;
}
@@ -170,12 +175,14 @@ protected function addMultipartFiles(array $files, array &$multipart, $arrayName
$multipart[$name] = new \CURLFile($info);
continue;
}
- if (!isset($info['tmp_name'])) {
+ if (!isset($info['tmp_name']) || is_array($info['tmp_name'])) {
$this->addMultipartFiles($info, $multipart, $name);
continue;
}
if ('' === $info['tmp_name']) {
+ // @codeCoverageIgnoreStart
continue;
+ // @codeCoverageIgnoreEnd
}
$multipart[$name] = new \CURLFile(
$info['tmp_name'],
@@ -198,4 +205,20 @@ protected function addMultipartFields(array $params, array &$multipart, $arrayNa
$this->addMultipartFields($value, $multipart, $name);
}
}
+
+ protected static function containsCURLFile(array $array)
+ {
+ foreach ($array as $item) {
+ if (is_array($item)) {
+ if (self::containsCURLFile($item)) {
+ return true;
+ }
+ continue;
+ }
+ if ($item instanceof \CURLFile) {
+ return true;
+ }
+ }
+ return false;
+ }
}
diff --git a/tests/assets/www/json.php b/tests/assets/www/json.php
new file mode 100644
index 0000000..31fbf37
--- /dev/null
+++ b/tests/assets/www/json.php
@@ -0,0 +1,9 @@
+
diff --git a/tests/assets/www/upload_form01.php b/tests/assets/www/upload_form01.php
new file mode 100644
index 0000000..d6d8b3f
--- /dev/null
+++ b/tests/assets/www/upload_form01.php
@@ -0,0 +1,11 @@
+
+
+
+SUCCESS
+
+ERROR
+
+
diff --git a/tests/assets/www/upload_form02.php b/tests/assets/www/upload_form02.php
new file mode 100644
index 0000000..ad48373
--- /dev/null
+++ b/tests/assets/www/upload_form02.php
@@ -0,0 +1,11 @@
+
+
+
+SUCCESS
+
+ERROR
+
+
diff --git a/tests/unit/ClientConfigTest.php b/tests/unit/ClientConfigTest.php
new file mode 100644
index 0000000..c1771df
--- /dev/null
+++ b/tests/unit/ClientConfigTest.php
@@ -0,0 +1,25 @@
+ false,
+ CURLOPT_SSL_VERIFYHOST => false,
+ ]);
+ $actual = array_intersect_key($client->getCurlOptions(), $expected);
+ $this->assertEquals($expected, $actual);
+ }
+}
diff --git a/tests/unit/ClientGetAndPostTest.php b/tests/unit/ClientGetAndPostTest.php
new file mode 100644
index 0000000..b0ad91b
--- /dev/null
+++ b/tests/unit/ClientGetAndPostTest.php
@@ -0,0 +1,51 @@
+ false,
+ CURLOPT_SSL_VERIFYHOST => false,
+ ]);
+ }
+
+ public function testInvalid()
+ {
+ $this->setExpectedException(CURLException::class);
+ $client = $this->getClient();
+ $client->request('GET', 'invalid');
+ }
+
+ public function testNormalPost()
+ {
+ $client = $this->getClient();
+ $expected = ['a' => 'b'];
+ $crawler = $client->request('POST', 'http://localhost:8080/json.php', $expected);
+ $json = json_decode($crawler->filter('.json')->text(), true);
+ $this->assertEquals($expected, $json['_POST']);
+ }
+
+ public function testRawPost()
+ {
+ $client = $this->getClient();
+ $expected = 'abcde';
+ $crawler = $client->request('POST', 'http://localhost:8080/json.php', [], [], [], $expected);
+ $json = json_decode($crawler->filter('.json')->text(), true);
+ $this->assertEquals([], $json['_POST']);
+ $this->assertEquals($expected, $json['rawpost']);
+ }
+
+}
diff --git a/tests/unit/ClientPayloadTest.php b/tests/unit/ClientPayloadTest.php
new file mode 100644
index 0000000..629d68a
--- /dev/null
+++ b/tests/unit/ClientPayloadTest.php
@@ -0,0 +1,56 @@
+ false,
+ CURLOPT_SSL_VERIFYHOST => false,
+ ]);
+ }
+
+ public function testPayloadHttp()
+ {
+ $client = $this->getClient();
+ $client->request('GET', 'http://localhost:8080/hello.php');
+ $this->assertEquals('Hello', $client->getResponse()->getContent());
+ }
+
+ public function testPayloadHttps()
+ {
+ $client = $this->getClient();
+ $client->request('GET', 'https://localhost:8081/hello.php');
+ $this->assertEquals('Hello', $client->getResponse()->getContent());
+ }
+
+ public function testAsyncPayloadHttp()
+ {
+ Co::wait(function () {
+ $client = $this->getClient();
+ yield $client->requestAsync('GET', 'http://localhost:8080/hello.php');
+ $this->assertEquals('Hello', $client->getResponse()->getContent());
+ });
+ }
+
+ public function testAsyncPayloadHttps()
+ {
+ Co::wait(function () {
+ $client = $this->getClient();
+ yield $client->requestAsync('GET', 'https://localhost:8081/hello.php');
+ $this->assertEquals('Hello', $client->getResponse()->getContent());
+ });
+ }
+
+}
diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php
deleted file mode 100644
index 2d8088e..0000000
--- a/tests/unit/ClientTest.php
+++ /dev/null
@@ -1,39 +0,0 @@
- false,
- CURLOPT_SSL_VERIFYHOST => false,
- ]);
- }
-
- public function testPayloadHttp()
- {
- $c = $this->getClient();
- $c->request('GET', 'http://localhost:8080/hello.php');
- $this->assertEquals('Hello', $c->getResponse()->getContent());
- }
-
- public function testPayloadHttps()
- {
- $c = $this->getClient();
- $c->request('GET', 'https://localhost:8081/hello.php');
- $this->assertEquals('Hello', $c->getResponse()->getContent());
- }
-
-}
diff --git a/tests/unit/ClientUploadTest.php b/tests/unit/ClientUploadTest.php
new file mode 100644
index 0000000..699abb7
--- /dev/null
+++ b/tests/unit/ClientUploadTest.php
@@ -0,0 +1,106 @@
+ false,
+ CURLOPT_SSL_VERIFYHOST => false,
+ ]);
+ }
+
+ public function testUpload01()
+ {
+ $client = $this->getClient();
+ $crawler = $client->request('GET', 'http://localhost:8080/upload_form01.php');
+ $form = $crawler->filter('form')->form([
+ 'file[x][y]' => __FILE__,
+ ]);
+ $crawler = $client->submit($form);
+ $this->assertEquals('SUCCESS', $crawler->filter('#success')->text());
+ }
+
+ public function testUpload02()
+ {
+ $client = $this->getClient();
+ $crawler = $client->request('GET', 'http://localhost:8080/upload_form02.php');
+ $form = $crawler->filter('form')->form([
+ 'file[tmp_name][y]' => __FILE__,
+ ]);
+ $crawler = $client->submit($form);
+ $this->assertEquals('SUCCESS', $crawler->filter('#success')->text());
+ }
+
+ public function testUpload03()
+ {
+ $this->setExpectedException(
+ PHPUnit_Framework_Exception::class,
+ 'is_readable() expects parameter 1 to be a valid path, object given'
+ );
+ $client = $this->getClient();
+ $crawler = $client->request('GET', 'http://localhost:8080/upload_form01.php');
+ $form = $crawler->filter('form')->form([
+ 'file[x][y]' => new CURLFile(__FILE__),
+ ]);
+ }
+
+ public function testUpload04()
+ {
+ $client = $this->getClient();
+ $crawler = $client->request(
+ 'POST',
+ 'http://localhost:8080/upload_form01.php',
+ [],
+ [
+ 'file[x]' => [
+ 'y' => new CURLFile(__FILE__)
+ ]
+ ]
+ );
+ $this->assertEquals('SUCCESS', $crawler->filter('#success')->text());
+ }
+
+ public function testUpload05()
+ {
+ $client = $this->getClient();
+ $crawler = $client->request(
+ 'POST',
+ 'http://localhost:8080/upload_form01.php',
+ [],
+ [
+ 'file[x]' => [
+ 'y' => __FILE__,
+ ]
+ ]
+ );
+ $this->assertEquals('SUCCESS', $crawler->filter('#success')->text());
+ }
+
+ public function testUpload06()
+ {
+ $client = $this->getClient();
+ $crawler = $client->request(
+ 'POST',
+ 'http://localhost:8080/upload_form01.php',
+ [
+ [],
+ 'file[x]' => [
+ 'y' => new CURLFile(__FILE__)
+ ]
+ ]
+ );
+ $this->assertEquals('SUCCESS', $crawler->filter('#success')->text());
+ }
+}