-
Notifications
You must be signed in to change notification settings - Fork 982
/
Copy pathserverHttp2.test.js
111 lines (95 loc) · 2.6 KB
/
serverHttp2.test.js
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
'use strict';
/* eslint-disable func-names */
var path = require('path');
var fs = require('fs');
var http2;
// http2 module is not available < v8.4.0 (only with flag <= 8.8.0)
try {
http2 = require('http2');
} catch (err) {
console.log('HTTP2 module is not available');
console.log(
'Node.js version >= v8.8.8 required, current: ' + process.versions.node
);
return;
}
var restify = require('../lib');
if (require.cache[__dirname + '/lib/helper.js']) {
delete require.cache[__dirname + '/lib/helper.js'];
}
var helper = require('./lib/helper.js');
///--- Globals
var after = helper.after;
var before = helper.before;
var test = helper.test;
var CERT = fs.readFileSync(path.join(__dirname, './keys/http2-cert.pem'));
var KEY = fs.readFileSync(path.join(__dirname, './keys/http2-key.pem'));
var CA = fs.readFileSync(path.join(__dirname, 'keys/http2-csr.pem'));
var PORT = process.env.UNIT_TEST_PORT || 0;
var CLIENT;
var SERVER;
///--- Tests
before(function(cb) {
try {
SERVER = restify.createServer({
dtrace: helper.dtrace,
handleUncaughtExceptions: true,
http2: {
cert: CERT,
key: KEY,
ca: CA
},
log: helper.getLog('server')
});
SERVER.listen(PORT, '127.0.0.1', function() {
PORT = SERVER.address().port;
CLIENT = http2.connect('https://127.0.0.1:' + PORT, {
rejectUnauthorized: false
});
cb();
});
} catch (e) {
console.error(e.stack);
process.exit(1);
}
});
after(function(cb) {
try {
CLIENT.destroy();
SERVER.close(function() {
CLIENT = null;
SERVER = null;
cb();
});
} catch (e) {
console.error(e.stack);
process.exit(1);
}
});
test('get (path only)', function(t) {
SERVER.get('/foo/:id', function echoId(req, res, next) {
t.ok(req.params);
t.equal(req.params.id, 'bar');
t.equal(req.isUpload(), false);
res.json({ hello: 'world' });
next();
});
var req = CLIENT.request({
':path': '/foo/bar',
':method': 'GET'
});
req.on('response', function(headers, flags) {
var data = '';
t.equal(headers[':status'], 200);
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
t.deepEqual(JSON.parse(data), { hello: 'world' });
t.end();
});
});
req.on('error', function(err) {
t.ifError(err);
});
});