From 020bc03d3b3e388dea8483d75d4e5abe4062a99a Mon Sep 17 00:00:00 2001 From: Kelvin Jin Date: Wed, 23 Aug 2017 13:04:05 -0700 Subject: [PATCH] Add test and put back qs helper method --- .../src/google-apis/auth-client.js | 20 +++++++++-- .../test/unit/google-apis/auth-client.js | 35 ++++++++++++------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/packages/error-reporting/src/google-apis/auth-client.js b/packages/error-reporting/src/google-apis/auth-client.js index 097b7331f7f..affc731635a 100644 --- a/packages/error-reporting/src/google-apis/auth-client.js +++ b/packages/error-reporting/src/google-apis/auth-client.js @@ -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 @@ -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) => { diff --git a/packages/error-reporting/test/unit/google-apis/auth-client.js b/packages/error-reporting/test/unit/google-apis/auth-client.js index 50a1c6f7d39..7ec319e34cf 100644 --- a/packages/error-reporting/test/unit/google-apis/auth-client.js +++ b/packages/error-reporting/test/unit/google-apis/auth-client.js @@ -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 = { @@ -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() { @@ -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, {}); }); });