From 3fe293851698d92b5da33091dfed273990190767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 15 May 2017 23:51:15 +0200 Subject: [PATCH] Simplify buffering tests by using new react/promise-stream release --- composer.json | 1 + tests/FunctionalServerTest.php | 69 ++++++++++++---------------------- 2 files changed, 24 insertions(+), 46 deletions(-) diff --git a/composer.json b/composer.json index 2568f2d8..c3f4b559 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "require-dev": { "phpunit/phpunit": "^4.8.10||^5.0", "react/socket": "^1.0 || ^0.8 || ^0.7", + "react/promise-stream": "^0.1.1", "clue/block-react": "^1.1" } } diff --git a/tests/FunctionalServerTest.php b/tests/FunctionalServerTest.php index 042c4585..cc1344e1 100644 --- a/tests/FunctionalServerTest.php +++ b/tests/FunctionalServerTest.php @@ -15,6 +15,7 @@ use React\EventLoop\LoopInterface; use React\Promise\Promise; use React\Promise\PromiseInterface; +use React\Promise\Stream; class FunctionalServerTest extends TestCase { @@ -31,10 +32,10 @@ public function testPlainHttpOnRandomPort() $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('http://' . noScheme($socket->getAddress()) . '/', $response); @@ -55,10 +56,10 @@ public function testPlainHttpOnRandomPortWithoutHostHeaderUsesSocketUri() $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('http://' . noScheme($socket->getAddress()) . '/', $response); @@ -79,10 +80,10 @@ public function testPlainHttpOnRandomPortWithOtherHostHeaderTakesPrecedence() $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: localhost:1000\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('http://localhost:1000/', $response); @@ -112,10 +113,10 @@ public function testSecureHttpsOnRandomPort() $result = $connector->connect('tls://' . noScheme($socket->getAddress()))->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('https://' . noScheme($socket->getAddress()) . '/', $response); @@ -145,10 +146,10 @@ public function testSecureHttpsOnRandomPortWithoutHostHeaderUsesSocketUri() $result = $connector->connect('tls://' . noScheme($socket->getAddress()))->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('https://' . noScheme($socket->getAddress()) . '/', $response); @@ -173,10 +174,10 @@ public function testPlainHttpOnStandardPortReturnsUriWithNoPort() $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('http://127.0.0.1/', $response); @@ -201,10 +202,10 @@ public function testPlainHttpOnStandardPortWithoutHostHeaderReturnsUriWithNoPort $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('http://127.0.0.1/', $response); @@ -238,10 +239,10 @@ public function testSecureHttpsOnStandardPortReturnsUriWithNoPort() $result = $connector->connect('tls://' . noScheme($socket->getAddress()))->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('https://127.0.0.1/', $response); @@ -275,10 +276,10 @@ public function testSecureHttpsOnStandardPortWithoutHostHeaderUsesSocketUri() $result = $connector->connect('tls://' . noScheme($socket->getAddress()))->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('https://127.0.0.1/', $response); @@ -303,10 +304,10 @@ public function testPlainHttpOnHttpsStandardPortReturnsUriWithPort() $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('http://127.0.0.1:443/', $response); @@ -340,40 +341,16 @@ public function testSecureHttpsOnHttpStandardPortReturnsUriWithPort() $result = $connector->connect('tls://' . noScheme($socket->getAddress()))->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); - return $conn; + return Stream\buffer($conn); }); - $response = $this->buffer($result, $loop, 1.0); + $response = Block\await($result, $loop, 1.0); $this->assertContains("HTTP/1.0 200 OK", $response); $this->assertContains('https://127.0.0.1:80/', $response); $socket->close(); } - - protected function buffer(PromiseInterface $promise, LoopInterface $loop, $timeout) - { - return Block\await($promise->then(function (ReadableStreamInterface $stream) { - return new Promise( - function ($resolve, $reject) use ($stream) { - $buffer = ''; - $stream->on('data', function ($chunk) use (&$buffer) { - $buffer .= $chunk; - }); - - $stream->on('error', $reject); - - $stream->on('close', function () use (&$buffer, $resolve) { - $resolve($buffer); - }); - }, - function () use ($stream) { - $stream->close(); - throw new \RuntimeException(); - } - ); - }), $loop, $timeout); - } } function noScheme($uri)