forked from varspool/Wrench
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathClientSocketTest.php
More file actions
155 lines (124 loc) · 3.91 KB
/
Copy pathClientSocketTest.php
File metadata and controls
155 lines (124 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Wrench\Socket;
use Exception;
use InvalidArgumentException;
use stdClass;
use TypeError;
use Wrench\Exception\SocketException;
use Wrench\Protocol\Rfc6455Protocol;
use Wrench\Test\ServerTestHelper;
class ClientSocketTest extends UriSocketBaseTest
{
public function testConstructor(): void
{
$instance = self::getInstance('ws://localhost:8000');
$this->assertInstanceOfClass($instance);
$socket = null;
$this->assertInstanceOfClass(
new ClientSocket('ws://localhost/'),
'ws:// scheme, default port'
);
$this->assertInstanceOfClass(
new ClientSocket('ws://localhost/some-arbitrary-path'),
'with path'
);
$this->assertInstanceOfClass(
new ClientSocket('wss://localhost/test', []),
'empty options'
);
$this->assertInstanceOfClass(
new ClientSocket('ws://localhost:8000/foo'),
'specified port'
);
}
public function testOptions(): void
{
$socket = null;
$this->assertInstanceOfClass(
$socket = new ClientSocket(
'ws://localhost:8000/foo',
[
'timeout_connect' => 10,
]
),
'connect timeout'
);
$this->assertInstanceOfClass(
$socket = new ClientSocket(
'ws://localhost:8000/foo',
[
'timeout_socket' => 10,
]
),
'socket timeout'
);
$this->assertInstanceOfClass(
$socket = new ClientSocket(
'ws://localhost:8000/foo',
[
'protocol' => new Rfc6455Protocol(),
]
),
'protocol'
);
}
public function testProtocolTypeError(): void
{
$this->expectException(InvalidArgumentException::class);
new ClientSocket(
'ws://localhost:8000/foo',
[
'protocol' => new stdClass(),
]
);
}
public function testConstructorUriEmpty(): void
{
$this->expectException(TypeError::class);
new ClientSocket(null);
}
public function testConstructorUriInvalid(): void
{
$this->expectException(InvalidArgumentException::class);
new ClientSocket('Bad argument');
}
public function testSendTooEarly(): void
{
$instance = self::getInstance('ws://localhost:8000');
$this->expectException(SocketException::class);
$instance->send('foo');
}
public function testWaitForDataTooEarly(): void
{
$instance = self::getInstance('ws://localhost:8000');
self::assertNull($instance->waitForData(0));
}
/**
* Test the connect, send, receive method.
*/
public function testConnect(): void
{
try {
$helper = new ServerTestHelper();
$helper->setUp();
$instance = self::getInstance($helper->getConnectionString());
$success = $instance->connect();
self::assertTrue($success, 'Client socket can connect to test server');
$sent = $instance->send("GET /echo HTTP/1.1\r
Host: localhost\r
Upgrade: websocket\r
Connection: Upgrade\r
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r
Origin: http://localhost\r
Sec-WebSocket-Version: 13\r\n\r\n");
self::assertNotEquals(false, $sent, 'Client socket can send to test server');
self::assertTrue($instance->waitForData(AbstractSocket::TIMEOUT_SOCKET), 'Data arrives from test server');
$response = $instance->receive();
self::assertStringStartsWith('HTTP', $response, 'Response looks like HTTP handshake response');
} catch (Exception $e) {
$helper->tearDown();
throw $e;
}
$helper->tearDown();
}
}