Skip to content

Commit

Permalink
Make sure each closure returns a valid response
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderdlm committed Nov 26, 2023
1 parent 6e0aa7b commit b738050
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Mono.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ public function run(): void
default => $this->createResponse('500 Internal Server Error', 500)
};

if ($response instanceof ResponseInterface) {
(new SapiEmitter())->emit($response);
if (!$response instanceof ResponseInterface) {
throw new \RuntimeException('Invalid response received from route ' . $request->getUri()->getPath() .
'. Please return a valid PSR-7 response from your handler.');
}

(new SapiEmitter())->emit($response);
}
}
18 changes: 18 additions & 0 deletions tests/MonoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,26 @@ public function testDependencyInjection(): void
$demoClass = $mono->get(NodeTraverser::class);

$this->assertInstanceOf(NodeTraverser::class, $demoClass);

return $mono->createResponse('OK');
});

$mono->run();
}

public function testInvalidResponseThrowError(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/';

$mono = new Mono(__DIR__);

$mono->addRoute('GET', '/', function () {
return 'OK';
});

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

$mono->run();
}
}

0 comments on commit b738050

Please sign in to comment.