-
Notifications
You must be signed in to change notification settings - Fork 29.7k
/
test-http2-https-fallback-http-server-options.js
89 lines (76 loc) Β· 2.37 KB
/
test-http2-https-fallback-http-server-options.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
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const url = require('url');
const tls = require('tls');
const http2 = require('http2');
const https = require('https');
const http = require('http');
const key = fixtures.readKey('agent8-key.pem');
const cert = fixtures.readKey('agent8-cert.pem');
const ca = fixtures.readKey('fake-startcom-root-cert.pem');
function onRequest(request, response) {
const { socket: { alpnProtocol } } = request.httpVersion === '2.0' ?
request.stream.session : request;
response.status(200);
response.end(JSON.stringify({
alpnProtocol,
httpVersion: request.httpVersion,
userAgent: request.getUserAgent()
}));
}
class MyIncomingMessage extends http.IncomingMessage {
getUserAgent() {
return this.headers['user-agent'] || 'unknown';
}
}
class MyServerResponse extends http.ServerResponse {
status(code) {
return this.writeHead(code, { 'Content-Type': 'application/json' });
}
}
// HTTP/2 & HTTP/1.1 server
{
const server = http2.createSecureServer(
{
cert,
key, allowHTTP1: true,
Http1IncomingMessage: MyIncomingMessage,
Http1ServerResponse: MyServerResponse
},
common.mustCall(onRequest, 1)
);
server.listen(0);
server.on('listening', common.mustCall(() => {
const { port } = server.address();
const origin = `https://localhost:${port}`;
// HTTP/1.1 client
https.get(
Object.assign(url.parse(origin), {
secureContext: tls.createSecureContext({ ca }),
headers: { 'User-Agent': 'node-test' }
}),
common.mustCall((response) => {
assert.strictEqual(response.statusCode, 200);
assert.strictEqual(response.statusMessage, 'OK');
assert.strictEqual(
response.headers['content-type'],
'application/json'
);
response.setEncoding('utf8');
let raw = '';
response.on('data', (chunk) => { raw += chunk; });
response.on('end', common.mustCall(() => {
const { alpnProtocol, httpVersion, userAgent } = JSON.parse(raw);
assert.strictEqual(alpnProtocol, false);
assert.strictEqual(httpVersion, '1.1');
assert.strictEqual(userAgent, 'node-test');
server.close();
}));
})
);
}));
}