2024-07-27 11:23:18 +00:00
|
|
|
<?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()
|
|
|
|
{
|
2024-07-27 14:21:24 +00:00
|
|
|
$middleware = new Http2Middleware();
|
|
|
|
|
2024-07-27 11:23:18 +00:00
|
|
|
$request = new ServerRequest("GET", "/", [
|
|
|
|
"Upgrade" => "h2",
|
|
|
|
"x-forwarded-proto" => "http"
|
|
|
|
]);
|
|
|
|
|
2024-07-27 14:21:24 +00:00
|
|
|
/** @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",
|
|
|
|
]);
|
2024-07-27 11:23:18 +00:00
|
|
|
|
|
|
|
/** @var ResponseInterface $response */
|
|
|
|
$response = $middleware($request);
|
|
|
|
|
|
|
|
$this->assertEquals(Response::STATUS_BAD_REQUEST, $response->getStatusCode());
|
2024-07-27 14:21:24 +00:00
|
|
|
$this->assertEquals("Unsupported protocol", $response->getBody());
|
|
|
|
|
2024-07-27 11:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|