forked from http-party/node-http-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib-http-proxy-common-test.js
241 lines (216 loc) · 6.77 KB
/
lib-http-proxy-common-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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
var common = require('../lib/http-proxy/common'),
expect = require('expect.js');
describe('lib/http-proxy/common.js', function () {
describe('#setupOutgoing', function () {
it('should setup the correct headers', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
agent : '?',
target: {
host : 'hey',
hostname : 'how',
socketPath: 'are',
port : 'you',
},
headers: {'fizz': 'bang', 'overwritten':true},
localAddress: 'local.address',
},
{
method : 'i',
url : 'am',
headers : {'pro':'xy','overwritten':false}
});
expect(outgoing.host).to.eql('hey');
expect(outgoing.hostname).to.eql('how');
expect(outgoing.socketPath).to.eql('are');
expect(outgoing.port).to.eql('you');
expect(outgoing.agent).to.eql('?');
expect(outgoing.method).to.eql('i');
expect(outgoing.path).to.eql('am');
expect(outgoing.headers.pro).to.eql('xy');
expect(outgoing.headers.fizz).to.eql('bang');
expect(outgoing.headers.overwritten).to.eql(true);
expect(outgoing.localAddress).to.eql('local.address');
});
it('should not override agentless upgrade header', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
agent: undefined,
target: {
host : 'hey',
hostname : 'how',
socketPath: 'are',
port : 'you',
},
headers: {'connection': 'upgrade'},
},
{
method : 'i',
url : 'am',
headers : {'pro':'xy','overwritten':false}
});
expect(outgoing.headers.connection).to.eql('upgrade');
});
it('should not override agentless connection: contains upgrade', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
agent: undefined,
target: {
host : 'hey',
hostname : 'how',
socketPath: 'are',
port : 'you',
},
headers: {'connection': 'keep-alive, upgrade'}, // this is what Firefox sets
},
{
method : 'i',
url : 'am',
headers : {'pro':'xy','overwritten':false}
});
expect(outgoing.headers.connection).to.eql('keep-alive, upgrade');
});
it('should override agentless connection: contains improper upgrade', function () {
// sanity check on upgrade regex
var outgoing = {};
common.setupOutgoing(outgoing,
{
agent: undefined,
target: {
host : 'hey',
hostname : 'how',
socketPath: 'are',
port : 'you',
},
headers: {'connection': 'keep-alive, not upgrade'},
},
{
method : 'i',
url : 'am',
headers : {'pro':'xy','overwritten':false}
});
expect(outgoing.headers.connection).to.eql('close');
});
it('should override agentless non-upgrade header to close', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
agent: undefined,
target: {
host : 'hey',
hostname : 'how',
socketPath: 'are',
port : 'you',
},
headers: {'connection': 'xyz'},
},
{
method : 'i',
url : 'am',
headers : {'pro':'xy','overwritten':false}
});
expect(outgoing.headers.connection).to.eql('close');
});
it('should set the agent to false if none is given', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {target:
'http://localhost'
}, { url: '/' });
expect(outgoing.agent).to.eql(false);
});
it('set the port according to the protocol', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
agent : '?',
target: {
host : 'how',
hostname : 'are',
socketPath: 'you',
protocol: 'https:'
}
},
{
method : 'i',
url : 'am',
headers : 'proxy'
});
expect(outgoing.host).to.eql('how');
expect(outgoing.hostname).to.eql('are');
expect(outgoing.socketPath).to.eql('you');
expect(outgoing.agent).to.eql('?');
expect(outgoing.method).to.eql('i');
expect(outgoing.path).to.eql('am');
expect(outgoing.headers).to.eql('proxy')
expect(outgoing.port).to.eql(443);
});
it('should keep the original target path in the outgoing path', function(){
var outgoing = {};
common.setupOutgoing(outgoing, {target:
{ path: 'some-path' }
}, { url : 'am' });
expect(outgoing.path).to.eql('some-path/am');
});
it('should keep the original forward path in the outgoing path', function(){
var outgoing = {};
common.setupOutgoing(outgoing, {
target: {},
forward: {
path: 'some-path'
}
}, {
url : 'am'
}, 'forward');
expect(outgoing.path).to.eql('some-path/am');
});
it('should not prepend the target path to the outgoing path with prependPath = false', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {
target: { path: 'hellothere' },
prependPath: false
}, { url: 'hi' });
expect(outgoing.path).to.eql('hi');
})
it('should properly join paths', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {
target: { path: '/forward' },
}, { url: '/static/path' });
expect(outgoing.path).to.eql('/forward/static/path');
})
it('should not modify the query string', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {
target: { path: '/forward' },
}, { url: '/?foo=bar//&target=http://foobar.com/' });
expect(outgoing.path).to.eql('/forward/?foo=bar//&target=http://foobar.com/');
})
});
describe('#setupSocket', function () {
it('should setup a socket', function () {
var socketConfig = {
timeout: null,
nodelay: false,
keepalive: false
},
stubSocket = {
setTimeout: function (num) {
socketConfig.timeout = num;
},
setNoDelay: function (bol) {
socketConfig.nodelay = bol;
},
setKeepAlive: function (bol) {
socketConfig.keepalive = bol;
}
}
returnValue = common.setupSocket(stubSocket);
expect(socketConfig.timeout).to.eql(0);
expect(socketConfig.nodelay).to.eql(true);
expect(socketConfig.keepalive).to.eql(true);
});
});
});