| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
namespace Ratchet\WebSocket\Version;
|
|
|
3 |
use Ratchet\WebSocket\Version\Hixie76;
|
|
|
4 |
use Ratchet\Http\HttpServer;
|
|
|
5 |
use Ratchet\WebSocket\WsServer;
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* @covers Ratchet\WebSocket\Version\Hixie76
|
|
|
9 |
*/
|
|
|
10 |
class Hixie76Test extends \PHPUnit_Framework_TestCase {
|
|
|
11 |
protected $_crlf = "\r\n";
|
|
|
12 |
protected $_body = '6dW+XgKfWV0=';
|
|
|
13 |
|
|
|
14 |
protected $_version;
|
|
|
15 |
|
|
|
16 |
public function setUp() {
|
|
|
17 |
$this->_version = new Hixie76;
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
public function testClassImplementsVersionInterface() {
|
|
|
21 |
$constraint = $this->isInstanceOf('\\Ratchet\\WebSocket\\Version\\VersionInterface');
|
|
|
22 |
$this->assertThat($this->_version, $constraint);
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* @dataProvider keyProvider
|
|
|
27 |
*/
|
|
|
28 |
public function testKeySigningForHandshake($accept, $key) {
|
|
|
29 |
$this->assertEquals($accept, $this->_version->generateKeyNumber($key));
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public static function keyProvider() {
|
|
|
33 |
return array(
|
|
|
34 |
array(179922739, '17 9 G`ZD9 2 2b 7X 3 /r90')
|
|
|
35 |
, array(906585445, '3e6b263 4 17 80')
|
|
|
36 |
, array(0, '3e6b26341780')
|
|
|
37 |
);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public function headerProvider() {
|
|
|
41 |
$key1 = base64_decode('QTN+ICszNiA2IDJvICBWOG4gNyAgc08yODhZ');
|
|
|
42 |
$key2 = base64_decode('TzEyICAgeVsgIFFSNDUgM1IgLiAyOFggNC00dn4z');
|
|
|
43 |
|
|
|
44 |
$headers = "GET / HTTP/1.1";
|
|
|
45 |
$headers .= "Upgrade: WebSocket{$this->_crlf}";
|
|
|
46 |
$headers .= "Connection: Upgrade{$this->_crlf}";
|
|
|
47 |
$headers .= "Host: socketo.me{$this->_crlf}";
|
|
|
48 |
$headers .= "Origin: http://fiddle.jshell.net{$this->_crlf}";
|
|
|
49 |
$headers .= "Sec-WebSocket-Key1:17 Z4< F94 N3 7P41 7{$this->_crlf}";
|
|
|
50 |
$headers .= "Sec-WebSocket-Key2:1 23C3:,2% 1-29 4 f0{$this->_crlf}";
|
|
|
51 |
$headers .= "(Key3):70:00:EE:6E:33:20:90:69{$this->_crlf}";
|
|
|
52 |
$headers .= $this->_crlf;
|
|
|
53 |
|
|
|
54 |
return $headers;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public function testNoUpgradeBeforeBody() {
|
|
|
58 |
$headers = $this->headerProvider();
|
|
|
59 |
|
|
|
60 |
$mockConn = $this->getMock('\Ratchet\ConnectionInterface');
|
|
|
61 |
$mockApp = $this->getMock('\Ratchet\MessageComponentInterface');
|
|
|
62 |
|
|
|
63 |
$server = new HttpServer(new WsServer($mockApp));
|
|
|
64 |
$server->onOpen($mockConn);
|
|
|
65 |
$mockApp->expects($this->exactly(0))->method('onOpen');
|
|
|
66 |
$server->onMessage($mockConn, $headers);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
public function testTcpFragmentedUpgrade() {
|
|
|
70 |
$headers = $this->headerProvider();
|
|
|
71 |
$body = base64_decode($this->_body);
|
|
|
72 |
|
|
|
73 |
$mockConn = $this->getMock('\Ratchet\ConnectionInterface');
|
|
|
74 |
$mockApp = $this->getMock('\Ratchet\MessageComponentInterface');
|
|
|
75 |
|
|
|
76 |
$server = new HttpServer(new WsServer($mockApp));
|
|
|
77 |
$server->onOpen($mockConn);
|
|
|
78 |
$server->onMessage($mockConn, $headers);
|
|
|
79 |
|
|
|
80 |
$mockApp->expects($this->once())->method('onOpen');
|
|
|
81 |
$server->onMessage($mockConn, $body . $this->_crlf . $this->_crlf);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public function testTcpFragmentedBodyUpgrade() {
|
|
|
85 |
$headers = $this->headerProvider();
|
|
|
86 |
$body = base64_decode($this->_body);
|
|
|
87 |
$body1 = substr($body, 0, 4);
|
|
|
88 |
$body2 = substr($body, 4);
|
|
|
89 |
|
|
|
90 |
$mockConn = $this->getMock('\Ratchet\ConnectionInterface');
|
|
|
91 |
$mockApp = $this->getMock('\Ratchet\MessageComponentInterface');
|
|
|
92 |
|
|
|
93 |
$server = new HttpServer(new WsServer($mockApp));
|
|
|
94 |
$server->onOpen($mockConn);
|
|
|
95 |
$server->onMessage($mockConn, $headers);
|
|
|
96 |
|
|
|
97 |
$mockApp->expects($this->once())->method('onOpen');
|
|
|
98 |
|
|
|
99 |
$server->onMessage($mockConn, $body1);
|
|
|
100 |
$server->onMessage($mockConn, $body2);
|
|
|
101 |
$server->onMessage($mockConn, $this->_crlf . $this->_crlf);
|
|
|
102 |
}
|
|
|
103 |
}
|