Skip to content

Commit cad6302

Browse files
clean up makeAuthorizedRequest & test
1 parent 97e4762 commit cad6302

6 files changed

Lines changed: 145 additions & 12 deletions

File tree

lib/bigquery/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function BigQuery(options) {
109109

110110
options = options || {};
111111

112-
this.makeAuthorizedRequest_ = util.makeAuthorizedRequest(this, {
112+
this.makeAuthorizedRequest_ = util.makeAuthorizedRequest({
113113
credentials: options.credentials,
114114
keyFile: options.keyFilename,
115115
scopes: SCOPES

lib/common/util.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,11 @@ function makeWritableStream(dup, options, onComplete) {
341341

342342
module.exports.makeWritableStream = makeWritableStream;
343343

344-
var tokenRefreshAttempts = 0;
345-
346-
function makeAuthorizedRequest(container, config) {
344+
function makeAuthorizedRequest(config) {
347345
var authorize = gsa(config);
348346

349347
function makeRequest(reqOpts, callback) {
350-
var that = this;
351-
348+
var tokenRefreshAttempts = 0;
352349
reqOpts.headers = reqOpts.headers || {};
353350

354351
if (reqOpts.headers['User-Agent']) {
@@ -361,15 +358,13 @@ function makeAuthorizedRequest(container, config) {
361358
if (err) {
362359
if (err.code === 401 &&
363360
++tokenRefreshAttempts <= MAX_TOKEN_REFRESH_ATTEMPTS) {
364-
that.authorizeReq_(reqOpts, onAuthorizedRequest);
361+
authorize(reqOpts, onAuthorizedRequest);
365362
} else {
366363
(callback.onAuthorized || callback)(err);
367364
}
368365
return;
369366
}
370367

371-
tokenRefreshAttempts = 0;
372-
373368
if (callback.onAuthorized) {
374369
callback.onAuthorized(null, authorizedReqOpts);
375370
} else {

lib/datastore/dataset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function Dataset(options) {
9393

9494
options = options || {};
9595

96-
this.makeAuthorizedRequest_ = util.makeAuthorizedRequest(this, {
96+
this.makeAuthorizedRequest_ = util.makeAuthorizedRequest({
9797
credentials: options.credentials,
9898
keyFile: options.keyFilename,
9999
scopes: SCOPES

lib/pubsub/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ var SCOPES = [
109109
function PubSub(options) {
110110
options = options || {};
111111

112-
this.makeAuthorizedRequest_ = util.makeAuthorizedRequest(this, {
112+
this.makeAuthorizedRequest_ = util.makeAuthorizedRequest({
113113
credentials: options.credentials,
114114
keyFile: options.keyFilename,
115115
scopes: SCOPES

lib/storage/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function Storage(config) {
101101
return new Storage(config);
102102
}
103103

104-
this.makeAuthorizedRequest_ = util.makeAuthorizedRequest(this, {
104+
this.makeAuthorizedRequest_ = util.makeAuthorizedRequest({
105105
credentials: config.credentials,
106106
keyFile: config.keyFilename,
107107
scopes: SCOPES

test/common/util.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,20 @@ var request = require('request');
2525
var util = require('sandboxed-module')
2626
.require('../../lib/common/util', {
2727
requires: {
28+
'google-service-account': fakeGsa,
2829
request: fakeRequest
2930
}
3031
});
3132

33+
var gsa_Override;
34+
35+
function fakeGsa() {
36+
var args = [].slice.apply(arguments);
37+
var results = (gsa_Override || util.noop).apply(null, args);
38+
gsa_Override = null;
39+
return results || { getCredentials: util.noop };
40+
}
41+
3242
var request_Cached = request;
3343
var request_Override;
3444

@@ -252,4 +262,132 @@ describe('common/util', function() {
252262
});
253263
});
254264
});
265+
266+
describe('makeAuthorizedRequest', function() {
267+
it('should pass configuration to gsa', function(done) {
268+
var config = { keyFile: 'key', scopes: [1, 2] };
269+
270+
gsa_Override = function(cfg) {
271+
assert.deepEqual(cfg, config);
272+
done();
273+
};
274+
275+
util.makeAuthorizedRequest(config);
276+
});
277+
278+
it('should return gsa.getCredentials function', function() {
279+
var getCredentials = util.makeAuthorizedRequest().getCredentials;
280+
assert.equal(typeof getCredentials, 'function');
281+
});
282+
283+
describe('makeRequest', function() {
284+
it('should add a user agent onto headers', function(done) {
285+
gsa_Override = function() {
286+
return function authorize(reqOpts) {
287+
assert(reqOpts.headers['User-Agent'].indexOf('gcloud') > -1);
288+
done();
289+
};
290+
};
291+
292+
var makeRequest = util.makeAuthorizedRequest();
293+
makeRequest({});
294+
});
295+
296+
it('should extend an existing user agent', function(done) {
297+
gsa_Override = function() {
298+
return function authorize(reqOpts) {
299+
var index = reqOpts.headers['User-Agent'].indexOf('test; gcloud');
300+
assert.equal(index, 0);
301+
done();
302+
};
303+
};
304+
305+
var makeRequest = util.makeAuthorizedRequest();
306+
makeRequest({ headers: { 'User-Agent': 'test' } });
307+
});
308+
309+
it('should execute callback with error', function(done) {
310+
var error = new Error('Error.');
311+
312+
gsa_Override = function() {
313+
return function authorize(reqOpts, callback) {
314+
callback(error);
315+
};
316+
};
317+
318+
var makeRequest = util.makeAuthorizedRequest();
319+
makeRequest({}, function(err) {
320+
assert.equal(err, error);
321+
done();
322+
});
323+
});
324+
325+
it('should try to reconnect if token invalid', function(done) {
326+
var attempts = 0;
327+
var expectedAttempts = 2;
328+
var error = { code: 401 };
329+
330+
gsa_Override = function() {
331+
return function authorize(reqOpts, callback) {
332+
attempts++;
333+
callback(error);
334+
};
335+
};
336+
337+
var makeRequest = util.makeAuthorizedRequest();
338+
makeRequest({}, function (err) {
339+
assert.equal(attempts, expectedAttempts);
340+
assert.equal(err, error);
341+
done();
342+
});
343+
});
344+
345+
it('should execute the onauthorized callback', function(done) {
346+
gsa_Override = function() {
347+
return function authorize(reqOpts, callback) {
348+
callback();
349+
};
350+
};
351+
352+
var makeRequest = util.makeAuthorizedRequest();
353+
makeRequest({}, { onAuthorized: done });
354+
});
355+
356+
it('should execute the onauthorized callback with error', function(done) {
357+
var error = new Error('Error.');
358+
359+
gsa_Override = function() {
360+
return function authorize(reqOpts, callback) {
361+
callback(error);
362+
};
363+
};
364+
365+
var makeRequest = util.makeAuthorizedRequest();
366+
makeRequest({}, {
367+
onAuthorized: function(err) {
368+
assert.equal(err, error);
369+
done();
370+
}
371+
});
372+
});
373+
374+
it('should make the authorized request', function(done) {
375+
var authorizedReqOpts = { a: 'b', c: 'd' };
376+
377+
gsa_Override = function() {
378+
return function authorize(reqOpts, callback) {
379+
callback(null, authorizedReqOpts);
380+
};
381+
};
382+
383+
request_Override = function(reqOpts) {
384+
assert.deepEqual(reqOpts, authorizedReqOpts);
385+
done();
386+
};
387+
388+
var makeRequest = util.makeAuthorizedRequest();
389+
makeRequest({}, assert.ifError);
390+
});
391+
});
392+
});
255393
});

0 commit comments

Comments
 (0)