From 4dec05b2117ed50b3775d25bbd06b48a45d6496c Mon Sep 17 00:00:00 2001 From: Gustavo Freze de Araujo Santos Date: Sun, 4 Feb 2024 17:39:45 -0300 Subject: [PATCH] fix: Fixes HTTP headers. --- src/HttpHeaders.php | 17 +++++++++++++++-- src/Internal/Response.php | 8 ++++++-- tests/HttpResponseTest.php | 2 +- tests/Internal/ResponseTest.php | 33 +++++++++++++++++---------------- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/HttpHeaders.php b/src/HttpHeaders.php index 6d2e40f..b6fe26d 100644 --- a/src/HttpHeaders.php +++ b/src/HttpHeaders.php @@ -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; } @@ -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] ?? []; diff --git a/src/Internal/Response.php b/src/Internal/Response.php index a43c3d0..e5679a5 100644 --- a/src/Internal/Response.php +++ b/src/Internal/Response.php @@ -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 diff --git a/tests/HttpResponseTest.php b/tests/HttpResponseTest.php index b458f13..f9e4f13 100644 --- a/tests/HttpResponseTest.php +++ b/tests/HttpResponseTest.php @@ -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] ]; } diff --git a/tests/Internal/ResponseTest.php b/tests/Internal/ResponseTest.php index 0b37aca..a7422b8 100644 --- a/tests/Internal/ResponseTest.php +++ b/tests/Internal/ResponseTest.php @@ -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] ]; @@ -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 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 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 cannot be used.'); + self::expectExceptionMessage('Method 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 cannot be used.'); + self::expectExceptionMessage('Method cannot be used.'); - $response->withoutHeader(name: ''); + $response->withStatus(code: HttpCode::OK->value); } public function testExceptionWhenBadMethodCallOnWithAddedHeader(): void