Commit inicial con archivos existentes

This commit is contained in:
2026-01-17 16:14:00 -06:00
parent 48671dc88e
commit 4c48c279de
2539 changed files with 2412708 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?php
namespace Ratchet\RFC6455\Test\Unit\Handshake;
use Ratchet\RFC6455\Handshake\PermessageDeflateOptions;
use PHPUnit\Framework\TestCase;
/**
* @covers Ratchet\RFC6455\Handshake\PermessageDeflateOptions
*/
class PermessageDeflateOptionsTest extends TestCase
{
public static function versionSupportProvider(): array {
return [
['7.0.17', false],
['7.0.18', true],
['7.0.200', true],
['5.6.0', false],
['7.1.3', false],
['7.1.4', true],
['7.1.200', true],
['10.0.0', true]
];
}
/**
* @requires function deflate_init
* @dataProvider versionSupportProvider
*/
public function testVersionSupport(string $version, bool $supported): void {
$this->assertEquals($supported, PermessageDeflateOptions::permessageDeflateSupported($version));
}
}

View File

@@ -0,0 +1,180 @@
<?php
namespace Ratchet\RFC6455\Test\Unit\Handshake;
use Ratchet\RFC6455\Handshake\RequestVerifier;
use PHPUnit\Framework\TestCase;
/**
* @covers Ratchet\RFC6455\Handshake\RequestVerifier
*/
class RequestVerifierTest extends TestCase {
/**
* @var RequestVerifier
*/
protected $_v;
protected function setUp(): void {
$this->_v = new RequestVerifier();
}
public static function methodProvider(): array {
return array(
array(true, 'GET'),
array(true, 'get'),
array(true, 'Get'),
array(false, 'POST'),
array(false, 'DELETE'),
array(false, 'PUT'),
array(false, 'PATCH')
);
}
/**
* @dataProvider methodProvider
*/
public function testMethodMustBeGet(bool $result, string $in): void {
$this->assertEquals($result, $this->_v->verifyMethod($in));
}
public static function httpVersionProvider(): array {
return array(
array(true, 1.1),
array(true, '1.1'),
array(true, 1.2),
array(true, '1.2'),
array(true, 2),
array(true, '2'),
array(true, '2.0'),
array(false, '1.0'),
array(false, 1),
array(false, '0.9'),
array(false, ''),
array(false, 'hello')
);
}
/**
* @dataProvider httpVersionProvider
*/
public function testHttpVersionIsAtLeast1Point1(bool $expected, $in): void {
$this->assertEquals($expected, $this->_v->verifyHTTPVersion($in));
}
public static function uRIProvider(): array {
return array(
array(true, '/chat'),
array(true, '/hello/world?key=val'),
array(false, '/chat#bad'),
array(false, 'nope'),
array(false, '/ ಠ_ಠ '),
array(false, '/✖')
);
}
/**
* @dataProvider URIProvider
*/
public function testRequestUri(bool $expected, string $in): void {
$this->assertEquals($expected, $this->_v->verifyRequestURI($in));
}
public static function hostProvider(): array {
return array(
array(true, ['server.example.com']),
array(false, [])
);
}
/**
* @dataProvider HostProvider
*/
public function testVerifyHostIsSet(bool $expected, array $in): void {
$this->assertEquals($expected, $this->_v->verifyHost($in));
}
public static function upgradeProvider(): array {
return array(
array(true, ['websocket']),
array(true, ['Websocket']),
array(true, ['webSocket']),
array(false, []),
array(false, [''])
);
}
/**
* @dataProvider upgradeProvider
*/
public function testVerifyUpgradeIsWebSocket(bool $expected, array $val): void {
$this->assertEquals($expected, $this->_v->verifyUpgradeRequest($val));
}
public static function connectionProvider(): array {
return array(
array(true, ['Upgrade']),
array(true, ['upgrade']),
array(true, ['keep-alive', 'Upgrade']),
array(true, ['Upgrade', 'keep-alive']),
array(true, ['keep-alive', 'Upgrade', 'something']),
// as seen in Firefox 47.0.1 - see https://github.com/ratchetphp/RFC6455/issues/14
array(true, ['keep-alive, Upgrade']),
array(true, ['Upgrade, keep-alive']),
array(true, ['keep-alive, Upgrade, something']),
array(true, ['keep-alive, Upgrade', 'something']),
array(false, ['']),
array(false, [])
);
}
/**
* @dataProvider connectionProvider
*/
public function testConnectionHeaderVerification(bool $expected, array $val): void {
$this->assertEquals($expected, $this->_v->verifyConnection($val));
}
public static function keyProvider(): array {
return array(
array(true, ['hkfa1L7uwN6DCo4IS3iWAw==']),
array(true, ['765vVoQpKSGJwPzJIMM2GA==']),
array(true, ['AQIDBAUGBwgJCgsMDQ4PEC==']),
array(true, ['axa2B/Yz2CdpfQAY2Q5P7w==']),
array(false, [0]),
array(false, ['Hello World']),
array(false, ['1234567890123456']),
array(false, ['123456789012345678901234']),
array(true, [base64_encode('UTF8allthngs+✓')]),
array(true, ['dGhlIHNhbXBsZSBub25jZQ==']),
array(false, []),
array(false, ['dGhlIHNhbXBsZSBub25jZQ==', 'Some other value']),
array(false, ['Some other value', 'dGhlIHNhbXBsZSBub25jZQ=='])
);
}
/**
* @dataProvider keyProvider
*/
public function testKeyIsBase64Encoded16BitNonce(bool $expected, array $val): void {
$this->assertEquals($expected, $this->_v->verifyKey($val));
}
public static function versionProvider(): array {
return array(
array(true, [13]),
array(true, ['13']),
array(false, [12]),
array(false, [14]),
array(false, ['14']),
array(false, ['hi']),
array(false, ['']),
array(false, [])
);
}
/**
* @dataProvider versionProvider
*/
public function testVersionEquals13(bool $expected, array $in): void {
$this->assertEquals($expected, $this->_v->verifyVersion($in));
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Ratchet\RFC6455\Test\Unit\Handshake;
use Ratchet\RFC6455\Handshake\ResponseVerifier;
use PHPUnit\Framework\TestCase;
/**
* @covers Ratchet\RFC6455\Handshake\ResponseVerifier
*/
class ResponseVerifierTest extends TestCase {
/**
* @var ResponseVerifier
*/
protected $_v;
protected function setUp(): void {
$this->_v = new ResponseVerifier;
}
public static function subProtocolsProvider(): array {
return [
[true, ['a'], ['a']]
, [true, ['c', 'd', 'a'], ['a']]
, [true, ['c, a', 'd'], ['a']]
, [true, [], []]
, [true, ['a', 'b'], []]
, [false, ['c', 'd', 'a'], ['b', 'a']]
, [false, ['a', 'b', 'c'], ['d']]
];
}
/**
* @dataProvider subProtocolsProvider
*/
public function testVerifySubProtocol(bool $expected, array $request, array $response): void {
$this->assertEquals($expected, $this->_v->verifySubProtocol($request, $response));
}
}

View File

@@ -0,0 +1,223 @@
<?php
namespace Ratchet\RFC6455\Test\Unit\Handshake;
use GuzzleHttp\Psr7\Message;
use GuzzleHttp\Psr7\HttpFactory;
use Ratchet\RFC6455\Handshake\RequestVerifier;
use Ratchet\RFC6455\Handshake\ServerNegotiator;
use PHPUnit\Framework\TestCase;
/**
* @covers Ratchet\RFC6455\Handshake\ServerNegotiator
*/
class ServerNegotiatorTest extends TestCase
{
public function testNoUpgradeRequested(): void {
$negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$requestText = 'GET / HTTP/1.1
Host: 127.0.0.1:6789
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
';
$request = Message::parseRequest($requestText);
$response = $negotiator->handshake($request);
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(426, $response->getStatusCode());
$this->assertEquals('Upgrade header MUST be provided', $response->getReasonPhrase());
$this->assertEquals('Upgrade', $response->getHeaderLine('Connection'));
$this->assertEquals('websocket', $response->getHeaderLine('Upgrade'));
$this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version'));
}
public function testNoConnectionUpgradeRequested(): void {
$negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$requestText = 'GET / HTTP/1.1
Host: 127.0.0.1:6789
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
';
$request = Message::parseRequest($requestText);
$response = $negotiator->handshake($request);
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(400, $response->getStatusCode());
$this->assertEquals('Connection Upgrade MUST be requested', $response->getReasonPhrase());
}
public function testInvalidSecWebsocketKey(): void {
$negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$requestText = 'GET / HTTP/1.1
Host: 127.0.0.1:6789
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Sec-WebSocket-Key: 12345
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
';
$request = Message::parseRequest($requestText);
$response = $negotiator->handshake($request);
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(400, $response->getStatusCode());
$this->assertEquals('Invalid Sec-WebSocket-Key', $response->getReasonPhrase());
}
public function testInvalidSecWebsocketVersion(): void {
$negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$requestText = 'GET / HTTP/1.1
Host: 127.0.0.1:6789
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
';
$request = Message::parseRequest($requestText);
$response = $negotiator->handshake($request);
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(426, $response->getStatusCode());
$this->assertEquals('Upgrade Required', $response->getReasonPhrase());
$this->assertEquals('Upgrade', $response->getHeaderLine('Connection'));
$this->assertEquals('websocket', $response->getHeaderLine('Upgrade'));
$this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version'));
}
public function testBadSubprotocolResponse(): void {
$negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$negotiator->setStrictSubProtocolCheck(true);
$negotiator->setSupportedSubProtocols([]);
$requestText = 'GET / HTTP/1.1
Host: 127.0.0.1:6789
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Sec-WebSocket-Protocol: someprotocol
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
';
$request = Message::parseRequest($requestText);
$response = $negotiator->handshake($request);
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(426, $response->getStatusCode());
$this->assertEquals('No Sec-WebSocket-Protocols requested supported', $response->getReasonPhrase());
$this->assertEquals('Upgrade', $response->getHeaderLine('Connection'));
$this->assertEquals('websocket', $response->getHeaderLine('Upgrade'));
$this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version'));
}
public function testNonStrictSubprotocolDoesNotIncludeHeaderWhenNoneAgreedOn(): void {
$negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$negotiator->setStrictSubProtocolCheck(false);
$negotiator->setSupportedSubProtocols(['someproto']);
$requestText = 'GET / HTTP/1.1
Host: 127.0.0.1:6789
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Sec-WebSocket-Protocol: someotherproto
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
';
$request = Message::parseRequest($requestText);
$response = $negotiator->handshake($request);
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(101, $response->getStatusCode());
$this->assertEquals('Upgrade', $response->getHeaderLine('Connection'));
$this->assertEquals('websocket', $response->getHeaderLine('Upgrade'));
$this->assertFalse($response->hasHeader('Sec-WebSocket-Protocol'));
}
public function testSuggestsAppropriateSubprotocol(): void
{
$negotiator = new ServerNegotiator(new RequestVerifier(), new HttpFactory());
$negotiator->setStrictSubProtocolCheck(true);
$negotiator->setSupportedSubProtocols(['someproto']);
$requestText = 'GET / HTTP/1.1
Host: localhost:8080
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Sec-WebSocket-Key: HGt8eQax7nAOlXUw0/asPQ==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
';
$request = Message::parseRequest($requestText);
$response = $negotiator->handshake($request);
$this->assertEquals('1.1', $response->getProtocolVersion());
$this->assertEquals(426, $response->getStatusCode());
$this->assertEquals('Upgrade', $response->getHeaderLine('Connection'));
$this->assertEquals('websocket', $response->getHeaderLine('Upgrade'));
$this->assertEquals('someproto', $response->getHeaderLine('Sec-WebSocket-Protocol'));
}
}