Skip to content

Commit

Permalink
basic add headers to response (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattvb91 authored Sep 23, 2022
1 parent cc3603c commit 5fb4a46
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Config/Apps/Http/Server/Routes/Handle/Headers/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Response implements Arrayable
*/
private ?array $_delete;

private ?array $_add;

private bool $_deferred = false;

public function addDeleteHeader(string $header): static
Expand All @@ -24,6 +26,13 @@ public function addDeleteHeader(string $header): static
return $this;
}

public function addHeader(string $name, string $value): static
{
$this->_add[$name] = [$value];

return $this;
}

public function setDeferred(bool $deferred): static
{
$this->_deferred = $deferred;
Expand All @@ -39,6 +48,10 @@ public function toArray(): array
$array['delete'] = $this->_delete;
}

if (isset($this->_add)) {
$array['add'] = $this->_add;
}

$array['deferred'] = $this->_deferred;

return $array;
Expand Down
34 changes: 34 additions & 0 deletions tests/Integration/Apps/Http/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,38 @@ public function test_static_file_server_handler()
$request = $client->request('GET', 'caddy/static.txt');
$this->assertEquals(200, $request->getStatusCode());
}

/**
* @coversNothing
*/
public function test_header_response()
{
$caddy = new Caddy();
$caddy->addApp((new Http())
->addServer('staticFileServer', (new Http\Server())
->addRoute((new Http\Server\Route())
->addHandle((new Http\Server\Routes\Handle\Headers())
->setResponse((new Http\Server\Routes\Handle\Headers\Response())
->addHeader('Cache-Status', 'public, max-age=3600')
))->addHandle((new Http\Server\Routes\Handle\FileServer())
->setRoot('/var/files')
)
)
)
);

$this->assertCaddyConfigLoaded($caddy);

$client = new Client([
'base_uri' => 'caddy',
'http_errors' => false,
'headers' => [
'Host' => 'localhost',
],
]);

$request = $client->request('GET', 'caddy/static.txt');
$this->assertEquals(200, $request->getStatusCode());
$this->assertEquals('public, max-age=3600', $request->getHeader('Cache-Status')[0]);
}
}

0 comments on commit 5fb4a46

Please sign in to comment.