Skip to content

Commit

Permalink
refactor GET authorization to header-based
Browse files Browse the repository at this point in the history
proposed bug fix for #31
  • Loading branch information
agraebe committed May 22, 2016
1 parent e379f93 commit b1c9d5b
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 78 deletions.
38 changes: 24 additions & 14 deletions lib/Uber.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Uber.prototype.modifierMethodHelper = function modifierMethodHelper(options, cal
if (options && options.server_token) {
access_type = 'Token ' + this.defaults.server_token;
} else {
if(!this.access_token) {
if (!this.access_token) {
return callback(new Error('Invalid access token'), 'A valid access token is required for this request');
} else {
// defaults to OAuth with access_token
Expand Down Expand Up @@ -172,34 +172,44 @@ Uber.prototype.modifierMethodHelper = function modifierMethodHelper(options, cal
return this;
};

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

if (!this.access_token) {
if(options && options.server_token) {
url += 'server_token=' + this.defaults.server_token;
} else {
return callback(new Error('Invalid access token'), 'A valid access token is required for this request');
}
if (server_token) {
access_type = 'Token ' + this.defaults.server_token;
} else {
url += qs.stringify({ access_token: this.access_token});
if (this.access_token) {
access_type = 'Bearer ' + this.access_token;
}
}

return access_type;
};

Uber.prototype.get = function get(options, callback) {
var access_type = this.createAccessHeader(options.server_token);
if (!access_type) {
return callback(new Error('Invalid access token'), 'A valid access token is required for this request');
}
var url = this.getRequestURL(options.version, options.url);


// add all further option params
if(options.params) {
url += '&' + qs.stringify(options.params);
if (options.params) {
url += '?' + qs.stringify(options.params);
}

request.get({
url: url,
json: true,
headers: {
'Content-Type': 'application/json',
'Authorization': access_type,
'Accept-Language': this.defaults.language
}
}, function(err, data, res) {
if (err || data.statusCode >= 400) {
return callback(err, data);
return callback((err ? err : data), res);
} else {
return callback(null, res);
}
Expand All @@ -222,5 +232,5 @@ Uber.prototype.getRequestURL = function getRequestURL(version, url) {
};

Uber.prototype.deprecateMethod = function deprecateMethod(f, oldMethod, newMethod) {
return util.deprecate(f, '`'+ oldMethod +'` is deprecated. Please use `'+ newMethod +'` instead.');
return util.deprecate(f, '`' + oldMethod + '` is deprecated. Please use `' + newMethod + '` instead.');
};
66 changes: 48 additions & 18 deletions test/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ describe('Products Resource', function() {
};

before(function() {
nock('https://api.uber.com')
.get('/v1/products?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&latitude=3.1357&longitude=101.688')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.get('/v1/products?latitude=3.1357&longitude=101.688')
.reply(200, productReply);
});

Expand Down Expand Up @@ -209,8 +213,12 @@ describe('Payment Resource', function() {
.post('/oauth/token')
.times(3)
.reply(200, tokenResponse);
nock('https://api.uber.com')
.get('/v1/payment-methods?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
}
})
.get('/v1/payment-methods')
.reply(200, paymentMethodsReply);
});

Expand Down Expand Up @@ -265,8 +273,12 @@ describe('Places Resource', function() {
.times(3)
.reply(200, tokenResponse);

nock('https://api.uber.com')
.get('/v1/places/home?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
}
})
.get('/v1/places/home')
.reply(200, placesHomeReply);
});

Expand Down Expand Up @@ -307,8 +319,12 @@ describe('Places Resource', function() {
.times(3)
.reply(200, tokenResponse);

nock('https://api.uber.com')
.get('/v1/places/work?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
}
})
.get('/v1/places/work')
.reply(200, placesWorkReply);
});

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

describe('Price Estimates', function() {
before(function() {
nock('https://api.uber.com')
.get('/v1/estimates/price?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&' +
'start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.get('/v1/estimates/price?start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
.reply(200, priceReply);
});

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

describe('Time Estimates', function() {
before(function() {
nock('https://api.uber.com')
.get('/v1/estimates/time?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&' +
'start_latitude=3.1357&start_longitude=101.688')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.get('/v1/estimates/time?start_latitude=3.1357&start_longitude=101.688')
.reply(200, timeReply);
});

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

nock('https://api.uber.com')
.get('/v1/me?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
}
})
.get('/v1/me')
.times(2)
.reply(200, profileReply);
});
Expand Down Expand Up @@ -533,9 +559,13 @@ describe('User Resource', function() {
.times(3)
.reply(200, tokenResponse);

nock('https://api.uber.com')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
}
})
.get(function(uri) {
var parts = uri.split('/v1.2/history?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL&offset=0&limit=');
var parts = uri.split('/v1.2/history?offset=0&limit=');
if (parts.length !== 2) {
return false;
}
Expand Down
33 changes: 18 additions & 15 deletions test/estimates.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,19 @@ var tokenResponse = {

describe('Price', function() {
before(function() {
nock('https://api.uber.com')
.get('/v1/estimates/price?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&' +
'start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.get('/v1/estimates/price?start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
.reply(200, priceReply);
nock('https://api.uber.com')
.get('/v1/estimates/price?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL&' +
'start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.get('/v1/estimates/price?start_latitude=3.1357&start_longitude=101.688&end_latitude=3.0833&end_longitude=101.65&seat_count=2')
.reply(200, priceReply);
});

Expand Down Expand Up @@ -124,18 +130,15 @@ describe('Time', function() {
.post('/oauth/token')
.times(3)
.reply(200, tokenResponse);
nock('https://api.uber.com')
.get(function(uri) {
return uri.indexOf('v1/estimates/time?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&' +
'start_latitude=3.1357&start_longitude=101.688') >= 0;
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.times(3)
.reply(200, timeReply);
nock('https://api.uber.com')
.get(function(uri) {
return uri.indexOf('v1/estimates/time?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL&' +
'start_latitude=3.1357&start_longitude=101.688') >= 0;
return uri.indexOf('v1/estimates/time?start_latitude=3.1357&start_longitude=101.688') >= 0;
})
.times(4)
.reply(200, timeReply);
});

Expand Down
8 changes: 6 additions & 2 deletions test/payment-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ before(function() {
.times(3)
.reply(200, tokenResponse);

nock('https://api.uber.com')
.get('/v1/payment-methods?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
}
})
.get('/v1/payment-methods')
.reply(200, paymentMethodsReply);
});

Expand Down
24 changes: 18 additions & 6 deletions test/places.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ describe('Home', function() {
.post('/oauth/token')
.times(3)
.reply(200, tokenResponse);
nock('https://api.uber.com')
.get('/v1/places/home?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
}
})
.get('/v1/places/home')
.reply(200, placesHomeReply);
nock('https://api.uber.com')
.put('/v1/places/home')
Expand Down Expand Up @@ -70,8 +74,12 @@ describe('Work', function() {
.post('/oauth/token')
.times(3)
.reply(200, tokenResponse);
nock('https://api.uber.com')
.get('/v1/places/work?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
}
})
.get('/v1/places/work')
.reply(200, placesWorkReply);
});

Expand Down Expand Up @@ -113,8 +121,12 @@ describe('By Place ID', function() {
nock('https://api.uber.com')
.put('/v1/places/shop')
.reply(404);
nock('https://api.uber.com')
.get('/v1/places/shop?access_token=EE1IDxytP04tJ767GbjH7ED9PpGmYvL')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Bearer EE1IDxytP04tJ767GbjH7ED9PpGmYvL'
}
})
.get('/v1/places/shop')
.reply(404);
});

Expand Down
16 changes: 12 additions & 4 deletions test/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ var uberBLACKReply = {

describe('List', function() {
before(function() {
nock('https://api.uber.com')
.get('/v1/products?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT&latitude=3.1357&longitude=101.688')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.get('/v1/products?latitude=3.1357&longitude=101.688')
.reply(200, productReply);
});

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

describe('Details', function() {
before(function() {
nock('https://api.uber.com')
.get('/v1/products/d4abaae7-f4d6-4152-91cc-77523e8165a4?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.get('/v1/products/d4abaae7-f4d6-4152-91cc-77523e8165a4')
.reply(200, uberBLACKReply);
});

Expand Down
14 changes: 9 additions & 5 deletions test/reminders.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,33 @@ var reminderReply = {


before(function() {
nock('https://api.uber.com')
.get('/v1/reminders/def-456?server_token=SERVERTOKENSERVERTOKENSERVERTOKENSERVERT')
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.get('/v1/reminders/def-456')
.times(2)
.reply(200, reminderReply);
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token ' + uber.defaults.server_token
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.post('/v1/reminders')
.times(2)
.reply(200, reminderReply);
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token ' + uber.defaults.server_token
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.patch('/v1/reminders/def-456')
.times(2)
.reply(200, reminderReply);
nock('https://api.uber.com', {
reqheaders: {
'Authorization': 'Token ' + uber.defaults.server_token
'Authorization': 'Token SERVERTOKENSERVERTOKENSERVERTOKENSERVERT'
}
})
.delete('/v1/reminders/def-456')
Expand Down
Loading

0 comments on commit b1c9d5b

Please sign in to comment.