Skip to content

Commit

Permalink
Simplify buffering tests by using new react/promise-stream release
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed May 15, 2017
1 parent 5a75c3b commit 3fe2938
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 46 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
69 changes: 23 additions & 46 deletions tests/FunctionalServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use React\EventLoop\LoopInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use React\Promise\Stream;

class FunctionalServerTest extends TestCase
{
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3fe2938

Please sign in to comment.