Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ private function runLoop()
return $this->handleRequest($request);
});

$listen = \getenv('X_LISTEN');
if ($listen === false) {
$listen = '127.0.0.1:8080';
}
$listen = $_SERVER['X_LISTEN'] ?? '127.0.0.1:8080';

$socket = new SocketServer($listen);
$http->listen($socket);
Expand Down
31 changes: 27 additions & 4 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@

class AppTest extends TestCase
{
/**
* @var array
*/
private $serverArgs;

protected function setUp(): void
{
// Store a snapshot of $_SERVER
$this->serverArgs = $_SERVER;
}

protected function tearDown(): void
{
// Restore $_SERVER as it was before
foreach ($_SERVER as $key => $value) {
if (!\array_key_exists($key, $this->serverArgs)) {
unset($_SERVER[$key]);
continue;
}
$_SERVER[$key] = $value;
}
}

public function testConstructWithMiddlewareAssignsGivenMiddleware()
{
$middleware = function () { };
Expand Down Expand Up @@ -74,7 +97,7 @@ public function testRunWillReportListeningAddressFromEnvironmentAndRunLoopWithSo
$addr = stream_socket_get_name($socket, false);
fclose($socket);

putenv('X_LISTEN=' . $addr);
$_SERVER['X_LISTEN'] = $addr;
$app = new App();

// lovely: remove socket server on next tick to terminate loop
Expand All @@ -92,7 +115,7 @@ public function testRunWillReportListeningAddressFromEnvironmentAndRunLoopWithSo

public function testRunWillReportListeningAddressFromEnvironmentWithRandomPortAndRunLoopWithSocketServer()
{
putenv('X_LISTEN=127.0.0.1:0');
$_SERVER['X_LISTEN'] = '127.0.0.1:0';
$app = new App();

// lovely: remove socket server on next tick to terminate loop
Expand All @@ -110,7 +133,7 @@ public function testRunWillReportListeningAddressFromEnvironmentWithRandomPortAn

public function testRunAppWithEmptyAddressThrows()
{
putenv('X_LISTEN=');
$_SERVER['X_LISTEN'] = '';
$app = new App();

$this->expectException(\InvalidArgumentException::class);
Expand All @@ -126,7 +149,7 @@ public function testRunAppWithBusyPortThrows()
$this->markTestSkipped('System does not prevent listening on same address twice');
}

putenv('X_LISTEN=' . $addr);
$_SERVER['X_LISTEN'] = $addr;
$app = new App();

$this->expectException(\RuntimeException::class);
Expand Down