react-http2/tests/Http2MiddlewareTest.php

64 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace NoccyLabs\React\Http2;
use NoccyLabs\React\Http2\Frame\SettingsFrame;
use PHPUnit\Framework\Attributes\CoversClass;
use Psr\Http\Message\ResponseInterface;
use React\Http\Message\Response;
use React\Http\Message\ServerRequest;
#[CoversClass(Http2Middleware::class)]
class Http2MiddlewareTest extends \PHPUnit\Framework\TestCase
{
public function testInvalidUpgradeRequests()
{
$middleware = new Http2Middleware();
$request = new ServerRequest("GET", "/", [
"Upgrade" => "h2",
"x-forwarded-proto" => "http"
]);
/** @var ResponseInterface $response */
$response = $middleware($request);
$this->assertEquals(Response::STATUS_BAD_REQUEST, $response->getStatusCode());
$this->assertEquals("Unsupported protocol", $response->getBody());
$request = new ServerRequest("GET", "/", [
"Upgrade" => "h2c",
"x-forwarded-proto" => "http",
]);
/** @var ResponseInterface $response */
$response = $middleware($request);
$this->assertEquals(Response::STATUS_BAD_REQUEST, $response->getStatusCode());
$this->assertEquals("Unsupported protocol", $response->getBody());
}
public function testHandlingUpgradeRequest()
{
$http2settings = new SettingsFrame();
$http2settings->set(SettingsFrame::SETTINGS_MAX_CONCURRENT_STREAMS, 64);
$request = new ServerRequest("GET", "/", [
"Upgrade" => "h2c",
"Connection" => "upgrade",
"x-forwarded-proto" => "http",
"HTTP2-Settings" => $http2settings->toBinary(),
]);
$middleware = new Http2Middleware();
/** @var ResponseInterface $response */
$response = $middleware($request);
$this->assertEquals(Response::STATUS_SWITCHING_PROTOCOLS, $response->getStatusCode());
}
}