diff --git a/infection.json.dist b/infection.json.dist index 605d106..13361a2 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -17,7 +17,9 @@ "LogicalOr": false, "IfNegation": false, "InstanceOf_": false, + "UnwrapArrayMap": false, "ArrayItemRemoval": false, + "UnwrapArrayUnique": false, "MethodCallRemoval": false, "LogicalOrAllSubExprNegation": false, "LogicalOrSingleSubExprNegation": false diff --git a/src/HttpHeaders.php b/src/HttpHeaders.php index edf8b2f..4d19178 100644 --- a/src/HttpHeaders.php +++ b/src/HttpHeaders.php @@ -13,17 +13,25 @@ final class HttpHeaders { private array $values = []; + private function __construct() + { + } + + public static function build(): HttpHeaders + { + return new HttpHeaders(); + } + public function add(Header $header): HttpHeaders { - $key = $header->key(); - $this->values['header'][$key] = sprintf('%s: %s', $key, $header->value()); + $this->values[$header->key()][] = $header->value(); return $this; } - public function getHeader(): array + public function getHeader(string $key): array { - return $this->values['header'] ?? []; + return $this->values[$key] ?? []; } public function hasHeaders(): bool @@ -33,11 +41,14 @@ public function hasHeaders(): bool public function hasHeader(string $key): bool { - return !empty($this->getHeader()[$key]); + return !empty($this->getHeader(key: $key)); } public function toArray(): array { - return $this->values; + return array_map( + fn(array $values): array => [end($values)], + array_map(fn(array $values): array => array_unique($values), $this->values) + ); } } diff --git a/src/Internal/Response.php b/src/Internal/Response.php index 999a4e0..0154b35 100644 --- a/src/Internal/Response.php +++ b/src/Internal/Response.php @@ -23,7 +23,7 @@ private function __construct( public static function from(HttpCode $code, mixed $data, ?HttpHeaders $headers): ResponseInterface { if (is_null($headers) || !$headers->hasHeaders()) { - $headers = (new HttpHeaders())->add(header: HttpContentType::APPLICATION_JSON); + $headers = HttpHeaders::build()->add(header: HttpContentType::APPLICATION_JSON); } return new Response(code: $code, body: StreamFactory::from(data: $data), headers: $headers); @@ -76,12 +76,12 @@ public function hasHeader(string $name): bool public function getHeader(string $name): array { - return $this->headers->getHeader(); + return $this->headers->getHeader(key: $name); } public function getHeaderLine(string $name): string { - return implode(', ', $this->getHeader(name: $name)); + return implode(', ', $this->headers->getHeader(key: $name)); } public function getBody(): StreamInterface diff --git a/tests/HttpResponseTest.php b/tests/HttpResponseTest.php index 28c0399..10e23a2 100644 --- a/tests/HttpResponseTest.php +++ b/tests/HttpResponseTest.php @@ -8,7 +8,12 @@ class HttpResponseTest extends TestCase { - private array $defaultHeader = ['header' => ['Content-Type' => 'Content-Type: application/json']]; + private array $defaultHeader; + + protected function setUp(): void + { + $this->defaultHeader = ['Content-Type' => [HttpContentType::APPLICATION_JSON->value]]; + } /** * @dataProvider providerData diff --git a/tests/Internal/HeadersTest.php b/tests/Internal/HeadersTest.php index 8dcfb82..dc34fba 100644 --- a/tests/Internal/HeadersTest.php +++ b/tests/Internal/HeadersTest.php @@ -10,19 +10,18 @@ class HeadersTest extends TestCase { public function testAddAndGetValues(): void { - $headers = (new HttpHeaders())->add(header: HttpContentType::APPLICATION_JSON); - $expected = ['header' => ['Content-Type' => 'Content-Type: application/json']]; + $headers = HttpHeaders::build()->add(header: HttpContentType::APPLICATION_JSON); + $expected = ['Content-Type' => [HttpContentType::APPLICATION_JSON->value]]; self::assertEquals($expected, $headers->toArray()); } public function testAddAndGetUniqueValues(): void { - $headers = (new HttpHeaders()) + $headers = HttpHeaders::build() ->add(header: HttpContentType::TEXT_HTML) ->add(header: HttpContentType::APPLICATION_PDF); - - $expected = ['header' => ['Content-Type' => 'Content-Type: application/pdf']]; + $expected = ['Content-Type' => [HttpContentType::APPLICATION_PDF->value]]; self::assertEquals($expected, $headers->toArray()); } diff --git a/tests/Internal/ResponseTest.php b/tests/Internal/ResponseTest.php index 1ae43cc..25d7f59 100644 --- a/tests/Internal/ResponseTest.php +++ b/tests/Internal/ResponseTest.php @@ -11,13 +11,10 @@ class ResponseTest extends TestCase { - private const TEXT_PLAIN = 'Content-Type: text/plain'; - private const APPLICATION_JSON = 'Content-Type: application/json'; - public function testDefaultHeaders(): void { $response = Response::from(code: HttpCode::OK, data: [], headers: null); - $expected = ['header' => ['Content-Type' => self::APPLICATION_JSON]]; + $expected = ['Content-Type' => [HttpContentType::APPLICATION_JSON->value]]; self::assertEquals($expected, $response->getHeaders()); } @@ -31,9 +28,9 @@ public function testGetProtocolVersion(): void public function testGetHeaders(): void { - $headers = (new HttpHeaders())->add(header: HttpContentType::APPLICATION_JSON); + $headers = HttpHeaders::build()->add(header: HttpContentType::APPLICATION_JSON); $response = Response::from(code: HttpCode::OK, data: [], headers: $headers); - $expected = ['Content-Type' => self::APPLICATION_JSON]; + $expected = [HttpContentType::APPLICATION_JSON->value]; self::assertEquals($headers->toArray(), $response->getHeaders()); self::assertEquals($expected, $response->getHeader(name: 'Content-Type')); @@ -41,9 +38,9 @@ public function testGetHeaders(): void public function testHasHeader(): void { - $headers = (new HttpHeaders())->add(header: HttpContentType::TEXT_PLAIN); + $headers = HttpHeaders::build()->add(header: HttpContentType::TEXT_PLAIN); $response = Response::from(code: HttpCode::OK, data: [], headers: $headers); - $expected = ['Content-Type' => self::TEXT_PLAIN]; + $expected = [HttpContentType::TEXT_PLAIN->value]; self::assertTrue($response->hasHeader(name: 'Content-Type')); self::assertEquals($expected, $response->getHeader(name: 'Content-Type')); @@ -51,10 +48,10 @@ public function testHasHeader(): void public function testGetHeaderLine(): void { - $headers = (new HttpHeaders())->add(header: HttpContentType::APPLICATION_JSON); + $headers = HttpHeaders::build()->add(header: HttpContentType::APPLICATION_JSON); $response = Response::from(code: HttpCode::OK, data: [], headers: $headers); - self::assertEquals(self::APPLICATION_JSON, $response->getHeaderLine(name: 'Content-Type')); + self::assertEquals(HttpContentType::APPLICATION_JSON->value, $response->getHeaderLine(name: 'Content-Type')); } public function testExceptionWhenBadMethodCallOnWithBody(): void