-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Http2.test.js
120 lines (105 loc) · 2.99 KB
/
Http2.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
112
113
114
115
116
117
118
119
120
'use strict';
const path = require('path');
const request = require('supertest');
const semver = require('semver');
const helper = require('./helper');
const config = require('./fixtures/contentbase-config/webpack.config');
const contentBasePublic = path.join(
__dirname,
'fixtures/contentbase-config/public'
);
describe('http2', () => {
let server;
let req;
// HTTP/2 will only work with node versions below 10.0.0
// since spdy is broken past that point, and this test will only
// work above Node 8.8.0, since it is the first version where the
// built-in http2 module is exposed without need for a flag
// (https://nodejs.org/en/blog/release/v8.8.0/)
// if someone is testing below this Node version and breaks this,
// their tests will not catch it, but CI will catch it.
if (
semver.gte(process.version, '8.8.0') &&
semver.lt(process.version, '10.0.0')
) {
/* eslint-disable global-require */
const http2 = require('http2');
/* eslint-enable global-require */
describe('http2 works with https', () => {
beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
https: true,
http2: true,
},
done
);
req = request(server.app);
});
it('confirm http2 client can connect', (done) => {
const client = http2.connect('https://localhost:8080', {
rejectUnauthorized: false,
});
client.on('error', (err) => console.error(err));
const http2Req = client.request({ ':path': '/' });
http2Req.on('response', (headers) => {
expect(headers[':status']).toEqual(200);
});
http2Req.setEncoding('utf8');
let data = '';
http2Req.on('data', (chunk) => {
data += chunk;
});
http2Req.on('end', () => {
expect(data).toEqual(expect.stringMatching(/Heyo/));
done();
});
http2Req.end();
});
afterAll(helper.close);
});
}
describe('server works with http2 option, but without https option', () => {
beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
http2: true,
},
done
);
req = request(server.app);
});
it('Request to index', (done) => {
req.get('/').expect(200, /Heyo/, done);
});
afterAll(helper.close);
});
describe('https without http2 disables HTTP/2', () => {
beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
https: true,
http2: false,
},
done
);
req = request(server.app);
});
it('Request to index', (done) => {
req
.get('/')
.expect(200, /Heyo/)
.then(({ res }) => {
expect(res.httpVersion).not.toEqual('2.0');
done();
});
});
afterAll(helper.close);
});
});