summaryrefslogtreecommitdiff
path: root/php-src
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-06-11 14:57:47 +0300
committerWolfy-J <[email protected]>2018-06-11 14:57:47 +0300
commit846cec64f177a9cfef016a8225c4fae0faa29a0c (patch)
treea0dc1df41ab25793a2dd5ada80f3ca5de4615e7d /php-src
parent78fb74cfa5c0156f70c095a4f2cfee9851a9dc0f (diff)
more tests for http
Diffstat (limited to 'php-src')
-rw-r--r--php-src/PSR7Client.php42
-rw-r--r--php-src/tests/http/client.php45
-rw-r--r--php-src/tests/http/echo.php10
-rw-r--r--php-src/tests/http/header.php9
4 files changed, 85 insertions, 21 deletions
diff --git a/php-src/PSR7Client.php b/php-src/PSR7Client.php
index 0f618d9e..c3cdf844 100644
--- a/php-src/PSR7Client.php
+++ b/php-src/PSR7Client.php
@@ -9,17 +9,10 @@ namespace Spiral\RoadRunner;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
-use Spiral\RoadRunner\Worker;
-use Zend\Diactoros\ServerRequest;
-use Zend\Diactoros\Stream;
-use Zend\Diactoros\UploadedFile;
+use Zend\Diactoros;
/**
- * Spiral Framework, SpiralScout LLC.
- *
- * @package spiralFramework
- * @author Anton Titov (Wolfy-J)
- * @copyright ©2009-2011
+ * Manages PSR-7 request and response.
*/
class PSR7Client
{
@@ -67,11 +60,11 @@ class PSR7Client
if ($ctx['parsed']) {
$parsedBody = json_decode($body, true);
} elseif ($body != null) {
- $bodyStream = new Stream("php://memory", "rwb");
+ $bodyStream = new Diactoros\Stream("php://memory", "rwb");
$bodyStream->write($body);
}
- return new ServerRequest(
+ return new Diactoros\ServerRequest(
$_SERVER,
$this->wrapUploads($ctx['uploads']),
$ctx['uri'],
@@ -92,9 +85,15 @@ class PSR7Client
*/
public function respond(ResponseInterface $response)
{
+ $headers = $response->getHeaders();
+ if (empty($headers)) {
+ // this is required to represent empty header set as map and not as array
+ $headers = new \stdClass();
+ }
+
$this->worker->send($response->getBody(), json_encode([
'status' => $response->getStatusCode(),
- 'headers' => $response->getHeaders()
+ 'headers' => $headers
]));
}
@@ -113,18 +112,19 @@ class PSR7Client
$result = [];
foreach ($files as $index => $file) {
- if (isset($file['name'])) {
- $result[$index] = new UploadedFile(
- $file['tmpName'],
- $file['size'],
- $file['error'],
- $file['name'],
- $file['type']
- );
+ if (!isset($file['name'])) {
+ $result[$index] = $this->wrapUploads($file);
continue;
}
- $result[$index] = $this->wrapUploads($file);
+
+ $result[$index] = new Diactoros\UploadedFile(
+ $file['tmpName'],
+ $file['size'],
+ $file['error'],
+ $file['name'],
+ $file['type']
+ );
}
return $result;
diff --git a/php-src/tests/http/client.php b/php-src/tests/http/client.php
new file mode 100644
index 00000000..3b6b5dc6
--- /dev/null
+++ b/php-src/tests/http/client.php
@@ -0,0 +1,45 @@
+<?php
+
+use Spiral\Goridge;
+use Spiral\RoadRunner;
+
+ini_set('display_errors', 'stderr');
+require dirname(__DIR__) . "/../../vendor/autoload.php";
+
+if (count($argv) < 3) {
+ die("need 2 arguments");
+}
+
+list($test, $goridge) = [$argv[1], $argv[2]];
+
+switch ($goridge) {
+ case "pipes":
+ $relay = new Goridge\StreamRelay(STDIN, STDOUT);
+ break;
+
+ case "tcp":
+ $relay = new Goridge\SocketRelay("localhost", 9007);
+ break;
+
+ case "unix":
+ $relay = new Goridge\SocketRelay(
+ "sock.unix",
+ null,
+ Goridge\SocketRelay::SOCK_UNIX
+ );
+ break;
+
+ default:
+ die("invalid protocol selection");
+}
+
+$psr7 = new RoadRunner\PSR7Client(new RoadRunner\Worker($relay));
+require_once sprintf("%s/%s.php", __DIR__, $test);
+
+while ($req = $psr7->acceptRequest()) {
+ try {
+ $psr7->respond(handleRequest($req, new \Zend\Diactoros\Response()));
+ } catch (\Throwable $e) {
+ $psr7->getWorker()->error((string)$e);
+ }
+}
diff --git a/php-src/tests/http/echo.php b/php-src/tests/http/echo.php
new file mode 100644
index 00000000..eeccc7e5
--- /dev/null
+++ b/php-src/tests/http/echo.php
@@ -0,0 +1,10 @@
+<?php
+
+use \Psr\Http\Message\ServerRequestInterface;
+use \Psr\Http\Message\ResponseInterface;
+
+function handleRequest(ServerRequestInterface $req, ResponseInterface $resp): ResponseInterface
+{
+ $resp->getBody()->write(strtoupper($req->getQueryParams()['hello']));
+ return $resp;
+} \ No newline at end of file
diff --git a/php-src/tests/http/header.php b/php-src/tests/http/header.php
new file mode 100644
index 00000000..9bb15ccd
--- /dev/null
+++ b/php-src/tests/http/header.php
@@ -0,0 +1,9 @@
+<?php
+
+use \Psr\Http\Message\ServerRequestInterface;
+use \Psr\Http\Message\ResponseInterface;
+
+function handleRequest(ServerRequestInterface $req, ResponseInterface $resp): ResponseInterface
+{
+ return $resp->withAddedHeader("Header", $req->getQueryParams()['hello']);
+} \ No newline at end of file