Skip to content

Commit b1c9d5b

Browse files
committed
refactor GET authorization to header-based
proposed bug fix for #31
1 parent e379f93 commit b1c9d5b

File tree

9 files changed

+173
-78
lines changed

9 files changed

+173
-78
lines changed

lib/Uber.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Uber.prototype.modifierMethodHelper = function modifierMethodHelper(options, cal
139139
if (options && options.server_token) {
140140
access_type = 'Token ' + this.defaults.server_token;
141141
} else {
142-
if(!this.access_token) {
142+
if (!this.access_token) {
143143
return callback(new Error('Invalid access token'), 'A valid access token is required for this request');
144144
} else {
145145
// defaults to OAuth with access_token
@@ -172,34 +172,44 @@ Uber.prototype.modifierMethodHelper = function modifierMethodHelper(options, cal
172172
return this;
173173
};
174174

175-
Uber.prototype.get = function get(options, callback) {
176-
var url = this.getRequestURL(options.version, options.url+'?');
175+
Uber.prototype.createAccessHeader = function createAccessHeader(server_token) {
176+
var access_type;
177177

178-
if (!this.access_token) {
179-
if(options && options.server_token) {
180-
url += 'server_token=' + this.defaults.server_token;
181-
} else {
182-
return callback(new Error('Invalid access token'), 'A valid access token is required for this request');
183-
}
178+
if (server_token) {
179+
access_type = 'Token ' + this.defaults.server_token;
184180
} else {
185-
url += qs.stringify({ access_token: this.access_token});
181+
if (this.access_token) {
182+
access_type = 'Bearer ' + this.access_token;
183+
}
186184
}
187185

186+
return access_type;
187+
};
188+
189+
Uber.prototype.get = function get(options, callback) {
190+
var access_type = this.createAccessHeader(options.server_token);
191+
if (!access_type) {
192+
return callback(new Error('Invalid access token'), 'A valid access token is required for this request');
193+
}
194+
var url = this.getRequestURL(options.version, options.url);
195+
196+
188197
// add all further option params
189-
if(options.params) {
190-
url += '&' + qs.stringify(options.params);
198+
if (options.params) {
199+
url += '?' + qs.stringify(options.params);
191200
}
192201

193202
request.get({
194203
url: url,
195204
json: true,
196205
headers: {
197206
'Content-Type': 'application/json',
207+
'Authorization': access_type,
198208
'Accept-Language': this.defaults.language
199209
}
200210
}, function(err, data, res) {
201211
if (err || data.statusCode >= 400) {
202-
return callback(err, data);
212+
return callback((err ? err : data), res);
203213
} else {
204214
return callback(null, res);
205215
}
@@ -222,5 +232,5 @@ Uber.prototype.getRequestURL = function getRequestURL(version, url) {
222232
};
223233

224234
Uber.prototype.deprecateMethod = function deprecateMethod(f, oldMethod, newMethod) {
225-
return util.deprecate(f, '`'+ oldMethod +'` is deprecated. Please use `'+ newMethod +'` instead.');
235+
return util.deprecate(f, '`' + oldMethod + '` is deprecated. Please use `' + newMethod + '` instead.');
226236
};

test/deprecated.js

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,12 @@ describe('Products Resource', function() {
150150
};
151151

152152
before(function() {
153-
nock('https://api.uber.com')
154-
.get('/v1/products?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&latitude=3.1357&longitude=101.688')
153+
nock('https://api.uber.com', {
154+
reqheaders: {
155+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
156+
}
157+
})
158+
.get('/v1/products?latitude=3.1357&longitude=101.688')
155159
.reply(200, productReply);
156160
});
157161

@@ -209,8 +213,12 @@ describe('Payment Resource', function() {
209213
.post('/oauth/token')
210214
.times(3)
211215
.reply(200, tokenResponse);
212-
nock('https://api.uber.com')
213-
.get('/v1/payment-methods?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
216+
nock('https://api.uber.com', {
217+
reqheaders: {
218+
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
219+
}
220+
})
221+
.get('/v1/payment-methods')
214222
.reply(200, paymentMethodsReply);
215223
});
216224

@@ -265,8 +273,12 @@ describe('Places Resource', function() {
265273
.times(3)
266274
.reply(200, tokenResponse);
267275

268-
nock('https://api.uber.com')
269-
.get('/v1/places/home?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
276+
nock('https://api.uber.com', {
277+
reqheaders: {
278+
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
279+
}
280+
})
281+
.get('/v1/places/home')
270282
.reply(200, placesHomeReply);
271283
});
272284

@@ -307,8 +319,12 @@ describe('Places Resource', function() {
307319
.times(3)
308320
.reply(200, tokenResponse);
309321

310-
nock('https://api.uber.com')
311-
.get('/v1/places/work?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
322+
nock('https://api.uber.com', {
323+
reqheaders: {
324+
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
325+
}
326+
})
327+
.get('/v1/places/work')
312328
.reply(200, placesWorkReply);
313329
});
314330

@@ -401,9 +417,12 @@ describe('Estimates Resource', function() {
401417

402418
describe('Price Estimates', function() {
403419
before(function() {
404-
nock('https://api.uber.com')
405-
.get('/v1/estimates/price?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&' +
406-
'start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
420+
nock('https://api.uber.com', {
421+
reqheaders: {
422+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
423+
}
424+
})
425+
.get('/v1/estimates/price?start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
407426
.reply(200, priceReply);
408427
});
409428

@@ -430,9 +449,12 @@ describe('Estimates Resource', function() {
430449

431450
describe('Time Estimates', function() {
432451
before(function() {
433-
nock('https://api.uber.com')
434-
.get('/v1/estimates/time?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&' +
435-
'start_latitude=3.1357&start_longitude=101.688')
452+
nock('https://api.uber.com', {
453+
reqheaders: {
454+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
455+
}
456+
})
457+
.get('/v1/estimates/time?start_latitude=3.1357&start_longitude=101.688')
436458
.reply(200, timeReply);
437459
});
438460

@@ -497,8 +519,12 @@ describe('User Resource', function() {
497519
.post('/oauth/token')
498520
.reply(200, tokenResponse);
499521

500-
nock('https://api.uber.com')
501-
.get('/v1/me?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
522+
nock('https://api.uber.com', {
523+
reqheaders: {
524+
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
525+
}
526+
})
527+
.get('/v1/me')
502528
.times(2)
503529
.reply(200, profileReply);
504530
});
@@ -533,9 +559,13 @@ describe('User Resource', function() {
533559
.times(3)
534560
.reply(200, tokenResponse);
535561

536-
nock('https://api.uber.com')
562+
nock('https://api.uber.com', {
563+
reqheaders: {
564+
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
565+
}
566+
})
537567
.get(function(uri) {
538-
var parts = uri.split('/v1.2/history?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL&offset=0&limit=');
568+
var parts = uri.split('/v1.2/history?offset=0&limit=');
539569
if (parts.length !== 2) {
540570
return false;
541571
}

test/estimates.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,19 @@ var tokenResponse = {
6767

6868
describe('Price', function() {
6969
before(function() {
70-
nock('https://api.uber.com')
71-
.get('/v1/estimates/price?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&' +
72-
'start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
70+
nock('https://api.uber.com', {
71+
reqheaders: {
72+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
73+
}
74+
})
75+
.get('/v1/estimates/price?start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
7376
.reply(200, priceReply);
74-
nock('https://api.uber.com')
75-
.get('/v1/estimates/price?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL&' +
76-
'start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
77+
nock('https://api.uber.com', {
78+
reqheaders: {
79+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
80+
}
81+
})
82+
.get('/v1/estimates/price?start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
7783
.reply(200, priceReply);
7884
});
7985

@@ -124,18 +130,15 @@ describe('Time', function() {
124130
.post('/oauth/token')
125131
.times(3)
126132
.reply(200, tokenResponse);
127-
nock('https://api.uber.com')
128-
.get(function(uri) {
129-
return uri.indexOf('v1/estimates/time?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&' +
130-
'start_latitude=3.1357&start_longitude=101.688') >= 0;
133+
nock('https://api.uber.com', {
134+
reqheaders: {
135+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
136+
}
131137
})
132-
.times(3)
133-
.reply(200, timeReply);
134-
nock('https://api.uber.com')
135138
.get(function(uri) {
136-
return uri.indexOf('v1/estimates/time?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL&' +
137-
'start_latitude=3.1357&start_longitude=101.688') >= 0;
139+
return uri.indexOf('v1/estimates/time?start_latitude=3.1357&start_longitude=101.688') >= 0;
138140
})
141+
.times(4)
139142
.reply(200, timeReply);
140143
});
141144

test/payment-methods.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ before(function() {
3737
.times(3)
3838
.reply(200, tokenResponse);
3939

40-
nock('https://api.uber.com')
41-
.get('/v1/payment-methods?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
40+
nock('https://api.uber.com', {
41+
reqheaders: {
42+
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
43+
}
44+
})
45+
.get('/v1/payment-methods')
4246
.reply(200, paymentMethodsReply);
4347
});
4448

test/places.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ describe('Home', function() {
2323
.post('/oauth/token')
2424
.times(3)
2525
.reply(200, tokenResponse);
26-
nock('https://api.uber.com')
27-
.get('/v1/places/home?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
26+
nock('https://api.uber.com', {
27+
reqheaders: {
28+
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
29+
}
30+
})
31+
.get('/v1/places/home')
2832
.reply(200, placesHomeReply);
2933
nock('https://api.uber.com')
3034
.put('/v1/places/home')
@@ -70,8 +74,12 @@ describe('Work', function() {
7074
.post('/oauth/token')
7175
.times(3)
7276
.reply(200, tokenResponse);
73-
nock('https://api.uber.com')
74-
.get('/v1/places/work?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
77+
nock('https://api.uber.com', {
78+
reqheaders: {
79+
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
80+
}
81+
})
82+
.get('/v1/places/work')
7583
.reply(200, placesWorkReply);
7684
});
7785

@@ -113,8 +121,12 @@ describe('By Place ID', function() {
113121
nock('https://api.uber.com')
114122
.put('/v1/places/shop')
115123
.reply(404);
116-
nock('https://api.uber.com')
117-
.get('/v1/places/shop?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
124+
nock('https://api.uber.com', {
125+
reqheaders: {
126+
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
127+
}
128+
})
129+
.get('/v1/places/shop')
118130
.reply(404);
119131
});
120132

test/products.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ var uberBLACKReply = {
5151

5252
describe('List', function() {
5353
before(function() {
54-
nock('https://api.uber.com')
55-
.get('/v1/products?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&latitude=3.1357&longitude=101.688')
54+
nock('https://api.uber.com', {
55+
reqheaders: {
56+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
57+
}
58+
})
59+
.get('/v1/products?latitude=3.1357&longitude=101.688')
5660
.reply(200, productReply);
5761
});
5862

@@ -75,8 +79,12 @@ describe('List', function() {
7579

7680
describe('Details', function() {
7781
before(function() {
78-
nock('https://api.uber.com')
79-
.get('/v1/products/d4abaae7-f4d6-4152-91cc-77523e8165a4?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT')
82+
nock('https://api.uber.com', {
83+
reqheaders: {
84+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
85+
}
86+
})
87+
.get('/v1/products/d4abaae7-f4d6-4152-91cc-77523e8165a4')
8088
.reply(200, uberBLACKReply);
8189
});
8290

test/reminders.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,33 @@ var reminderReply = {
2323

2424

2525
before(function() {
26-
nock('https://api.uber.com')
27-
.get('/v1/reminders/def-456?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT')
26+
nock('https://api.uber.com', {
27+
reqheaders: {
28+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
29+
}
30+
})
31+
.get('/v1/reminders/def-456')
2832
.times(2)
2933
.reply(200, reminderReply);
3034
nock('https://api.uber.com', {
3135
reqheaders: {
32-
'Authorization': 'Token ' + uber.defaults.server_token
36+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
3337
}
3438
})
3539
.post('/v1/reminders')
3640
.times(2)
3741
.reply(200, reminderReply);
3842
nock('https://api.uber.com', {
3943
reqheaders: {
40-
'Authorization': 'Token ' + uber.defaults.server_token
44+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
4145
}
4246
})
4347
.patch('/v1/reminders/def-456')
4448
.times(2)
4549
.reply(200, reminderReply);
4650
nock('https://api.uber.com', {
4751
reqheaders: {
48-
'Authorization': 'Token ' + uber.defaults.server_token
52+
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
4953
}
5054
})
5155
.delete('/v1/reminders/def-456')

0 commit comments

Comments
 (0)