Skip to content

Commit

Permalink
Merge pull request #20 from gacela-project/allow-headers-in-response
Browse files Browse the repository at this point in the history
Allow headers in Response
  • Loading branch information
Chemaclass authored Apr 22, 2023
2 parents 2aad05c + 0a24735 commit 56c0cb7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
12 changes: 12 additions & 0 deletions example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_once \dirname(__DIR__) . '/vendor/autoload.php';

use Gacela\Router\Entities\Request;
use Gacela\Router\Entities\Response;
use Gacela\Router\Router;
use Gacela\Router\Routes;

Expand Down Expand Up @@ -34,6 +35,14 @@ public function customAction(int $number = 0): string
{
return "customAction(number: {$number})";
}

public function customHeaders(): Response
{
return new Response('{"custom": "headers"}', [
'Access-Control-Allow-Origin: *',
'Content-Type: application/json',
]);
}
}

Router::configure(static function (Routes $routes): void {
Expand All @@ -48,4 +57,7 @@ public function customAction(int $number = 0): string

# Try it out: http://localhost:8081/custom
$routes->any('custom', Controller::class);

# Try it out: http://localhost:8081/headers
$routes->any('headers', Controller::class, 'customHeaders');
});
19 changes: 10 additions & 9 deletions src/Router/Entities/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

namespace Gacela\Router\Entities;

use function in_array;

final class JsonResponse extends Response
{
public function __construct(array $json)
/**
* @param list<string> $headers
*/
public function __construct(array $json, array $headers = [])
{
parent::__construct(json_encode($json, JSON_THROW_ON_ERROR));
}

public function __toString(): string
{
header('Content-Type: application/json');

return parent::__toString();
if (!in_array('Content-Type: application/json', $headers, true)) {
$headers[] = 'Content-Type: application/json';
}
parent::__construct(json_encode($json, JSON_THROW_ON_ERROR), $headers);
}
}
12 changes: 10 additions & 2 deletions src/Router/Entities/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@

class Response
{
/**
* @param list<string> $headers
*/
public function __construct(
private string $body,
private string $content,
private array $headers = [],
) {
}

public function __toString(): string
{
return $this->body;
foreach ($this->headers as $header) {
header($header);
}

return $this->content;
}
}
56 changes: 56 additions & 0 deletions tests/Feature/Router/RouterResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,60 @@ public function test_json_response(): void
],
], $this->headers());
}

public function test_response_headers(): void
{
$_SERVER['REQUEST_URI'] = 'https://example.org/uri';
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;

Router::configure(static function (Routes $routes): void {
$routes->get('uri', static fn () => new Response('{"key":"value"}', [
'Access-Control-Allow-Origin: *',
'Content-Type: application/json',
]));
});

$this->expectOutputString('{"key":"value"}');

self::assertSame([
[
'header' => 'Access-Control-Allow-Origin: *',
'replace' => true,
'response_code' => 0,
],
[
'header' => 'Content-Type: application/json',
'replace' => true,
'response_code' => 0,
],
], $this->headers());
}

public function test_json_response_headers(): void
{
$_SERVER['REQUEST_URI'] = 'https://example.org/uri';
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;

Router::configure(static function (Routes $routes): void {
$routes->get('uri', static fn () => new JsonResponse(['key' => 'value'], [
'Access-Control-Allow-Origin: *',
'Content-Type: application/json',
]));
});

$this->expectOutputString('{"key":"value"}');

self::assertSame([
[
'header' => 'Access-Control-Allow-Origin: *',
'replace' => true,
'response_code' => 0,
],
[
'header' => 'Content-Type: application/json',
'replace' => true,
'response_code' => 0,
],
], $this->headers());
}
}

0 comments on commit 56c0cb7

Please sign in to comment.