Skip to content

Commit

Permalink
Add test and put back qs helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin committed Aug 23, 2017
1 parent d31902d commit 020bc03
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
20 changes: 17 additions & 3 deletions packages/error-reporting/src/google-apis/auth-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ var API = 'https://clouderrorreporting.googleapis.com/v1beta1';
* @property {Object} _logger - the instance-cached logger instance
*/
class RequestHandler extends common.Service {
/**
* Returns a query-string request object if a string key is given, otherwise
* will return null.
* @param {String|Null} [key] - the API key used to authenticate against the
* service in place of application default credentials.
* @returns {Object|Null} api key query string object for use with request or
* null in case no api key is given
* @static
*/
static manufactureQueryString(key) {
if (isString(key)) {
return {key: key};
}
return null;
}

/**
* No-operation stub function for user callback substitution
* @param {Error|Null} err - the error
Expand Down Expand Up @@ -119,9 +135,7 @@ class RequestHandler extends common.Service {
if (this._config.getShouldReportErrorsToAPI()) {
this.request({
uri: 'events:report',
qs: {
key: this._config.getKey()
},
qs: RequestHandler.manufactureQueryString(this._config.getKey()),
method: 'POST',
json: errorMessage
}, (err, body, response) => {
Expand Down
35 changes: 23 additions & 12 deletions packages/error-reporting/test/unit/google-apis/auth-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var proxyquire = require('proxyquire');

var Configuration = require('../../../src/configuration.js');

function verifyReportedMessage(config, errToReturn, expectedMessage) {
function verifyReportedMessage(config, errToReturn, expectedLogs) {
class ServiceStub {
constructor() {
this.authClient = {
Expand All @@ -37,16 +37,24 @@ function verifyReportedMessage(config, errToReturn, expectedMessage) {
}
});

var message = '';
var logs = {};
var logger = {
error: function(text) {
message += text;
if (!logs.error) {
logs.error = '';
}
logs.error += text;
},
info: function() {}
info: function(text) {
if (!logs.info) {
logs.info = '';
}
logs.info += text;
}
};
var config = new Configuration(config, logger);
new RequestHandler(config, logger);
assert.strictEqual(message, expectedMessage);
assert.deepStrictEqual(logs, expectedLogs);
}
describe('RequestHandler', function() {
it('should not request OAuth2 token if key is provided', function() {
Expand All @@ -55,21 +63,24 @@ describe('RequestHandler', function() {
key: 'key'
};
var message = 'Made OAuth2 Token Request';
verifyReportedMessage(config, new Error(message), '');
verifyReportedMessage(config, new Error(message), {
info: 'API key provided; skipping OAuth2 token request.'
});
});

it('should issue a warning if it cannot communicate with the API', function() {
var config = { ignoreEnvironmentCheck: true };
var message = 'Test Error';
verifyReportedMessage(config, new Error(message),
'Unable to find credential information on instance. This library ' +
'will be unable to communicate with the Stackdriver API to save ' +
'errors. Message: ' + message);
verifyReportedMessage(config, new Error(message), {
error: 'Unable to find credential information on instance. This ' +
'library will be unable to communicate with the Stackdriver API to ' +
'save errors. Message: ' + message
});
});

it('should not issue a warning if it can communicate with the API', function() {
var config = { ignoreEnvironmentCheck: true };
verifyReportedMessage(config, null, '');
verifyReportedMessage(config, undefined, '');
verifyReportedMessage(config, null, {});
verifyReportedMessage(config, undefined, {});
});
});

0 comments on commit 020bc03

Please sign in to comment.