react-http2/tests/Http2MiddlewareTest.php
Christopher Vagnetoft dc3225538f Added tests for Http2Middleware
* Added unit tests for middleware
* Message handling code
2024-07-27 13:23:18 +02:00

51 lines
1.4 KiB
PHP

<?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()
{
$request = new ServerRequest("GET", "/", [
"Upgrade" => "h2",
"x-forwarded-proto" => "http"
]);
$middleware = new Http2Middleware();
/** @var ResponseInterface $response */
$response = $middleware($request);
$this->assertEquals(Response::STATUS_BAD_REQUEST, $response->getStatusCode());
}
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());
}
}