Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixes HTTP headers. #25

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/HttpHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ public static function build(): HttpHeaders
return new HttpHeaders();
}

public function addFrom(string $key, mixed $value): HttpHeaders
{
$this->values[$key][] = $value;

return $this;
}

public function addFromCode(HttpCode $code): HttpHeaders
{
$template = 'HTTP/1.1 %s';
$this->values['Status'][] = sprintf($template, $code->message());
$this->values['Status'][] = $code->message();

return $this;
}
Expand All @@ -33,6 +39,13 @@ public function addFromContentType(Header $header): HttpHeaders
return $this;
}

public function removeFrom(string $key): HttpHeaders
{
unset($this->values[$key]);

return $this;
}

public function getHeader(string $key): array
{
return $this->values[$key] ?? [];
Expand Down
8 changes: 6 additions & 2 deletions src/Internal/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterf

public function withHeader(string $name, mixed $value): MessageInterface
{
throw new BadMethodCall(method: __METHOD__);
$this->headers->addFrom(key: $name, value: $value);

return $this;
}

public function withoutHeader(string $name): MessageInterface
{
throw new BadMethodCall(method: __METHOD__);
$this->headers->removeFrom(key: $name);

return $this;
}

public function withAddedHeader(string $name, mixed $value): MessageInterface
Expand Down
2 changes: 1 addition & 1 deletion tests/HttpResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function providerData(): array
private function defaultHeaderFrom(HttpCode $code): array
{
return [
'Status' => [sprintf('HTTP/1.1 %s', $code->message())],
'Status' => [$code->message()],
'Content-Type' => [HttpContentType::APPLICATION_JSON->value]
];
}
Expand Down
33 changes: 17 additions & 16 deletions tests/Internal/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testDefaultHeaders(): void
{
$response = Response::from(code: HttpCode::OK, data: [], headers: null);
$expected = [
'Status' => [sprintf('HTTP/1.1 %s', HttpCode::OK->message())],
'Status' => [HttpCode::OK->message()],
'Content-Type' => [HttpContentType::APPLICATION_JSON->value]
];

Expand Down Expand Up @@ -57,44 +57,45 @@ public function testGetHeaderLine(): void
self::assertEquals(HttpContentType::APPLICATION_JSON->value, $response->getHeaderLine(name: 'Content-Type'));
}

public function testExceptionWhenBadMethodCallOnWithBody(): void
public function testWithHeader(): void
{
$value = '2850bf62-8383-4e9f-b237-d41247a1df3b';
$response = Response::from(code: HttpCode::OK, data: [], headers: null);
$response->withHeader(name: 'Token', value: $value);

self::expectException(BadMethodCall::class);
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withBody> cannot be used.');
$expected = [$value];

$response->withBody(body: StreamFactory::from(data: []));
self::assertEquals($expected, $response->getHeader(name: 'Token'));
}

public function testExceptionWhenBadMethodCallOnWithStatus(): void
public function testWithoutHeader(): void
{
$response = Response::from(code: HttpCode::OK, data: [], headers: null);
$response->withoutHeader('Status');
$expected = [HttpContentType::APPLICATION_JSON->value];

self::expectException(BadMethodCall::class);
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withStatus> cannot be used.');

$response->withStatus(code: HttpCode::OK->value);
self::assertEmpty($response->getHeader(name: 'Status'));
self::assertEquals($expected, $response->getHeader(name: 'Content-Type'));
}

public function testExceptionWhenBadMethodCallOnWithHeader(): void
public function testExceptionWhenBadMethodCallOnWithBody(): void
{
$response = Response::from(code: HttpCode::OK, data: [], headers: null);

self::expectException(BadMethodCall::class);
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withHeader> cannot be used.');
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withBody> cannot be used.');

$response->withHeader(name: '', value: '');
$response->withBody(body: StreamFactory::from(data: []));
}

public function testExceptionWhenBadMethodCallOnWithoutHeader(): void
public function testExceptionWhenBadMethodCallOnWithStatus(): void
{
$response = Response::from(code: HttpCode::OK, data: [], headers: null);

self::expectException(BadMethodCall::class);
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withoutHeader> cannot be used.');
self::expectExceptionMessage('Method <TinyBlocks\Http\Internal\Response::withStatus> cannot be used.');

$response->withoutHeader(name: '');
$response->withStatus(code: HttpCode::OK->value);
}

public function testExceptionWhenBadMethodCallOnWithAddedHeader(): void
Expand Down
Loading