Skip to content

Commit 439be0e

Browse files
committed
Add responses
1 parent 72c2597 commit 439be0e

File tree

4 files changed

+249
-0
lines changed

4 files changed

+249
-0
lines changed

src/HtmlResponse.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of php-fast-forward/http-message.
7+
*
8+
* This source file is subject to the license bundled
9+
* with this source code in the file LICENSE.
10+
*
11+
* @link https://github.com/php-fast-forward/http-message
12+
* @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
13+
* @license https://opensource.org/licenses/MIT MIT License
14+
*/
15+
16+
namespace FastForward\Http\Message;
17+
18+
use Nyholm\Psr7\Response;
19+
use Nyholm\Psr7\Stream;
20+
21+
/**
22+
* Class HtmlResponse.
23+
*
24+
* Represents an HTTP response containing HTML content.
25+
*
26+
* This class MUST be used to generate HTTP responses with a `text/html` content type.
27+
* It automatically sets the 'Content-Type' header, encodes the body using the specified charset,
28+
* and applies the HTTP 200 (OK) status code by default.
29+
*
30+
* @package FastForward\Http\Message
31+
*/
32+
final class HtmlResponse extends Response
33+
{
34+
/**
35+
* Constructs a new HtmlResponse instance.
36+
*
37+
* This constructor SHALL set the 'Content-Type' header to `text/html` with the specified charset
38+
* and initialize the response body with the provided HTML content. The response status code
39+
* will be set to 200 (OK) by default, with the corresponding reason phrase.
40+
*
41+
* @param string $html the HTML content to send in the response body
42+
* @param string $charset The character encoding to declare in the 'Content-Type' header. Defaults to 'utf-8'.
43+
* @param array<string, string|string[]> $headers optional additional headers to include in the response
44+
*/
45+
public function __construct(string $html, string $charset = 'utf-8', array $headers = [])
46+
{
47+
$headers['Content-Type'] = 'text/html; charset=' . $charset;
48+
49+
parent::__construct(
50+
status: StatusCode::Ok->value,
51+
headers: $headers,
52+
body: Stream::create($html),
53+
reason: StatusCode::Ok->getReasonPhrase(),
54+
);
55+
}
56+
}

src/TextResponse.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of php-fast-forward/http-message.
7+
*
8+
* This source file is subject to the license bundled
9+
* with this source code in the file LICENSE.
10+
*
11+
* @link https://github.com/php-fast-forward/http-message
12+
* @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
13+
* @license https://opensource.org/licenses/MIT MIT License
14+
*/
15+
16+
namespace FastForward\Http\Message;
17+
18+
use Nyholm\Psr7\Response;
19+
use Nyholm\Psr7\Stream;
20+
21+
/**
22+
* Class TextResponse.
23+
*
24+
* Represents an HTTP response containing plain text content.
25+
*
26+
* This class MUST be used to generate HTTP responses with a `text/plain` content type.
27+
* It automatically sets the 'Content-Type' header, encodes the body using the specified charset,
28+
* and applies the HTTP 200 (OK) status code by default.
29+
*
30+
* @package FastForward\Http\Message
31+
*/
32+
final class TextResponse extends Response
33+
{
34+
/**
35+
* Constructs a new TextResponse instance.
36+
*
37+
* This constructor SHALL set the 'Content-Type' header to `text/plain` with the specified charset
38+
* and initialize the response body with the provided plain text content. The response status code
39+
* will be set to 200 (OK) by default, with the corresponding reason phrase.
40+
*
41+
* @param string $text the plain text content to send in the response body
42+
* @param string $charset The character encoding to declare in the 'Content-Type' header. Defaults to 'utf-8'.
43+
* @param array<string, string|string[]> $headers optional additional headers to include in the response
44+
*/
45+
public function __construct(string $text, string $charset = 'utf-8', array $headers = [])
46+
{
47+
$headers['Content-Type'] = 'text/plain; charset=' . $charset;
48+
49+
parent::__construct(
50+
status: StatusCode::Ok->value,
51+
headers: $headers,
52+
body: Stream::create($text),
53+
reason: StatusCode::Ok->getReasonPhrase(),
54+
);
55+
}
56+
}

