Added output postprocessors.
This commit is contained in:
parent
ee48bb5bd6
commit
c2a79a1076
5 changed files with 130 additions and 7 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
|||
0.2503.150208
|
||||
0.2503.150240
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
// ValueMultipartFormData.php
|
||||
// Created: 2025-03-12
|
||||
// Updated: 2025-03-12
|
||||
// Updated: 2025-03-15
|
||||
|
||||
namespace Index\Http\Content\Multipart;
|
||||
|
||||
|
@ -29,9 +29,7 @@ class ValueMultipartFormData implements MultipartFormData {
|
|||
* Retrieves the value of this part.
|
||||
*/
|
||||
public string $value {
|
||||
get {
|
||||
return (string)$this->stream;
|
||||
}
|
||||
get => (string)$this->stream;
|
||||
}
|
||||
|
||||
public function __toString(): string {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
// HttpHeaders.php
|
||||
// Created: 2025-03-12
|
||||
// Updated: 2025-03-12
|
||||
// Created: 2022-02-08
|
||||
// Updated: 2025-03-15
|
||||
|
||||
namespace Index\Http;
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ namespace Index\Http\Routing;
|
|||
|
||||
use RuntimeException;
|
||||
use Index\MediaType;
|
||||
use Index\Bencode\Bencode;
|
||||
use Index\Http\Content\{MultipartFormContent,UrlEncodedFormContent};
|
||||
use Index\Http\Routing\Processors\{Postprocessor,Preprocessor};
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
|
@ -65,4 +66,46 @@ class RouterProcessors implements RouteHandler {
|
|||
$context->halt();
|
||||
}
|
||||
}
|
||||
|
||||
#[Postprocessor('output:stream')]
|
||||
public function postOutputStream(HandlerContext $context): void {
|
||||
$context->response->setTypeStream();
|
||||
}
|
||||
|
||||
#[Postprocessor('output:plain')]
|
||||
public function postOutputPlain(HandlerContext $context, string $charset = ''): void {
|
||||
$context->response->setTypePlain($charset);
|
||||
}
|
||||
|
||||
#[Postprocessor('output:html')]
|
||||
public function postOutputHtml(HandlerContext $context, string $charset = ''): void {
|
||||
$context->response->setTypeHtml($charset);
|
||||
}
|
||||
|
||||
#[Postprocessor('output:xml')]
|
||||
public function postOutputXml(HandlerContext $context, string $charset = ''): void {
|
||||
$context->response->setTypeXml($charset);
|
||||
}
|
||||
|
||||
#[Postprocessor('output:css')]
|
||||
public function postOutputCss(HandlerContext $context, string $charset = ''): void {
|
||||
$context->response->setTypeCss($charset);
|
||||
}
|
||||
|
||||
#[Postprocessor('output:js')]
|
||||
public function postOutputJs(HandlerContext $context, string $charset = ''): void {
|
||||
$context->response->setTypeJs($charset);
|
||||
}
|
||||
|
||||
#[Postprocessor('output:json')]
|
||||
public function postOutputJson(HandlerContext $context, string $charset = '', int $flags = JSON_UNESCAPED_SLASHES): void {
|
||||
$context->response->setTypeJson($charset);
|
||||
$context->result = json_encode($context->result, JSON_THROW_ON_ERROR | $flags);
|
||||
}
|
||||
|
||||
#[Postprocessor('output:bencode')]
|
||||
public function postOutputBencode(HandlerContext $context): void {
|
||||
$context->response->setTypeBencode();
|
||||
$context->result = Bencode::encode($context->result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -201,8 +201,90 @@ final class RouterTest extends TestCase {
|
|||
public function urlencodedRequired(UrlEncodedFormContent $content): string {
|
||||
return (string)$content->getParam('test');
|
||||
}
|
||||
|
||||
#[After('output:stream')]
|
||||
#[ExactRoute('GET', '/output/stream')]
|
||||
public function testOutputStream(): string {
|
||||
return '<!doctype html><h1>ensuring autodetect gets skipped</h1>';
|
||||
}
|
||||
|
||||
#[After('output:plain', charset: 'utf-8')]
|
||||
#[ExactRoute('GET', '/output/plain')]
|
||||
public function testOutputPlain(): string {
|
||||
return '<!doctype html><h1>ensuring autodetect gets skipped</h1>';
|
||||
}
|
||||
|
||||
#[After('output:html', charset: 'us-ascii')]
|
||||
#[ExactRoute('GET', '/output/html')]
|
||||
public function testOutputHtml(): string {
|
||||
return '<?xml idk how xml opens the prefix is enough><beans></beans>';
|
||||
}
|
||||
|
||||
#[After('output:xml')]
|
||||
#[ExactRoute('GET', '/output/xml')]
|
||||
public function testOutputXml(): string {
|
||||
return 'soup';
|
||||
}
|
||||
|
||||
#[After('output:css')]
|
||||
#[ExactRoute('GET', '/output/css')]
|
||||
public function testOutputCss(): string {
|
||||
return '<!doctype html><h1>ensuring autodetect gets skipped</h1>';
|
||||
}
|
||||
|
||||
#[After('output:js')]
|
||||
#[ExactRoute('GET', '/output/js')]
|
||||
public function testOutputJs(): string {
|
||||
return '<!doctype html><h1>ensuring autodetect gets skipped</h1>';
|
||||
}
|
||||
|
||||
#[After('output:json', flags: 0)]
|
||||
#[ExactRoute('GET', '/output/json')]
|
||||
/** @return array{wow: string} */
|
||||
public function testOutputJson(): array {
|
||||
return ['wow' => 'objects?? / epic!!'];
|
||||
}
|
||||
|
||||
#[After('output:bencode')]
|
||||
#[ExactRoute('GET', '/output/bencode')]
|
||||
/** @return array{benben: int} */
|
||||
public function testOutputBencode(): array {
|
||||
return ['benben' => 12345];
|
||||
}
|
||||
});
|
||||
|
||||
$response = $router->handle(HttpRequest::createRequestWithoutBody('GET', '/output/stream'));
|
||||
$this->assertEquals('application/octet-stream', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertEquals('<!doctype html><h1>ensuring autodetect gets skipped</h1>', (string)$response->getBody());
|
||||
|
||||
$response = $router->handle(HttpRequest::createRequestWithoutBody('GET', '/output/plain'));
|
||||
$this->assertEquals('text/plain;charset=utf-8', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertEquals('<!doctype html><h1>ensuring autodetect gets skipped</h1>', (string)$response->getBody());
|
||||
|
||||
$response = $router->handle(HttpRequest::createRequestWithoutBody('GET', '/output/html'));
|
||||
$this->assertEquals('text/html;charset=us-ascii', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertEquals('<?xml idk how xml opens the prefix is enough><beans></beans>', (string)$response->getBody());
|
||||
|
||||
$response = $router->handle(HttpRequest::createRequestWithoutBody('GET', '/output/xml'));
|
||||
$this->assertEquals('application/xml', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertEquals('soup', (string)$response->getBody());
|
||||
|
||||
$response = $router->handle(HttpRequest::createRequestWithoutBody('GET', '/output/css'));
|
||||
$this->assertEquals('text/css', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertEquals('<!doctype html><h1>ensuring autodetect gets skipped</h1>', (string)$response->getBody());
|
||||
|
||||
$response = $router->handle(HttpRequest::createRequestWithoutBody('GET', '/output/js'));
|
||||
$this->assertEquals('application/javascript', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertEquals('<!doctype html><h1>ensuring autodetect gets skipped</h1>', (string)$response->getBody());
|
||||
|
||||
$response = $router->handle(HttpRequest::createRequestWithoutBody('GET', '/output/json'));
|
||||
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertEquals('{"wow":"objects?? \/ epic!!"}', (string)$response->getBody());
|
||||
|
||||
$response = $router->handle(HttpRequest::createRequestWithoutBody('GET', '/output/bencode'));
|
||||
$this->assertEquals('application/x-bittorrent', $response->getHeaderLine('Content-Type'));
|
||||
$this->assertEquals('d6:benbeni12345ee', (string)$response->getBody());
|
||||
|
||||
$response = $router->handle(HttpRequest::createRequestWithoutBody('POST', '/optional-form'));
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('none', (string)$response->getBody());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue