Skip to content

Commit

Permalink
feat: response with http protocol version
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Oct 19, 2023
1 parent 2f7d2db commit 7565479
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/System/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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');
}

/**
Expand All @@ -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)
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use PHPUnit\Framework\TestCase;
use System\Http\Request;
use System\Http\Response;
use System\Text\Str;

class ResponseTest extends TestCase
{
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 7565479

Please sign in to comment.