source: trip-planner-front/node_modules/needle/test/proxy_spec.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.9 KB
Line 
1var helpers = require('./helpers'),
2 should = require('should'),
3 sinon = require('sinon'),
4 http = require('http'),
5 needle = require('./../');
6
7var port = 7707;
8var url = 'localhost:' + port;
9var nonexisting_host = 'awepfokawepofawe.com';
10
11describe('proxy option', function() {
12
13 var spy, opts;
14
15 function send_request(opts, done) {
16 if (spy) spy.restore();
17 spy = sinon.spy(http, 'request');
18 needle.get(url, opts, done);
19 }
20
21 //////////////////////
22 // proxy opts helpers
23
24 function not_proxied(done) {
25 return function(err, resp) {
26 var path = spy.args[0][0].path;
27 path.should.eql('/'); // not the full original URI
28 spy.restore();
29 done();
30 }
31 }
32
33 function proxied(host, port, done) {
34 return function(err, resp) {
35 var path = spy.args[0][0].path;
36 path.should.eql('http://' + url); // the full original URI
37
38 var http_host = spy.args[0][0].host;
39 if (http_host) http_host.should.eql(host);
40
41 var http_port = spy.args[0][0].port;
42 if (http_port) http_port.should.eql(port);
43
44 spy.restore();
45 done();
46 }
47 }
48
49 //////////////////////
50 // auth helpers
51
52 function get_auth(header) {
53 var token = header.split(/\s+/).pop();
54 return token && Buffer.from(token, 'base64').toString().split(':');
55 }
56
57 function no_proxy_auth(done) {
58 return function(err, resp) {
59 var headers = spy.args[0][0].headers;
60 Object.keys(headers).should.not.containEql('proxy-authorization');
61 done();
62 }
63 }
64
65 function header_set(name, user, pass, done) {
66 return function(err, resp) {
67 var headers = spy.args[0][0].headers;
68 var auth = get_auth(headers[name]);
69 auth[0].should.eql(user);
70 auth[1].should.eql(pass);
71 done();
72 }
73 }
74
75 function proxy_auth_set(user, pass, done) {
76 return header_set('proxy-authorization', user, pass, done);
77 }
78
79 function basic_auth_set(user, pass, done) {
80 return header_set('authorization', user, pass, done);
81 }
82
83 after(function() {
84 spy.restore();
85 })
86
87 describe('when null proxy is passed', function() {
88
89 it('does not proxy', function(done) {
90 send_request({ proxy: null }, not_proxied(done))
91 })
92
93 describe('but defaults has been set', function() {
94
95 before(function() {
96 needle.defaults({ proxy: 'foobar' });
97 })
98
99 after(function() {
100 needle.defaults({ proxy: null });
101 })
102
103 it('tries to proxy anyway', function(done) {
104 send_request({}, proxied('foobar', 80, done))
105 })
106
107 })
108
109 })
110
111 describe('when weird string is passed', function() {
112
113 it('tries to proxy anyway', function(done) {
114 send_request({ proxy: 'alfalfa' }, proxied('alfalfa', 80, done))
115 })
116 })
117
118 describe('when valid url is passed', function() {
119
120 it('proxies request', function(done) {
121 send_request({ proxy: nonexisting_host + ':123/done' }, proxied(nonexisting_host, '123', done))
122 })
123
124 it('does not set a Proxy-Authorization header', function(done) {
125 send_request({ proxy: nonexisting_host + ':123/done' }, no_proxy_auth(done));
126 })
127
128 describe('and proxy url contains user:pass', function() {
129
130 before(function() {
131 opts = {
132 proxy: 'http://mj:x@' + nonexisting_host + ':123/done'
133 }
134 })
135
136 it('proxies request', function(done) {
137 send_request(opts, proxied(nonexisting_host, '123', done))
138 })
139
140 it('sets Proxy-Authorization header', function(done) {
141 send_request(opts, proxy_auth_set('mj', 'x', done));
142 })
143
144 })
145
146 describe('and a proxy_user is passed', function() {
147
148 before(function() {
149 opts = {
150 proxy: nonexisting_host + ':123',
151 proxy_user: 'someone',
152 proxy_pass: 'else'
153 }
154 })
155
156 it('proxies request', function(done) {
157 send_request(opts, proxied(nonexisting_host, '123', done))
158 })
159
160 it('sets Proxy-Authorization header', function(done) {
161 send_request(opts, proxy_auth_set('someone', 'else', done));
162 })
163
164 describe('and url also contains user:pass', function() {
165
166 it('url user:pass wins', function(done) {
167 var opts = {
168 proxy: 'http://xxx:yyy@' + nonexisting_host + ':123',
169 proxy_user: 'someone',
170 proxy_pass: 'else'
171 }
172
173 send_request(opts, proxy_auth_set('xxx', 'yyy', done));
174 })
175
176 })
177
178 describe('and options.username is also present', function() {
179
180 before(function() {
181 opts = { proxy_user: 'foobar', username: 'someone' };
182 })
183
184 it('a separate Authorization header is set', function(done) {
185 var opts = {
186 proxy: nonexisting_host + ':123',
187 proxy_user: 'someone',
188 proxy_pass: 'else',
189 username: 'test',
190 password: 'X'
191 }
192
193 send_request(opts, basic_auth_set('test', 'X', done));
194 })
195
196 })
197
198 })
199
200 })
201
202})
Note: See TracBrowser for help on using the repository browser.