| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
namespace Ratchet\Wamp;
|
|
|
3 |
use Ratchet\Mock\Connection;
|
|
|
4 |
use Ratchet\Mock\WampComponent as TestComponent;
|
|
|
5 |
|
|
|
6 |
/**
|
|
|
7 |
* @covers \Ratchet\Wamp\ServerProtocol
|
|
|
8 |
* @covers \Ratchet\Wamp\WampServerInterface
|
|
|
9 |
* @covers \Ratchet\Wamp\WampConnection
|
|
|
10 |
*/
|
|
|
11 |
class ServerProtocolTest extends \PHPUnit_Framework_TestCase {
|
|
|
12 |
protected $_comp;
|
|
|
13 |
|
|
|
14 |
protected $_app;
|
|
|
15 |
|
|
|
16 |
public function setUp() {
|
|
|
17 |
$this->_app = new TestComponent;
|
|
|
18 |
$this->_comp = new ServerProtocol($this->_app);
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
protected function newConn() {
|
|
|
22 |
return new Connection;
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
public function invalidMessageProvider() {
|
|
|
26 |
return [
|
|
|
27 |
[0]
|
|
|
28 |
, [3]
|
|
|
29 |
, [4]
|
|
|
30 |
, [8]
|
|
|
31 |
, [9]
|
|
|
32 |
];
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @dataProvider invalidMessageProvider
|
|
|
37 |
*/
|
|
|
38 |
public function testInvalidMessages($type) {
|
|
|
39 |
$this->setExpectedException('\Ratchet\Wamp\Exception');
|
|
|
40 |
|
|
|
41 |
$conn = $this->newConn();
|
|
|
42 |
$this->_comp->onOpen($conn);
|
|
|
43 |
$this->_comp->onMessage($conn, json_encode([$type]));
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function testWelcomeMessage() {
|
|
|
47 |
$conn = $this->newConn();
|
|
|
48 |
|
|
|
49 |
$this->_comp->onOpen($conn);
|
|
|
50 |
|
|
|
51 |
$message = $conn->last['send'];
|
|
|
52 |
$json = json_decode($message);
|
|
|
53 |
|
|
|
54 |
$this->assertEquals(4, count($json));
|
|
|
55 |
$this->assertEquals(0, $json[0]);
|
|
|
56 |
$this->assertTrue(is_string($json[1]));
|
|
|
57 |
$this->assertEquals(1, $json[2]);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
public function testSubscribe() {
|
|
|
61 |
$uri = 'http://example.com';
|
|
|
62 |
$clientMessage = array(5, $uri);
|
|
|
63 |
|
|
|
64 |
$conn = $this->newConn();
|
|
|
65 |
|
|
|
66 |
$this->_comp->onOpen($conn);
|
|
|
67 |
$this->_comp->onMessage($conn, json_encode($clientMessage));
|
|
|
68 |
|
|
|
69 |
$this->assertEquals($uri, $this->_app->last['onSubscribe'][1]);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public function testUnSubscribe() {
|
|
|
73 |
$uri = 'http://example.com/endpoint';
|
|
|
74 |
$clientMessage = array(6, $uri);
|
|
|
75 |
|
|
|
76 |
$conn = $this->newConn();
|
|
|
77 |
|
|
|
78 |
$this->_comp->onOpen($conn);
|
|
|
79 |
$this->_comp->onMessage($conn, json_encode($clientMessage));
|
|
|
80 |
|
|
|
81 |
$this->assertEquals($uri, $this->_app->last['onUnSubscribe'][1]);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public function callProvider() {
|
|
|
85 |
return [
|
|
|
86 |
[2, 'a', 'b']
|
|
|
87 |
, [2, ['a', 'b']]
|
|
|
88 |
, [1, 'one']
|
|
|
89 |
, [3, 'one', 'two', 'three']
|
|
|
90 |
, [3, ['un', 'deux', 'trois']]
|
|
|
91 |
, [2, 'hi', ['hello', 'world']]
|
|
|
92 |
, [2, ['hello', 'world'], 'hi']
|
|
|
93 |
, [2, ['hello' => 'world', 'herp' => 'derp']]
|
|
|
94 |
];
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* @dataProvider callProvider
|
|
|
99 |
*/
|
|
|
100 |
public function testCall() {
|
|
|
101 |
$args = func_get_args();
|
|
|
102 |
$paramNum = array_shift($args);
|
|
|
103 |
|
|
|
104 |
$uri = 'http://example.com/endpoint/' . rand(1, 100);
|
|
|
105 |
$id = uniqid('', false);
|
|
|
106 |
$clientMessage = array_merge(array(2, $id, $uri), $args);
|
|
|
107 |
|
|
|
108 |
$conn = $this->newConn();
|
|
|
109 |
|
|
|
110 |
$this->_comp->onOpen($conn);
|
|
|
111 |
$this->_comp->onMessage($conn, json_encode($clientMessage));
|
|
|
112 |
|
|
|
113 |
$this->assertEquals($id, $this->_app->last['onCall'][1]);
|
|
|
114 |
$this->assertEquals($uri, $this->_app->last['onCall'][2]);
|
|
|
115 |
|
|
|
116 |
$this->assertEquals($paramNum, count($this->_app->last['onCall'][3]));
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public function testPublish() {
|
|
|
120 |
$conn = $this->newConn();
|
|
|
121 |
|
|
|
122 |
$topic = 'pubsubhubbub';
|
|
|
123 |
$event = 'Here I am, publishing data';
|
|
|
124 |
|
|
|
125 |
$clientMessage = array(7, $topic, $event);
|
|
|
126 |
|
|
|
127 |
$this->_comp->onOpen($conn);
|
|
|
128 |
$this->_comp->onMessage($conn, json_encode($clientMessage));
|
|
|
129 |
|
|
|
130 |
$this->assertEquals($topic, $this->_app->last['onPublish'][1]);
|
|
|
131 |
$this->assertEquals($event, $this->_app->last['onPublish'][2]);
|
|
|
132 |
$this->assertEquals(array(), $this->_app->last['onPublish'][3]);
|
|
|
133 |
$this->assertEquals(array(), $this->_app->last['onPublish'][4]);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
public function testPublishAndExcludeMe() {
|
|
|
137 |
$conn = $this->newConn();
|
|
|
138 |
|
|
|
139 |
$this->_comp->onOpen($conn);
|
|
|
140 |
$this->_comp->onMessage($conn, json_encode(array(7, 'topic', 'event', true)));
|
|
|
141 |
|
|
|
142 |
$this->assertEquals($conn->WAMP->sessionId, $this->_app->last['onPublish'][3][0]);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
public function testPublishAndEligible() {
|
|
|
146 |
$conn = $this->newConn();
|
|
|
147 |
|
|
|
148 |
$buddy = uniqid('', false);
|
|
|
149 |
$friend = uniqid('', false);
|
|
|
150 |
|
|
|
151 |
$this->_comp->onOpen($conn);
|
|
|
152 |
$this->_comp->onMessage($conn, json_encode(array(7, 'topic', 'event', false, array($buddy, $friend))));
|
|
|
153 |
|
|
|
154 |
$this->assertEquals(array(), $this->_app->last['onPublish'][3]);
|
|
|
155 |
$this->assertEquals(2, count($this->_app->last['onPublish'][4]));
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
public function eventProvider() {
|
|
|
159 |
return array(
|
|
|
160 |
array('http://example.com', array('one', 'two'))
|
|
|
161 |
, array('curie', array(array('hello' => 'world', 'herp' => 'derp')))
|
|
|
162 |
);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* @dataProvider eventProvider
|
|
|
167 |
*/
|
|
|
168 |
public function testEvent($topic, $payload) {
|
|
|
169 |
$conn = new WampConnection($this->newConn());
|
|
|
170 |
$conn->event($topic, $payload);
|
|
|
171 |
|
|
|
172 |
$eventString = $conn->last['send'];
|
|
|
173 |
|
|
|
174 |
$this->assertSame(array(8, $topic, $payload), json_decode($eventString, true));
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
public function testOnClosePropagation() {
|
|
|
178 |
$conn = new Connection;
|
|
|
179 |
|
|
|
180 |
$this->_comp->onOpen($conn);
|
|
|
181 |
$this->_comp->onClose($conn);
|
|
|
182 |
|
|
|
183 |
$class = new \ReflectionClass('\\Ratchet\\Wamp\\WampConnection');
|
|
|
184 |
$method = $class->getMethod('getConnection');
|
|
|
185 |
$method->setAccessible(true);
|
|
|
186 |
|
|
|
187 |
$check = $method->invokeArgs($this->_app->last['onClose'][0], array());
|
|
|
188 |
|
|
|
189 |
$this->assertSame($conn, $check);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
public function testOnErrorPropagation() {
|
|
|
193 |
$conn = new Connection;
|
|
|
194 |
|
|
|
195 |
$e = new \Exception('Nope');
|
|
|
196 |
|
|
|
197 |
$this->_comp->onOpen($conn);
|
|
|
198 |
$this->_comp->onError($conn, $e);
|
|
|
199 |
|
|
|
200 |
$class = new \ReflectionClass('\\Ratchet\\Wamp\\WampConnection');
|
|
|
201 |
$method = $class->getMethod('getConnection');
|
|
|
202 |
$method->setAccessible(true);
|
|
|
203 |
|
|
|
204 |
$check = $method->invokeArgs($this->_app->last['onError'][0], array());
|
|
|
205 |
|
|
|
206 |
$this->assertSame($conn, $check);
|
|
|
207 |
$this->assertSame($e, $this->_app->last['onError'][1]);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
public function testPrefix() {
|
|
|
211 |
$conn = new WampConnection($this->newConn());
|
|
|
212 |
$this->_comp->onOpen($conn);
|
|
|
213 |
|
|
|
214 |
$prefix = 'incoming';
|
|
|
215 |
$fullURI = "http://example.com/$prefix";
|
|
|
216 |
$method = 'call';
|
|
|
217 |
|
|
|
218 |
$this->_comp->onMessage($conn, json_encode(array(1, $prefix, $fullURI)));
|
|
|
219 |
|
|
|
220 |
$this->assertEquals($fullURI, $conn->WAMP->prefixes[$prefix]);
|
|
|
221 |
$this->assertEquals("$fullURI#$method", $conn->getUri("$prefix:$method"));
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
public function testMessageMustBeJson() {
|
|
|
225 |
$this->setExpectedException('\\Ratchet\\Wamp\\JsonException');
|
|
|
226 |
|
|
|
227 |
$conn = new Connection;
|
|
|
228 |
|
|
|
229 |
$this->_comp->onOpen($conn);
|
|
|
230 |
$this->_comp->onMessage($conn, 'Hello World!');
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
public function testGetSubProtocolsReturnsArray() {
|
|
|
234 |
$this->assertTrue(is_array($this->_comp->getSubProtocols()));
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
public function testGetSubProtocolsGetFromApp() {
|
|
|
238 |
$this->_app->protocols = array('hello', 'world');
|
|
|
239 |
|
|
|
240 |
$this->assertGreaterThanOrEqual(3, count($this->_comp->getSubProtocols()));
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
public function testWampOnMessageApp() {
|
|
|
244 |
$app = $this->getMock('\\Ratchet\\Wamp\\WampServerInterface');
|
|
|
245 |
$wamp = new ServerProtocol($app);
|
|
|
246 |
|
|
|
247 |
$this->assertContains('wamp', $wamp->getSubProtocols());
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
public function badFormatProvider() {
|
|
|
251 |
return array(
|
|
|
252 |
array(json_encode(true))
|
|
|
253 |
, array('{"valid":"json", "invalid": "message"}')
|
|
|
254 |
, array('{"0": "fail", "hello": "world"}')
|
|
|
255 |
);
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
/**
|
|
|
259 |
* @dataProvider badFormatProvider
|
|
|
260 |
*/
|
|
|
261 |
public function testValidJsonButInvalidProtocol($message) {
|
|
|
262 |
$this->setExpectedException('\Ratchet\Wamp\Exception');
|
|
|
263 |
|
|
|
264 |
$conn = $this->newConn();
|
|
|
265 |
$this->_comp->onOpen($conn);
|
|
|
266 |
$this->_comp->onMessage($conn, $message);
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
public function testBadClientInputFromNonStringTopic() {
|
|
|
270 |
$this->setExpectedException('\Ratchet\Wamp\Exception');
|
|
|
271 |
|
|
|
272 |
$conn = new WampConnection($this->newConn());
|
|
|
273 |
$this->_comp->onOpen($conn);
|
|
|
274 |
|
|
|
275 |
$this->_comp->onMessage($conn, json_encode([5, ['hells', 'nope']]));
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
public function testBadPrefixWithNonStringTopic() {
|
|
|
279 |
$this->setExpectedException('\Ratchet\Wamp\Exception');
|
|
|
280 |
|
|
|
281 |
$conn = new WampConnection($this->newConn());
|
|
|
282 |
$this->_comp->onOpen($conn);
|
|
|
283 |
|
|
|
284 |
$this->_comp->onMessage($conn, json_encode([1, ['hells', 'nope'], ['bad', 'input']]));
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
public function testBadPublishWithNonStringTopic() {
|
|
|
288 |
$this->setExpectedException('\Ratchet\Wamp\Exception');
|
|
|
289 |
|
|
|
290 |
$conn = new WampConnection($this->newConn());
|
|
|
291 |
$this->_comp->onOpen($conn);
|
|
|
292 |
|
|
|
293 |
$this->_comp->onMessage($conn, json_encode([7, ['bad', 'input'], 'Hider']));
|
|
|
294 |
}
|
|
|
295 |
}
|