tests/HtmlResponseTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of php-fast-forward/http-message.
7+
*
8+
* This source file is subject to the license bundled
9+
* with this source code in the file LICENSE.
10+
*
11+
* @link https://github.com/php-fast-forward/http-message
12+
* @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
13+
* @license https://opensource.org/licenses/MIT MIT License
14+
*/
15+
16+
namespace FastForward\Http\Message\Tests;
17+
18+
use FastForward\Http\Message\HtmlResponse;
19+
use FastForward\Http\Message\StatusCode;
20+
use PHPUnit\Framework\Attributes\CoversClass;
21+
use PHPUnit\Framework\Attributes\UsesClass;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* @internal
26+
*/
27+
#[CoversClass(HtmlResponse::class)]
28+
#[UsesClass(StatusCode::class)]
29+
final class HtmlResponseTest extends TestCase
30+
{
31+
public function testConstructorWillSetHtmlBodyAndContentType(): void
32+
{
33+
$html = '<h1>Hello World</h1>';
34+
35+
$response = new HtmlResponse($html);
36+
37+
self::assertSame(StatusCode::Ok->value, $response->getStatusCode());
38+
self::assertSame(StatusCode::Ok->getReasonPhrase(), $response->getReasonPhrase());
39+
self::assertSame('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
40+
self::assertSame($html, (string) $response->getBody());
41+
}
42+
43+
public function testConstructorWillRespectCustomCharset(): void
44+
{
45+
$html = '<p>Charset Test</p>';
46+
47+
$response = new HtmlResponse($html, charset: 'iso-8859-1');
48+
49+
self::assertSame('text/html; charset=iso-8859-1', $response->getHeaderLine('Content-Type'));
50+
self::assertSame($html, (string) $response->getBody());
51+
}
52+
53+
public function testConstructorWillPreserveAdditionalHeaders(): void
54+
{
55+
$html = '<div>Content</div>';
56+
$headers = [
57+
'X-Test' => 'test-value',
58+
'X-Array' => ['one', 'two'],
59+
];
60+
61+
$response = new HtmlResponse($html, headers: $headers);
62+
63+
self::assertSame('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
64+
self::assertSame('test-value', $response->getHeaderLine('X-Test'));
65+
self::assertSame('one, two', $response->getHeaderLine('X-Array'));
66+
self::assertSame($html, (string) $response->getBody());
67+
}
68+
}

tests/TextResponseTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of php-fast-forward/http-message.
7+
*
8+
* This source file is subject to the license bundled
9+
* with this source code in the file LICENSE.
10+
*
11+
* @link https://github.com/php-fast-forward/http-message
12+
* @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
13+
* @license https://opensource.org/licenses/MIT MIT License
14+
*/
15+
16+
namespace FastForward\Http\Message\Tests;
17+
18+
use FastForward\Http\Message\StatusCode;
19+
use FastForward\Http\Message\TextResponse;
20+
use PHPUnit\Framework\Attributes\CoversClass;
21+
use PHPUnit\Framework\Attributes\UsesClass;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* @internal
26+
*/
27+
#[CoversClass(TextResponse::class)]
28+
#[UsesClass(StatusCode::class)]
29+
final class TextResponseTest extends TestCase
30+
{
31+
public function testConstructorWillSetTextBodyAndContentType(): void
32+
{
33+
$text = 'Plain text response';
34+
35+
$response = new TextResponse($text);
36+
37+
self::assertSame(StatusCode::Ok->value, $response->getStatusCode());
38+
self::assertSame(StatusCode::Ok->getReasonPhrase(), $response->getReasonPhrase());
39+
self::assertSame('text/plain; charset=utf-8', $response->getHeaderLine('Content-Type'));
40+
self::assertSame($text, (string) $response->getBody());
41+
}
42+
43+
public function testConstructorWillRespectCustomCharset(): void
44+
{
45+
$text = 'Texto com charset';
46+
$charset = 'iso-8859-1';
47+
48+
$response = new TextResponse($text, $charset);
49+
50+
self::assertSame('text/plain; charset=iso-8859-1', $response->getHeaderLine('Content-Type'));
51+
self::assertSame($text, (string) $response->getBody());
52+
}
53+
54+
public function testConstructorWillPreserveAdditionalHeaders(): void
55+
{
56+
$text = 'Texto com cabeçalhos';
57+
$headers = [
58+
'X-Test' => 'test-value',
59+
'X-Multiple' => ['um', 'dois'],
60+
];
61+
62+
$response = new TextResponse($text, headers: $headers);
63+
64+
self::assertSame('text/plain; charset=utf-8', $response->getHeaderLine('Content-Type'));
65+
self::assertSame('test-value', $response->getHeaderLine('X-Test'));
66+
self::assertSame('um, dois', $response->getHeaderLine('X-Multiple'));
67+
self::assertSame($text, (string) $response->getBody());
68+
}
69+
}

0 commit comments

Comments
 (0)