proxy_spec.js
4.86 KB
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
var helpers = require('./helpers'),
should = require('should'),
sinon = require('sinon'),
http = require('http'),
needle = require('./../');
var port = 7707;
var url = 'localhost:' + port;
var nonexisting_host = 'awepfokawepofawe.com';
describe('proxy option', function() {
var spy, opts;
function send_request(opts, done) {
if (spy) spy.restore();
spy = sinon.spy(http, 'request');
needle.get(url, opts, done);
}
//////////////////////
// proxy opts helpers
function not_proxied(done) {
return function(err, resp) {
var path = spy.args[0][0].path;
path.should.eql('/'); // not the full original URI
spy.restore();
done();
}
}
function proxied(host, port, done) {
return function(err, resp) {
var path = spy.args[0][0].path;
path.should.eql('http://' + url); // the full original URI
var http_host = spy.args[0][0].host;
if (http_host) http_host.should.eql(host);
var http_port = spy.args[0][0].port;
if (http_port) http_port.should.eql(port);
spy.restore();
done();
}
}
//////////////////////
// auth helpers
function get_auth(header) {
var token = header.split(/\s+/).pop();
return token && Buffer.from(token, 'base64').toString().split(':');
}
function no_proxy_auth(done) {
return function(err, resp) {
var headers = spy.args[0][0].headers;
Object.keys(headers).should.not.containEql('proxy-authorization');
done();
}
}
function header_set(name, user, pass, done) {
return function(err, resp) {
var headers = spy.args[0][0].headers;
var auth = get_auth(headers[name]);
auth[0].should.eql(user);
auth[1].should.eql(pass);
done();
}
}
function proxy_auth_set(user, pass, done) {
return header_set('proxy-authorization', user, pass, done);
}
function basic_auth_set(user, pass, done) {
return header_set('authorization', user, pass, done);
}
after(function() {
spy.restore();
})
describe('when null proxy is passed', function() {
it('does not proxy', function(done) {
send_request({ proxy: null }, not_proxied(done))
})
describe('but defaults has been set', function() {
before(function() {
needle.defaults({ proxy: 'foobar' });
})
after(function() {
needle.defaults({ proxy: null });
})
it('tries to proxy anyway', function(done) {
send_request({}, proxied('foobar', 80, done))
})
})
})
describe('when weird string is passed', function() {
it('tries to proxy anyway', function(done) {
send_request({ proxy: 'alfalfa' }, proxied('alfalfa', 80, done))
})
})
describe('when valid url is passed', function() {
it('proxies request', function(done) {
send_request({ proxy: nonexisting_host + ':123/done' }, proxied(nonexisting_host, '123', done))
})
it('does not set a Proxy-Authorization header', function(done) {
send_request({ proxy: nonexisting_host + ':123/done' }, no_proxy_auth(done));
})
describe('and proxy url contains user:pass', function() {
before(function() {
opts = {
proxy: 'http://mj:x@' + nonexisting_host + ':123/done'
}
})
it('proxies request', function(done) {
send_request(opts, proxied(nonexisting_host, '123', done))
})
it('sets Proxy-Authorization header', function(done) {
send_request(opts, proxy_auth_set('mj', 'x', done));
})
})
describe('and a proxy_user is passed', function() {
before(function() {
opts = {
proxy: nonexisting_host + ':123',
proxy_user: 'someone',
proxy_pass: 'else'
}
})
it('proxies request', function(done) {
send_request(opts, proxied(nonexisting_host, '123', done))
})
it('sets Proxy-Authorization header', function(done) {
send_request(opts, proxy_auth_set('someone', 'else', done));
})
describe('and url also contains user:pass', function() {
it('url user:pass wins', function(done) {
var opts = {
proxy: 'http://xxx:yyy@' + nonexisting_host + ':123',
proxy_user: 'someone',
proxy_pass: 'else'
}
send_request(opts, proxy_auth_set('xxx', 'yyy', done));
})
})
describe('and options.username is also present', function() {
before(function() {
opts = { proxy_user: 'foobar', username: 'someone' };
})
it('a separate Authorization header is set', function(done) {
var opts = {
proxy: nonexisting_host + ':123',
proxy_user: 'someone',
proxy_pass: 'else',
username: 'test',
password: 'X'
}
send_request(opts, basic_auth_set('test', 'X', done));
})
})
})
})
})