@ohos-ports/http-deceiver 0.0.0-stage → 1.2.7-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.travis.yml +5 -0
- package/README.md +30 -2
- package/lib/deceiver.js +273 -0
- package/package.json +29 -4
- package/test/api-test.js +226 -0
package/.travis.yml
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# HTTP Deceiver
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](http://travis-ci.org/indutny/http-deceiver)
|
|
4
|
+
[](http://badge.fury.io/js/http-deceiver)
|
|
5
|
+
|
|
6
|
+
Deceive!
|
|
7
|
+
|
|
8
|
+
## LICENSE
|
|
9
|
+
|
|
10
|
+
This software is licensed under the MIT License.
|
|
11
|
+
|
|
12
|
+
Copyright Fedor Indutny, 2015.
|
|
13
|
+
|
|
14
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
|
15
|
+
copy of this software and associated documentation files (the
|
|
16
|
+
"Software"), to deal in the Software without restriction, including
|
|
17
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
18
|
+
distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
19
|
+
persons to whom the Software is furnished to do so, subject to the
|
|
20
|
+
following conditions:
|
|
21
|
+
|
|
22
|
+
The above copyright notice and this permission notice shall be included
|
|
23
|
+
in all copies or substantial portions of the Software.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
26
|
+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
27
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
28
|
+
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
29
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
30
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
31
|
+
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/lib/deceiver.js
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
var assert = require('assert');
|
|
2
|
+
var util = require('util');
|
|
3
|
+
|
|
4
|
+
var Buffer = require('buffer').Buffer;
|
|
5
|
+
|
|
6
|
+
// Node.js version
|
|
7
|
+
var mode = /^v0\.8\./.test(process.version) ? 'rusty' :
|
|
8
|
+
/^v0\.(9|10)\./.test(process.version) ? 'old' :
|
|
9
|
+
/^v0\.12\./.test(process.version) ? 'normal' :
|
|
10
|
+
'modern';
|
|
11
|
+
|
|
12
|
+
var HTTPParser;
|
|
13
|
+
|
|
14
|
+
var methods;
|
|
15
|
+
var reverseMethods;
|
|
16
|
+
|
|
17
|
+
var kOnHeaders;
|
|
18
|
+
var kOnHeadersComplete;
|
|
19
|
+
var kOnMessageComplete;
|
|
20
|
+
var kOnBody;
|
|
21
|
+
if (mode === 'normal' || mode === 'modern') {
|
|
22
|
+
// Node.js >= 12 replaced the internal `http_parser` binding with llhttp and
|
|
23
|
+
// `process.binding('http_parser')` throws. The llhttp parser instances
|
|
24
|
+
// still use the same numeric callback-slot constants and the same 9-argument
|
|
25
|
+
// `onHeadersComplete` signature as the old binding-based parser, and the
|
|
26
|
+
// method-id table is exported as the public `http.METHODS` array (ordered by
|
|
27
|
+
// method id), so fall back to those when the binding is unavailable.
|
|
28
|
+
var httpParserBinding = null;
|
|
29
|
+
try {
|
|
30
|
+
httpParserBinding = process.binding('http_parser');
|
|
31
|
+
} catch (e) {
|
|
32
|
+
httpParserBinding = null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (httpParserBinding) {
|
|
36
|
+
HTTPParser = httpParserBinding.HTTPParser;
|
|
37
|
+
methods = HTTPParser.methods;
|
|
38
|
+
|
|
39
|
+
// v6
|
|
40
|
+
if (!methods)
|
|
41
|
+
methods = httpParserBinding.methods;
|
|
42
|
+
} else {
|
|
43
|
+
methods = require('http').METHODS;
|
|
44
|
+
HTTPParser = {
|
|
45
|
+
kOnHeaders: 1,
|
|
46
|
+
kOnHeadersComplete: 2,
|
|
47
|
+
kOnBody: 3,
|
|
48
|
+
kOnMessageComplete: 4
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
reverseMethods = {};
|
|
53
|
+
|
|
54
|
+
methods.forEach(function(method, index) {
|
|
55
|
+
reverseMethods[method] = index;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
kOnHeaders = HTTPParser.kOnHeaders | 0;
|
|
59
|
+
kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
|
|
60
|
+
kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
|
|
61
|
+
kOnBody = HTTPParser.kOnBody | 0;
|
|
62
|
+
} else {
|
|
63
|
+
kOnHeaders = 'onHeaders';
|
|
64
|
+
kOnHeadersComplete = 'onHeadersComplete';
|
|
65
|
+
kOnMessageComplete = 'onMessageComplete';
|
|
66
|
+
kOnBody = 'onBody';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function Deceiver(socket, options) {
|
|
70
|
+
this.socket = socket;
|
|
71
|
+
this.options = options || {};
|
|
72
|
+
this.isClient = this.options.isClient;
|
|
73
|
+
}
|
|
74
|
+
module.exports = Deceiver;
|
|
75
|
+
|
|
76
|
+
Deceiver.create = function create(stream, options) {
|
|
77
|
+
return new Deceiver(stream, options);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
Deceiver.prototype._toHeaderList = function _toHeaderList(object) {
|
|
81
|
+
var out = [];
|
|
82
|
+
var keys = Object.keys(object);
|
|
83
|
+
|
|
84
|
+
for (var i = 0; i < keys.length; i++)
|
|
85
|
+
out.push(keys[i], object[keys[i]]);
|
|
86
|
+
|
|
87
|
+
return out;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
Deceiver.prototype._isUpgrade = function _isUpgrade(request) {
|
|
91
|
+
return request.method === 'CONNECT' ||
|
|
92
|
+
request.headers.upgrade ||
|
|
93
|
+
request.headers.connection &&
|
|
94
|
+
/(^|\W)upgrade(\W|$)/i.test(request.headers.connection);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// TODO(indutny): support CONNECT
|
|
98
|
+
if (mode === 'modern') {
|
|
99
|
+
/*
|
|
100
|
+
function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
|
|
101
|
+
url, statusCode, statusMessage, upgrade,
|
|
102
|
+
shouldKeepAlive) {
|
|
103
|
+
*/
|
|
104
|
+
Deceiver.prototype.emitRequest = function emitRequest(request) {
|
|
105
|
+
var parser = this.socket.parser;
|
|
106
|
+
assert(parser, 'No parser present');
|
|
107
|
+
|
|
108
|
+
parser.execute = null;
|
|
109
|
+
|
|
110
|
+
var self = this;
|
|
111
|
+
var method = reverseMethods[request.method];
|
|
112
|
+
parser.execute = function execute() {
|
|
113
|
+
self._skipExecute(this);
|
|
114
|
+
this[kOnHeadersComplete](1,
|
|
115
|
+
1,
|
|
116
|
+
self._toHeaderList(request.headers),
|
|
117
|
+
method,
|
|
118
|
+
request.path,
|
|
119
|
+
0,
|
|
120
|
+
'',
|
|
121
|
+
self._isUpgrade(request),
|
|
122
|
+
true);
|
|
123
|
+
return 0;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
this._emitEmpty();
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
Deceiver.prototype.emitResponse = function emitResponse(response) {
|
|
130
|
+
var parser = this.socket.parser;
|
|
131
|
+
assert(parser, 'No parser present');
|
|
132
|
+
|
|
133
|
+
parser.execute = null;
|
|
134
|
+
|
|
135
|
+
var self = this;
|
|
136
|
+
parser.execute = function execute() {
|
|
137
|
+
self._skipExecute(this);
|
|
138
|
+
this[kOnHeadersComplete](1,
|
|
139
|
+
1,
|
|
140
|
+
self._toHeaderList(response.headers),
|
|
141
|
+
response.path,
|
|
142
|
+
response.code,
|
|
143
|
+
response.status,
|
|
144
|
+
response.reason || '',
|
|
145
|
+
self._isUpgrade(response),
|
|
146
|
+
true);
|
|
147
|
+
return 0;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
this._emitEmpty();
|
|
151
|
+
};
|
|
152
|
+
} else {
|
|
153
|
+
/*
|
|
154
|
+
`function parserOnHeadersComplete(info) {`
|
|
155
|
+
|
|
156
|
+
info = { .versionMajor, .versionMinor, .url, .headers, .method,
|
|
157
|
+
.statusCode, .statusMessage, .upgrade, .shouldKeepAlive }
|
|
158
|
+
*/
|
|
159
|
+
Deceiver.prototype.emitRequest = function emitRequest(request) {
|
|
160
|
+
var parser = this.socket.parser;
|
|
161
|
+
assert(parser, 'No parser present');
|
|
162
|
+
|
|
163
|
+
var method = request.method;
|
|
164
|
+
if (reverseMethods)
|
|
165
|
+
method = reverseMethods[method];
|
|
166
|
+
|
|
167
|
+
var info = {
|
|
168
|
+
versionMajor: 1,
|
|
169
|
+
versionMinor: 1,
|
|
170
|
+
url: request.path,
|
|
171
|
+
headers: this._toHeaderList(request.headers),
|
|
172
|
+
method: method,
|
|
173
|
+
statusCode: 0,
|
|
174
|
+
statusMessage: '',
|
|
175
|
+
upgrade: this._isUpgrade(request),
|
|
176
|
+
shouldKeepAlive: true
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
var self = this;
|
|
180
|
+
parser.execute = function execute() {
|
|
181
|
+
self._skipExecute(this);
|
|
182
|
+
this[kOnHeadersComplete](info);
|
|
183
|
+
return 0;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
this._emitEmpty();
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
Deceiver.prototype.emitResponse = function emitResponse(response) {
|
|
190
|
+
var parser = this.socket.parser;
|
|
191
|
+
assert(parser, 'No parser present');
|
|
192
|
+
|
|
193
|
+
var info = {
|
|
194
|
+
versionMajor: 1,
|
|
195
|
+
versionMinor: 1,
|
|
196
|
+
url: response.path,
|
|
197
|
+
headers: this._toHeaderList(response.headers),
|
|
198
|
+
method: false,
|
|
199
|
+
statusCode: response.status,
|
|
200
|
+
statusMessage: response.reason || '',
|
|
201
|
+
upgrade: this._isUpgrade(response),
|
|
202
|
+
shouldKeepAlive: true
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
var self = this;
|
|
206
|
+
parser.execute = function execute() {
|
|
207
|
+
self._skipExecute(this);
|
|
208
|
+
this[kOnHeadersComplete](info);
|
|
209
|
+
return 0;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
this._emitEmpty();
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
Deceiver.prototype._skipExecute = function _skipExecute(parser) {
|
|
217
|
+
var self = this;
|
|
218
|
+
var oldExecute = parser.constructor.prototype.execute;
|
|
219
|
+
var oldFinish = parser.constructor.prototype.finish;
|
|
220
|
+
|
|
221
|
+
parser.execute = null;
|
|
222
|
+
parser.finish = null;
|
|
223
|
+
|
|
224
|
+
parser.execute = function execute(buffer, start, len) {
|
|
225
|
+
// Parser reuse
|
|
226
|
+
if (this.socket !== self.socket) {
|
|
227
|
+
this.execute = oldExecute;
|
|
228
|
+
this.finish = oldFinish;
|
|
229
|
+
return this.execute(buffer, start, len);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (start !== undefined)
|
|
233
|
+
buffer = buffer.slice(start, start + len);
|
|
234
|
+
self.emitBody(buffer);
|
|
235
|
+
return len;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
parser.finish = function finish() {
|
|
239
|
+
// Parser reuse
|
|
240
|
+
if (this.socket !== self.socket) {
|
|
241
|
+
this.execute = oldExecute;
|
|
242
|
+
this.finish = oldFinish;
|
|
243
|
+
return this.finish();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
this.execute = oldExecute;
|
|
247
|
+
this.finish = oldFinish;
|
|
248
|
+
self.emitMessageComplete();
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
Deceiver.prototype.emitBody = function emitBody(buffer) {
|
|
253
|
+
var parser = this.socket.parser;
|
|
254
|
+
assert(parser, 'No parser present');
|
|
255
|
+
|
|
256
|
+
parser[kOnBody](buffer, 0, buffer.length);
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
Deceiver.prototype._emitEmpty = function _emitEmpty() {
|
|
260
|
+
// Emit data to force out handling of UPGRADE
|
|
261
|
+
var empty = new Buffer(0);
|
|
262
|
+
if (this.socket.ondata)
|
|
263
|
+
this.socket.ondata(empty, 0, 0);
|
|
264
|
+
else
|
|
265
|
+
this.socket.emit('data', empty);
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
Deceiver.prototype.emitMessageComplete = function emitMessageComplete() {
|
|
269
|
+
var parser = this.socket.parser;
|
|
270
|
+
assert(parser, 'No parser present');
|
|
271
|
+
|
|
272
|
+
parser[kOnMessageComplete]();
|
|
273
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohos-ports/http-deceiver",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.2.7-beta.1",
|
|
4
|
+
"description": "Deceive HTTP parser",
|
|
5
|
+
"main": "lib/deceiver.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "mocha --reporter=spec test/*-test.js"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/ohos-ports/ohos-ports.git",
|
|
12
|
+
"directory": "ports/http-deceiver/1.2.7"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"http",
|
|
16
|
+
"net",
|
|
17
|
+
"deceive"
|
|
18
|
+
],
|
|
19
|
+
"author": "Fedor Indutny <fedor@indutny.com>",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/ohos-ports/ohos-ports/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/indutny/http-deceiver#readme",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"handle-thing": "^1.0.1",
|
|
27
|
+
"mocha": "^2.2.5",
|
|
28
|
+
"readable-stream": "^2.0.1",
|
|
29
|
+
"stream-pair": "^1.0.0"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/test/api-test.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
var assert = require('assert');
|
|
2
|
+
var net = require('net');
|
|
3
|
+
var http = require('http');
|
|
4
|
+
var streamPair = require('stream-pair');
|
|
5
|
+
var thing = require('handle-thing');
|
|
6
|
+
|
|
7
|
+
var httpDeceiver = require('../');
|
|
8
|
+
|
|
9
|
+
describe('HTTP Deceiver', function() {
|
|
10
|
+
var handle;
|
|
11
|
+
var pair;
|
|
12
|
+
var socket;
|
|
13
|
+
var deceiver;
|
|
14
|
+
|
|
15
|
+
beforeEach(function() {
|
|
16
|
+
pair = streamPair.create();
|
|
17
|
+
handle = thing.create(pair.other);
|
|
18
|
+
socket = new net.Socket({ handle: handle });
|
|
19
|
+
|
|
20
|
+
// For v0.8
|
|
21
|
+
socket.readable = true;
|
|
22
|
+
socket.writable = true;
|
|
23
|
+
|
|
24
|
+
deceiver = httpDeceiver.create(socket);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should emit request', function(done) {
|
|
28
|
+
var server = http.createServer();
|
|
29
|
+
server.emit('connection', socket);
|
|
30
|
+
|
|
31
|
+
server.on('request', function(req, res) {
|
|
32
|
+
assert.equal(req.method, 'PUT');
|
|
33
|
+
assert.equal(req.url, '/hello');
|
|
34
|
+
assert.deepEqual(req.headers, { a: 'b' });
|
|
35
|
+
|
|
36
|
+
done();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
deceiver.emitRequest({
|
|
40
|
+
method: 'PUT',
|
|
41
|
+
path: '/hello',
|
|
42
|
+
headers: {
|
|
43
|
+
a: 'b'
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should emit response', function(done) {
|
|
49
|
+
var agent = new http.Agent();
|
|
50
|
+
agent.createConnection = function createConnection() {
|
|
51
|
+
return socket;
|
|
52
|
+
};
|
|
53
|
+
var client = http.request({
|
|
54
|
+
method: 'POST',
|
|
55
|
+
path: '/ok',
|
|
56
|
+
agent: agent
|
|
57
|
+
}, function(res) {
|
|
58
|
+
assert.equal(res.statusCode, 421);
|
|
59
|
+
assert.deepEqual(res.headers, { a: 'b' });
|
|
60
|
+
|
|
61
|
+
done();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
process.nextTick(function() {
|
|
65
|
+
deceiver.emitResponse({
|
|
66
|
+
status: 421,
|
|
67
|
+
reason: 'F',
|
|
68
|
+
headers: {
|
|
69
|
+
a: 'b'
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should override .execute and .finish', function(done) {
|
|
76
|
+
var server = http.createServer();
|
|
77
|
+
server.emit('connection', socket);
|
|
78
|
+
|
|
79
|
+
server.on('request', function(req, res) {
|
|
80
|
+
assert.equal(req.method, 'PUT');
|
|
81
|
+
assert.equal(req.url, '/hello');
|
|
82
|
+
assert.deepEqual(req.headers, { a: 'b' });
|
|
83
|
+
|
|
84
|
+
var actual = '';
|
|
85
|
+
req.on('data', function(chunk) {
|
|
86
|
+
actual += chunk;
|
|
87
|
+
});
|
|
88
|
+
req.once('end', function() {
|
|
89
|
+
assert.equal(actual, 'hello world');
|
|
90
|
+
done();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
deceiver.emitRequest({
|
|
95
|
+
method: 'PUT',
|
|
96
|
+
path: '/hello',
|
|
97
|
+
headers: {
|
|
98
|
+
a: 'b'
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
pair.write('hello');
|
|
103
|
+
pair.end(' world');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should work with reusing parser', function(done) {
|
|
107
|
+
var server = http.createServer();
|
|
108
|
+
server.emit('connection', socket);
|
|
109
|
+
|
|
110
|
+
function secondRequest() {
|
|
111
|
+
pair = streamPair.create();
|
|
112
|
+
handle = thing.create(pair.other);
|
|
113
|
+
socket = new net.Socket({ handle: handle });
|
|
114
|
+
|
|
115
|
+
// For v0.8
|
|
116
|
+
socket.readable = true;
|
|
117
|
+
socket.writable = true;
|
|
118
|
+
|
|
119
|
+
server.emit('connection', socket);
|
|
120
|
+
|
|
121
|
+
pair.end('PUT /second HTTP/1.1\r\nContent-Length:11\r\n\r\nhello world');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
server.on('request', function(req, res) {
|
|
125
|
+
var actual = '';
|
|
126
|
+
req.on('data', function(chunk) {
|
|
127
|
+
actual += chunk;
|
|
128
|
+
});
|
|
129
|
+
req.once('end', function() {
|
|
130
|
+
assert.equal(actual, 'hello world');
|
|
131
|
+
|
|
132
|
+
if (req.url === '/first')
|
|
133
|
+
secondRequest();
|
|
134
|
+
else
|
|
135
|
+
done();
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
deceiver.emitRequest({
|
|
140
|
+
method: 'PUT',
|
|
141
|
+
path: '/first',
|
|
142
|
+
headers: {
|
|
143
|
+
a: 'b'
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
pair.write('hello');
|
|
148
|
+
pair.end(' world');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('should emit CONNECT request', function(done) {
|
|
152
|
+
var server = http.createServer();
|
|
153
|
+
server.emit('connection', socket);
|
|
154
|
+
|
|
155
|
+
server.on('connect', function(req, socket, bodyHead) {
|
|
156
|
+
assert.equal(req.method, 'CONNECT');
|
|
157
|
+
assert.equal(req.url, '/hello');
|
|
158
|
+
|
|
159
|
+
done();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
deceiver.emitRequest({
|
|
163
|
+
method: 'CONNECT',
|
|
164
|
+
path: '/hello',
|
|
165
|
+
headers: {
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should emit Upgrade request', function(done) {
|
|
171
|
+
var server = http.createServer();
|
|
172
|
+
server.emit('connection', socket);
|
|
173
|
+
|
|
174
|
+
server.on('upgrade', function(req, socket, bodyHead) {
|
|
175
|
+
assert.equal(req.method, 'POST');
|
|
176
|
+
assert.equal(req.url, '/hello');
|
|
177
|
+
|
|
178
|
+
socket.on('data', function(chunk) {
|
|
179
|
+
assert.equal(chunk + '', 'hm');
|
|
180
|
+
done();
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
deceiver.emitRequest({
|
|
185
|
+
method: 'POST',
|
|
186
|
+
path: '/hello',
|
|
187
|
+
headers: {
|
|
188
|
+
'upgrade': 'websocket'
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
pair.write('hm');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should emit Upgrade response', function(done) {
|
|
196
|
+
var agent = new http.Agent();
|
|
197
|
+
agent.createConnection = function createConnection() {
|
|
198
|
+
return socket;
|
|
199
|
+
};
|
|
200
|
+
var client = http.request({
|
|
201
|
+
method: 'POST',
|
|
202
|
+
path: '/ok',
|
|
203
|
+
headers: {
|
|
204
|
+
connection: 'upgrade',
|
|
205
|
+
upgrade: 'websocket'
|
|
206
|
+
},
|
|
207
|
+
agent: agent
|
|
208
|
+
}, function(res) {
|
|
209
|
+
assert(false);
|
|
210
|
+
});
|
|
211
|
+
client.on('upgrade', function(res, socket) {
|
|
212
|
+
assert.equal(res.statusCode, 421);
|
|
213
|
+
done();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
process.nextTick(function() {
|
|
217
|
+
deceiver.emitResponse({
|
|
218
|
+
status: 421,
|
|
219
|
+
reason: 'F',
|
|
220
|
+
headers: {
|
|
221
|
+
upgrade: 'websocket'
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
});
|