From 7565479839259a6bc9bb24eef648ff3343d30083 Mon Sep 17 00:00:00 2001 From: SonyPradana Date: Thu, 19 Oct 2023 20:46:45 +0700 Subject: [PATCH] feat: response with http protocol version --- src/System/Http/Response.php | 26 +++++++++++++++++++++++++- tests/Http/ResponseTest.php | 10 ++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/System/Http/Response.php b/src/System/Http/Response.php index 7ee36ccc..e4681ffb 100644 --- a/src/System/Http/Response.php +++ b/src/System/Http/Response.php @@ -73,6 +73,11 @@ class Response * */ private $content_type = 'text/html'; + /** + * Http Protocol version (1.0 or 1.1). + */ + private string $protocol_version; + /** * Create rosone http base on conten and header. * @@ -85,6 +90,7 @@ public function __construct($content = '', int $respone_code = Response::HTTP_OK $this->setContent($content); $this->setResponeCode($respone_code); $this->headers = new HeaderCollection($headers); + $this->setProtocolVersion('1.1'); } /** @@ -96,7 +102,7 @@ public function __toString() { $respone_code = $this->respone_code; $respone_text = Response::$statusTexts[$respone_code] ?? 'ok'; - $respone_header = sprintf('HTTP/1.1 %s %s', $respone_code, $respone_text); + $respone_header = sprintf('HTTP/%s %s %s', $this->getProtocolVersion(), $respone_code, $respone_text); $header_lines = (string) $this->headers; $content = is_array($this->content) @@ -356,6 +362,16 @@ public function setHeaders($headers) return $this; } + /** + * Set http protocol version. + */ + public function setProtocolVersion(string $version): self + { + $this->protocol_version = $version; + + return $this; + } + /** * Remove header from origin header. * @@ -420,6 +436,14 @@ public function getContent() return $this->content; } + /** + * Get http protocole version. + */ + public function getProtocolVersion(): string + { + return $this->protocol_version; + } + /** * Prepare response to send header to client. * diff --git a/tests/Http/ResponseTest.php b/tests/Http/ResponseTest.php index 95e71e07..c29fe1bf 100644 --- a/tests/Http/ResponseTest.php +++ b/tests/Http/ResponseTest.php @@ -3,6 +3,7 @@ use PHPUnit\Framework\TestCase; use System\Http\Request; use System\Http\Response; +use System\Text\Str; class ResponseTest extends TestCase { @@ -167,4 +168,13 @@ public function itCanGetTypeOfResponseCode() $res = new Response('content', rand(500, 599)); $this->assertTrue($res->isServerError()); } + + /** @test */ + public function itCanChangeProtocolVersion() + { + $res = new Response('content'); + $res->setProtocolVersion('1.0'); + + $this->assertTrue(Str::contains((string) $res, '1.0'), 'Test protocol version'); + } }