Subversion Repositories php-qbpwcf

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace Ratchet\RFC6455\Test\Unit\Handshake;
4
 
5
use Ratchet\RFC6455\Handshake\RequestVerifier;
6
use Ratchet\RFC6455\Handshake\ServerNegotiator;
7
use PHPUnit\Framework\TestCase;
8
 
9
class ServerNegotiatorTest extends TestCase
10
{
11
    public function testNoUpgradeRequested() {
12
        $negotiator = new ServerNegotiator(new RequestVerifier());
13
 
14
        $requestText = 'GET / HTTP/1.1
15
Host: 127.0.0.1:6789
16
Connection: keep-alive
17
Pragma: no-cache
18
Cache-Control: no-cache
19
Upgrade-Insecure-Requests: 1
20
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
21
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
22
Accept-Encoding: gzip, deflate, sdch, br
23
Accept-Language: en-US,en;q=0.8
24
 
25
';
26
 
27
        $request = \GuzzleHttp\Psr7\parse_request($requestText);
28
 
29
        $response = $negotiator->handshake($request);
30
 
31
        $this->assertEquals('1.1', $response->getProtocolVersion());
32
        $this->assertEquals(426, $response->getStatusCode());
33
        $this->assertEquals('Upgrade header MUST be provided', $response->getReasonPhrase());
34
        $this->assertEquals('Upgrade', $response->getHeaderLine('Connection'));
35
        $this->assertEquals('websocket', $response->getHeaderLine('Upgrade'));
36
        $this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version'));
37
    }
38
 
39
    public function testNoConnectionUpgradeRequested() {
40
        $negotiator = new ServerNegotiator(new RequestVerifier());
41
 
42
        $requestText = 'GET / HTTP/1.1
43
Host: 127.0.0.1:6789
44
Connection: keep-alive
45
Pragma: no-cache
46
Cache-Control: no-cache
47
Upgrade: websocket
48
Upgrade-Insecure-Requests: 1
49
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
50
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
51
Accept-Encoding: gzip, deflate, sdch, br
52
Accept-Language: en-US,en;q=0.8
53
 
54
';
55
 
56
        $request = \GuzzleHttp\Psr7\parse_request($requestText);
57
 
58
        $response = $negotiator->handshake($request);
59
 
60
        $this->assertEquals('1.1', $response->getProtocolVersion());
61
        $this->assertEquals(400, $response->getStatusCode());
62
        $this->assertEquals('Connection Upgrade MUST be requested', $response->getReasonPhrase());
63
    }
64
 
65
    public function testInvalidSecWebsocketKey() {
66
        $negotiator = new ServerNegotiator(new RequestVerifier());
67
 
68
        $requestText = 'GET / HTTP/1.1
69
Host: 127.0.0.1:6789
70
Connection: Upgrade
71
Pragma: no-cache
72
Cache-Control: no-cache
73
Upgrade: websocket
74
Sec-WebSocket-Key: 12345
75
Upgrade-Insecure-Requests: 1
76
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
77
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
78
Accept-Encoding: gzip, deflate, sdch, br
79
Accept-Language: en-US,en;q=0.8
80
 
81
';
82
 
83
        $request = \GuzzleHttp\Psr7\parse_request($requestText);
84
 
85
        $response = $negotiator->handshake($request);
86
 
87
        $this->assertEquals('1.1', $response->getProtocolVersion());
88
        $this->assertEquals(400, $response->getStatusCode());
89
        $this->assertEquals('Invalid Sec-WebSocket-Key', $response->getReasonPhrase());
90
    }
91
 
92
    public function testInvalidSecWebsocketVersion() {
93
        $negotiator = new ServerNegotiator(new RequestVerifier());
94
 
95
        $requestText = 'GET / HTTP/1.1
96
Host: 127.0.0.1:6789
97
Connection: Upgrade
98
Pragma: no-cache
99
Cache-Control: no-cache
100
Upgrade: websocket
101
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
102
Upgrade-Insecure-Requests: 1
103
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
104
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
105
Accept-Encoding: gzip, deflate, sdch, br
106
Accept-Language: en-US,en;q=0.8
107
 
108
';
109
 
110
        $request = \GuzzleHttp\Psr7\parse_request($requestText);
111
 
112
        $response = $negotiator->handshake($request);
113
 
114
        $this->assertEquals('1.1', $response->getProtocolVersion());
115
        $this->assertEquals(426, $response->getStatusCode());
116
        $this->assertEquals('Upgrade Required', $response->getReasonPhrase());
117
        $this->assertEquals('Upgrade', $response->getHeaderLine('Connection'));
118
        $this->assertEquals('websocket', $response->getHeaderLine('Upgrade'));
119
        $this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version'));
120
    }
121
 
122
    public function testBadSubprotocolResponse() {
123
        $negotiator = new ServerNegotiator(new RequestVerifier());
124
        $negotiator->setStrictSubProtocolCheck(true);
125
        $negotiator->setSupportedSubProtocols([]);
126
 
127
        $requestText = 'GET / HTTP/1.1
128
Host: 127.0.0.1:6789
129
Connection: Upgrade
130
Pragma: no-cache
131
Cache-Control: no-cache
132
Upgrade: websocket
133
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
134
Sec-WebSocket-Version: 13
135
Sec-WebSocket-Protocol: someprotocol
136
Upgrade-Insecure-Requests: 1
137
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
138
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
139
Accept-Encoding: gzip, deflate, sdch, br
140
Accept-Language: en-US,en;q=0.8
141
 
142
';
143
 
144
        $request = \GuzzleHttp\Psr7\parse_request($requestText);
145
 
146
        $response = $negotiator->handshake($request);
147
 
148
        $this->assertEquals('1.1', $response->getProtocolVersion());
149
        $this->assertEquals(426, $response->getStatusCode());
150
        $this->assertEquals('No Sec-WebSocket-Protocols requested supported', $response->getReasonPhrase());
151
        $this->assertEquals('Upgrade', $response->getHeaderLine('Connection'));
152
        $this->assertEquals('websocket', $response->getHeaderLine('Upgrade'));
153
        $this->assertEquals('13', $response->getHeaderLine('Sec-WebSocket-Version'));
154
    }
155
 
156
    public function testNonStrictSubprotocolDoesNotIncludeHeaderWhenNoneAgreedOn() {
157
        $negotiator = new ServerNegotiator(new RequestVerifier());
158
        $negotiator->setStrictSubProtocolCheck(false);
159
        $negotiator->setSupportedSubProtocols(['someproto']);
160
 
161
        $requestText = 'GET / HTTP/1.1
162
Host: 127.0.0.1:6789
163
Connection: Upgrade
164
Pragma: no-cache
165
Cache-Control: no-cache
166
Upgrade: websocket
167
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
168
Sec-WebSocket-Version: 13
169
Sec-WebSocket-Protocol: someotherproto
170
Upgrade-Insecure-Requests: 1
171
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
172
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
173
Accept-Encoding: gzip, deflate, sdch, br
174
Accept-Language: en-US,en;q=0.8
175
 
176
';
177
 
178
        $request = \GuzzleHttp\Psr7\parse_request($requestText);
179
 
180
        $response = $negotiator->handshake($request);
181
 
182
        $this->assertEquals('1.1', $response->getProtocolVersion());
183
        $this->assertEquals(101, $response->getStatusCode());
184
        $this->assertEquals('Upgrade', $response->getHeaderLine('Connection'));
185
        $this->assertEquals('websocket', $response->getHeaderLine('Upgrade'));
186
        $this->assertFalse($response->hasHeader('Sec-WebSocket-Protocol'));
187
    }
188
 
189
    public function testSuggestsAppropriateSubprotocol()
190
    {
191
        $negotiator = new ServerNegotiator(new RequestVerifier());
192
        $negotiator->setStrictSubProtocolCheck(true);
193
        $negotiator->setSupportedSubProtocols(['someproto']);
194
 
195
        $requestText = 'GET / HTTP/1.1
196
Host: localhost:8080
197
Connection: Upgrade
198
Upgrade: websocket
199
Sec-WebSocket-Version: 13
200
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
201
Accept-Encoding: gzip, deflate, br
202
Accept-Language: en-US,en;q=0.9
203
Sec-WebSocket-Key: HGt8eQax7nAOlXUw0/asPQ==
204
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
205
 
206
';
207
 
208
        $request = \GuzzleHttp\Psr7\parse_request($requestText);
209
 
210
        $response = $negotiator->handshake($request);
211
 
212
        $this->assertEquals('1.1', $response->getProtocolVersion());
213
        $this->assertEquals(426, $response->getStatusCode());
214
        $this->assertEquals('Upgrade', $response->getHeaderLine('Connection'));
215
        $this->assertEquals('websocket', $response->getHeaderLine('Upgrade'));
216
        $this->assertEquals('someproto', $response->getHeaderLine('Sec-WebSocket-Protocol'));
217
    }
218
}