Skip to content

Commit

Permalink
- updates the header value, or removes the x-powered-by header
Browse files Browse the repository at this point in the history
  • Loading branch information
kodeart committed Feb 21, 2024
1 parent b63cc41 commit cc3bc01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
13 changes: 7 additions & 6 deletions src/Middleware/XPoweredByMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@

class XPoweredByMiddleware implements MiddlewareInterface
{
private readonly string|null $value;

public function __construct(string|null $value = null)
public function __construct(private string|null $value = null)
{
$this->value = empty($value) ? 'Koded v' . get_version() : $value;
@header_remove('x-powered-by');
}

public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler): ResponseInterface
{
return $handler->handle($request)
->withHeader('X-Powered-By', $this->value);
$response = $handler->handle($request);
if (null === $this->value) {
return $response;
}
return $response->withHeader('x-powered-by', $this->value);
}
}
31 changes: 19 additions & 12 deletions tests/Middleware/XPoweredByMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,33 @@ class XPoweredByMiddlewareTest extends TestCase
{
private App $app;

public function test_xpoweredby_headers()
public function test_xpoweredby_with_null_value()
{
$version = file_get_contents(__DIR__ . '/../../VERSION');
$this->app
->route('/', TestResource::class, [
XPoweredByMiddleware::class
], true);

[, $response] = call_user_func($this->app);

$this->assertStringContainsString(
$version,
$response->getheaderLine('X-Powered-By'),
'Should set the version in the header'
);
$this->assertArrayNotHasKey('x-powered-by', $response->getHeaders());
$this->assertSame('', $response->getheaderLine('X-Powered-By'));
$this->assertSame([], $response->getHeader('x-powered-by'));
}

public function test_xpoweredby_version()
public function test_xpoweredby_value()
{
define('VERSION', ['1.2.3', 'dev', '0']);
$this->app
->route('/', TestResource::class, [
new XPoweredByMiddleware('koded')
], true);

[, $response] = call_user_func($this->app);

$this->assertSame(
'Koded v1.2.3-dev',
$response->getHeaderLine('x-powered-by')
'koded',
$response->getheaderLine('X-Powered-By'),
'Should set the value in the header'
);
}

Expand All @@ -49,6 +56,6 @@ protected function setUp(): void

$this->app = (new App(
renderer: [$this, '_renderer']
))->route('/', TestResource::class, [XPoweredByMiddleware::class], true);
));
}
}

0 comments on commit cc3bc01

Please sign in to comment.