Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ function makeWritableStream(dup, options, onComplete) {
// When the request is complete, parse it. If everything went well, pass
// the parsed response data to the callback handler.
stream.on('complete', function(res) {
handleResp(null, res, res.body, function(err, data) {
util.handleResp(null, res, res.body, function(err, data) {
if (err) {
dup.emit('error', err);
dup.end();
Expand Down
148 changes: 148 additions & 0 deletions test/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ describe('common/util', function() {
});
});

describe('format', function() {
it('should replace denoated keys with corresponding values', function() {
var formatted = util.format('{greeting} {thing}!', {
greeting: 'Hello',
thing: 'world'
});

assert.strictEqual(formatted, 'Hello world!');
});

it('should leave any unsatisfied keys unchanged', function() {
var formatted = util.format('{greeting} {thing}!', {
greeting: 'swamp'
});

assert.strictEqual(formatted, 'swamp {thing}!');
});

it('should inject falsy values', function() {
var formatted = util.format('{num}', {
num: 0
});

assert.strictEqual(formatted, '0');
});
});

This comment was marked as spam.

This comment was marked as spam.


describe('ApiError', function() {
it('should build correct ApiError', function() {
var error = {
Expand Down Expand Up @@ -420,9 +447,85 @@ describe('common/util', function() {
}
});
});

it('should emit an error if the request fails', function(done) {
var dup = duplexify();
var fakeStream = new stream.Writable();
var error = new Error('Error.');

fakeStream.write = function() {};
dup.end = function() {};

utilOverrides.handleResp = function(err, res, body, callback) {
callback(error);
};

requestOverride = function() {
return fakeStream;
};

var options = {
makeAuthorizedRequest: function(request, opts) {
opts.onAuthorized();
}
};

util.makeWritableStream(dup, options);

dup.on('error', function(err) {
assert.strictEqual(err, error);
done();
});

setImmediate(function() {
fakeStream.emit('complete', {});
});
});

it('should pass back the response data to the callback', function(done) {
var dup = duplexify();
var fakeStream = new stream.Writable();
var fakeResponse = {};

fakeStream.write = function() {};

utilOverrides.handleResp = function(err, res, body, callback) {
callback(null, fakeResponse);
};

requestOverride = function() {
return fakeStream;
};

var options = {
makeAuthorizedRequest: function(request, opts) {
opts.onAuthorized();
}
};

util.makeWritableStream(dup, options, function(data) {
assert.strictEqual(data, fakeResponse);
done();
});

setImmediate(function() {
fakeStream.emit('complete', {});
});
});
});

describe('getAuthClient', function() {
it('should use any authClient provided as a config', function(done) {
var config = {
authClient: {}
};

util.getAuthClient(config, function(err, authClient) {
assert.strictEqual(authClient, config.authClient);
done();
});
});

it('should use google-auth-library', function() {
var googleAuthLibraryCalled = false;

Expand Down Expand Up @@ -526,6 +629,23 @@ describe('common/util', function() {

util.getAuthClient(config, done);
});

it('should pass back any errors from the authClient', function(done) {
var error = new Error('Error!');

googleAuthLibraryOverride = function() {
return {
getApplicationDefault: function(callback) {
callback(error);
}
};
};

util.getAuthClient({}, function(err) {
assert.strictEqual(error, err);
done();
});
});
});

describe('authorizeRequest', function() {
Expand Down Expand Up @@ -952,6 +1072,29 @@ describe('common/util', function() {
});
});

describe('is', function() {
it('should check if an object is of the given type', function() {
assert(new Buffer(''), 'buffer');
assert(util.is('Hi!', 'string'));
assert(util.is([1, 2, 3], 'array'));
assert(util.is({}, 'object'));
assert(util.is(5, 'number'));
assert(util.is(true, 'boolean'));
});
});

describe('toArray', function() {
it('should convert an arguments object into an array', function() {
function stub() {
var args = util.toArray(arguments);

assert.deepEqual(args, [1, 2, 3]);
}

stub(1, 2, 3);
});
});

describe('shouldRetryRequest', function() {
it('should return false if there is no error', function() {
assert.strictEqual(util.shouldRetryRequest(), false);
Expand Down Expand Up @@ -1085,6 +1228,11 @@ describe('common/util', function() {
});

describe('callback mode', function() {
it('should optionally accept config', function(done) {
retryRequestOverride = testDefaultRetryRequestConfig(done);
util.makeRequest(reqOpts, assert.ifError);
});

it('should pass the default options to retryRequest', function(done) {
retryRequestOverride = testDefaultRetryRequestConfig(done);
util.makeRequest(reqOpts, {}, assert.ifError);
Expand Down