-
Notifications
You must be signed in to change notification settings - Fork 156
/
http2-client-test.js
175 lines (136 loc) · 5.9 KB
/
http2-client-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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const fs = require('fs');
const { HTTP2_HEADER_STATUS } = require('http2').constants;
const { expect } = require('chai');
const request = require('request-promise-native');
const logger = require('../../../lib/utils/logger');
const { noop } = require('lodash');
const http2Utils = require('../../../lib/request-pipeline/destination-request/http2');
const { CROSS_DOMAIN_SERVER_PORT } = require('../common/constants');
const {
createSession,
createProxy,
getBasicProxyUrl,
createDestinationServer,
createHttp2DestServer,
compareCode
} = require('../common/utils');
describe('https proxy', () => {
let session = null;
let proxy = null;
let http2Server = null;
let httpsServer = null;
let logs = null;
function getProxyUrl (url, resourceType) {
return getBasicProxyUrl(url, resourceType, null, null, false, session);
}
function overrideLoggerFn (loggerObj, fnName) {
const loggerFn = loggerObj[fnName];
loggerObj[fnName] = (_, ...args) => logs.push(fnName, args);
loggerObj[fnName].native = loggerFn;
}
function restoreLoggerFn (loggerObj, fnName) {
loggerObj[fnName] = loggerObj[fnName].native;
}
before(() => {
const crossDomainDestinationServer = createDestinationServer(CROSS_DOMAIN_SERVER_PORT, true);
const { app: httpsApp } = crossDomainDestinationServer;
httpsServer = crossDomainDestinationServer.server;
httpsApp.get('/stylesheet', (_req, res) => res
.status(200)
.set('content-type', 'text/css')
.end(fs.readFileSync('test/server/data/stylesheet/src.css')));
const http2App = createHttp2DestServer();
http2Server = http2App.server;
http2App.get('/script', stream => {
stream.respond({
[HTTP2_HEADER_STATUS]: 200,
'content-type': 'application/javascript'
});
stream.end(fs.readFileSync('test/server/data/script/src.js'));
});
overrideLoggerFn(logger.destination, 'onHttp2Stream');
overrideLoggerFn(logger.destination, 'onHttp2SessionCreated');
overrideLoggerFn(logger.destination, 'onHttp2Unsupported');
});
after(() => {
restoreLoggerFn(logger.destination, 'onHttp2Stream');
restoreLoggerFn(logger.destination, 'onHttp2SessionCreated');
restoreLoggerFn(logger.destination, 'onHttp2Unsupported');
http2Server.close();
http2Utils.clearSessionsCache();
httpsServer.close();
});
beforeEach(() => {
session = createSession();
proxy = createProxy();
logs = [];
});
afterEach(() => proxy.close());
it('Should send request through http2', () => {
const proxyUrl = getProxyUrl('https://127.0.0.1:2000/script', { isScript: true });
proxy.openSession('https://127.0.0.1:2000', session);
return request(proxyUrl)
.then(body => {
const expected = fs.readFileSync('test/server/data/script/expected.js').toString();
expect(body).eql(expected);
expect(logs.length).eql(4);
expect(logs[0]).eql('onHttp2SessionCreated');
expect(logs[1]).deep.eql(['https://127.0.0.1:2000', 1, 100]);
expect(logs[2]).eql('onHttp2Stream');
expect(logs[3][0]).deep.eql({
':method': 'GET',
':path': '/script',
':authority': '127.0.0.1:2000'
});
});
});
it('Should send request through https', () => {
session.id = 'sessionId';
const proxyUrl = getProxyUrl('https://127.0.0.1:2002/stylesheet');
proxy.openSession('https://127.0.0.1:2000', session);
return request(proxyUrl)
.then(body => {
const expected = fs.readFileSync('test/server/data/stylesheet/expected.css').toString();
compareCode(body, expected);
expect(logs.length).eql(2);
expect(logs[0]).eql('onHttp2Unsupported');
expect(logs[1][0]).eql('https://127.0.0.1:2002');
});
});
// https://github.com/nodejs/node/issues/37849
it('Should resend a request through https if http2 stream is emitted a protocol error', () => {
const sessionMock = {
destroyCalled: false,
request: () => ({
setTimeout: noop,
write: noop,
end: function () {
setImmediate(() => this._errorHandler({
code: 'ERR_HTTP2_STREAM_ERROR',
message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR'
}));
},
on: function (eventName, fn) {
if (eventName === 'error')
this._errorHandler = fn;
}
}),
destroy: function () {
this.destroyCalled = true;
}
};
session.id = 'sessionId';
const storedGetHttp2Session = http2Utils.getHttp2Session;
const proxyUrl = getProxyUrl('https://127.0.0.1:2002/stylesheet');
http2Utils.getHttp2Session = () => sessionMock;
proxy.openSession('https://127.0.0.1:2000/', session);
return request(proxyUrl, { form: { key: 'value' } })
.then(body => {
http2Utils.getHttp2Session = storedGetHttp2Session;
const expected = fs.readFileSync('test/server/data/stylesheet/expected.css').toString();
compareCode(body, expected);
expect(logs[0]).eql('onHttp2Stream');
expect(sessionMock.destroyCalled).to.be.true;
});
});
});