| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
namespace Ratchet\WebSocket\Version;
|
|
|
3 |
use Ratchet\WebSocket\Version\RFC6455;
|
|
|
4 |
use Ratchet\WebSocket\Version\RFC6455\Frame;
|
|
|
5 |
use Guzzle\Http\Message\RequestFactory;
|
|
|
6 |
use Guzzle\Http\Message\EntityEnclosingRequest;
|
|
|
7 |
|
|
|
8 |
/**
|
|
|
9 |
* @covers Ratchet\WebSocket\Version\RFC6455
|
|
|
10 |
*/
|
|
|
11 |
class RFC6455Test extends \PHPUnit_Framework_TestCase {
|
|
|
12 |
protected $version;
|
|
|
13 |
|
|
|
14 |
public function setUp() {
|
|
|
15 |
$this->version = new RFC6455;
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* @dataProvider handshakeProvider
|
|
|
20 |
*/
|
|
|
21 |
public function testKeySigningForHandshake($key, $accept) {
|
|
|
22 |
$this->assertEquals($accept, $this->version->sign($key));
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
public static function handshakeProvider() {
|
|
|
26 |
return array(
|
|
|
27 |
array('x3JJHMbDL1EzLkh9GBhXDw==', 'HSmrc0sMlYUkAGmm5OPpG2HaGWk=')
|
|
|
28 |
, array('dGhlIHNhbXBsZSBub25jZQ==', 's3pPLMBiTxaQ9kYGzzhZRbK+xOo=')
|
|
|
29 |
);
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* @dataProvider UnframeMessageProvider
|
|
|
34 |
*/
|
|
|
35 |
public function testUnframeMessage($message, $framed) {
|
|
|
36 |
$frame = new Frame;
|
|
|
37 |
$frame->addBuffer(base64_decode($framed));
|
|
|
38 |
|
|
|
39 |
$this->assertEquals($message, $frame->getPayload());
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
public static function UnframeMessageProvider() {
|
|
|
43 |
return array(
|
|
|
44 |
array('Hello World!', 'gYydAIfa1WXrtvIg0LXvbOP7')
|
|
|
45 |
, array('!@#$%^&*()-=_+[]{}\|/.,<>`~', 'gZv+h96r38f9j9vZ+IHWrvOWoayF9oX6gtfRqfKXwOeg')
|
|
|
46 |
, array('ಠ_ಠ', 'gYfnSpu5B/g75gf4Ow==')
|
|
|
47 |
, array("The quick brown fox jumps over the lazy dog. All work and no play makes Chris a dull boy. I'm trying to get past 128 characters for a unit test here...", 'gf4Amahb14P8M7Kj2S6+4MN7tfHHLLmjzjSvo8IuuvPbe7j1zSn398A+9+/JIa6jzDSwrYh7lu/Ee6Ds2jD34sY/9+3He6fvySL37skwsvCIGL/xwSj34og/ou/Ee7Xs0XX3o+F8uqPcKa7qxjz398d7sObce6fi2y/3sppj9+DAOqXiyy+y8dt7sezae7aj3TW+94gvsvDce7/m2j75rYY=')
|
|
|
48 |
);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function testUnframeMatchesPreFraming() {
|
|
|
52 |
$string = 'Hello World!';
|
|
|
53 |
$framed = $this->version->newFrame($string)->getContents();
|
|
|
54 |
|
|
|
55 |
$frame = new Frame;
|
|
|
56 |
$frame->addBuffer($framed);
|
|
|
57 |
|
|
|
58 |
$this->assertEquals($string, $frame->getPayload());
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public static $good_rest = 'GET /chat HTTP/1.1';
|
|
|
62 |
|
|
|
63 |
public static $good_header = array(
|
|
|
64 |
'Host' => 'server.example.com'
|
|
|
65 |
, 'Upgrade' => 'websocket'
|
|
|
66 |
, 'Connection' => 'Upgrade'
|
|
|
67 |
, 'Sec-WebSocket-Key' => 'dGhlIHNhbXBsZSBub25jZQ=='
|
|
|
68 |
, 'Origin' => 'http://example.com'
|
|
|
69 |
, 'Sec-WebSocket-Protocol' => 'chat, superchat'
|
|
|
70 |
, 'Sec-WebSocket-Version' => 13
|
|
|
71 |
);
|
|
|
72 |
|
|
|
73 |
public function caseVariantProvider() {
|
|
|
74 |
return array(
|
|
|
75 |
array('Sec-Websocket-Version')
|
|
|
76 |
, array('sec-websocket-version')
|
|
|
77 |
, array('SEC-WEBSOCKET-VERSION')
|
|
|
78 |
, array('sEC-wEBsOCKET-vERSION')
|
|
|
79 |
);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @dataProvider caseVariantProvider
|
|
|
84 |
*/
|
|
|
85 |
public function testIsProtocolWithCaseInsensitivity($headerName) {
|
|
|
86 |
$header = static::$good_header;
|
|
|
87 |
unset($header['Sec-WebSocket-Version']);
|
|
|
88 |
$header[$headerName] = 13;
|
|
|
89 |
|
|
|
90 |
$this->assertTrue($this->version->isProtocol(new EntityEnclosingRequest('get', '/', $header)));
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* A helper function to try and quickly put together a valid WebSocket HTTP handshake
|
|
|
95 |
* but optionally replace a piece to an invalid value for failure testing
|
|
|
96 |
*/
|
|
|
97 |
public static function getAndSpliceHeader($key = null, $val = null) {
|
|
|
98 |
$headers = static::$good_header;
|
|
|
99 |
|
|
|
100 |
if (null !== $key && null !== $val) {
|
|
|
101 |
$headers[$key] = $val;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
$header = '';
|
|
|
105 |
foreach ($headers as $key => $val) {
|
|
|
106 |
if (!empty($key)) {
|
|
|
107 |
$header .= "{$key}: ";
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
$header .= "{$val}\r\n";
|
|
|
111 |
}
|
|
|
112 |
$header .= "\r\n";
|
|
|
113 |
|
|
|
114 |
return $header;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public static function headerHandshakeProvider() {
|
|
|
118 |
return array(
|
|
|
119 |
array(false, "GET /test HTTP/1.0\r\n" . static::getAndSpliceHeader())
|
|
|
120 |
, array(true, static::$good_rest . "\r\n" . static::getAndSpliceHeader())
|
|
|
121 |
, array(false, "POST / HTTP:/1.1\r\n" . static::getAndSpliceHeader())
|
|
|
122 |
, array(false, static::$good_rest . "\r\n" . static::getAndSpliceHeader('Upgrade', 'useless'))
|
|
|
123 |
, array(false, "GET /ಠ_ಠ HTTP/1.1\r\n" . static::getAndSpliceHeader())
|
|
|
124 |
, array(true, static::$good_rest . "\r\n" . static::getAndSpliceHeader('Connection', 'Herp, Upgrade, Derp'))
|
|
|
125 |
);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* @dataProvider headerHandshakeProvider
|
|
|
130 |
*/
|
|
|
131 |
public function testVariousHeadersToCheckHandshakeTolerance($pass, $header) {
|
|
|
132 |
$request = RequestFactory::getInstance()->fromMessage($header);
|
|
|
133 |
$response = $this->version->handshake($request);
|
|
|
134 |
|
|
|
135 |
$this->assertInstanceOf('\\Guzzle\\Http\\Message\\Response', $response);
|
|
|
136 |
|
|
|
137 |
if ($pass) {
|
|
|
138 |
$this->assertEquals(101, $response->getStatusCode());
|
|
|
139 |
} else {
|
|
|
140 |
$this->assertGreaterThanOrEqual(400, $response->getStatusCode());
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
public function testNewMessage() {
|
|
|
145 |
$this->assertInstanceOf('\\Ratchet\\WebSocket\\Version\\RFC6455\\Message', $this->version->newMessage());
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
public function testNewFrame() {
|
|
|
149 |
$this->assertInstanceOf('\\Ratchet\\WebSocket\\Version\\RFC6455\\Frame', $this->version->newFrame());
|
|
|
150 |
}
|
|
|
151 |
}
|