diff --git a/src/Application.php b/src/Application.php index cd80fe40..4b6e6805 100644 --- a/src/Application.php +++ b/src/Application.php @@ -41,30 +41,35 @@ public function run() list($request, $response) = $this->components->getHttp(); - $route = $this->getRoute($request); + $result = $this->getResponse($request); - if ($route instanceof ResponseInterface) { - $response = $route; + if ($result instanceof ResponseInterface) { + $response = $result; } - // Sets the HTTP response code. - http_response_code($response->getStatusCode()); - // Gets the selected route and sets it as the content. if ( ! $response->getBody() || $response->getBody() == '') { - $response->getBody()->write($route); + $response->getBody()->write($result); + } + + // Sets the specified headers, if any. + foreach ($response->getHeaders() as $name => $value) { + header($name . ': ' . implode(',', $value)); } + // Sets the HTTP response code. + http_response_code($response->getStatusCode()); + echo $response->getBody(); } /** - * Gets the route result from the dispatcher. + * Gets the response from the dispatcher. * * @param \Psr\Http\Message\RequestInterface $request - * @return string + * @return mixed */ - private function getRoute(RequestInterface $request) + private function getResponse(RequestInterface $request) { // Gets the requested route $route = $this->components->getDispatcher()->dispatch( diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index 337df648..09b44b05 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -118,12 +118,13 @@ public function testRunMethod() $application = new Application($this->components); - echo $application->run(); + $application->run(); } /** * Tests the run() method with a response as result. * + * @runInSeparateProcess * @return void */ public function testRunMethodWithResponse() @@ -134,7 +135,7 @@ public function testRunMethodWithResponse() $application = new Application($this->components); - echo $application->run(); + $application->run(); } /** @@ -150,7 +151,7 @@ public function testRunMethodWithCallback() $application = new Application($this->components); - echo $application->run(); + $application->run(); } /** diff --git a/tests/Fixture/TestClassWithResponseInterface.php b/tests/Fixture/TestClassWithResponseInterface.php index 3f3afdea..f26fccf8 100644 --- a/tests/Fixture/TestClassWithResponseInterface.php +++ b/tests/Fixture/TestClassWithResponseInterface.php @@ -27,8 +27,13 @@ public function __construct(ResponseInterface $response) public function index() { - $this->response->getBody()->write('Hello'); + $response = $this->response + ->withStatus(200) + ->withHeader('Content-Type', 'application/json') + ->withHeader('Access-Control-Allow-Credentials', 'true'); - return $this->response; + $response->getBody()->write('Hello'); + + return $response; } }