From 7947b2ad9ac760cf97ba98e2225091c44306634b Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Tue, 6 Sep 2016 12:09:34 -0400 Subject: [PATCH 1/7] common: fix user agent strings --- packages/bigquery/src/index.js | 4 +-- packages/bigquery/test/index.js | 4 +-- packages/bigtable/src/index.js | 12 +++---- packages/bigtable/test/index.js | 3 +- packages/common/src/grpc-service.js | 5 +++ packages/common/src/service.js | 8 +++++ packages/common/src/util.js | 11 ------ packages/common/test/grpc-service.js | 30 ++++++++++++++-- packages/common/test/service.js | 22 +++++++++++- packages/common/test/util.js | 54 ---------------------------- packages/compute/src/index.js | 4 +-- packages/compute/test/index.js | 4 +-- packages/datastore/src/index.js | 4 +-- packages/datastore/test/index.js | 4 +-- packages/dns/src/index.js | 4 +-- packages/dns/test/index.js | 4 +-- packages/language/src/index.js | 4 +-- packages/language/test/index.js | 4 +-- packages/logging/src/index.js | 4 +-- packages/logging/test/index.js | 4 +-- packages/prediction/src/index.js | 4 +-- packages/prediction/test/index.js | 4 +-- packages/pubsub/src/index.js | 4 +-- packages/pubsub/test/index.js | 4 +-- packages/resource/src/index.js | 4 +-- packages/resource/test/index.js | 4 +-- packages/storage/src/index.js | 4 +-- packages/storage/test/index.js | 4 +-- packages/translate/src/index.js | 5 ++- packages/translate/test/index.js | 2 +- packages/vision/src/index.js | 4 +-- packages/vision/test/index.js | 4 +-- 32 files changed, 95 insertions(+), 145 deletions(-) diff --git a/packages/bigquery/src/index.js b/packages/bigquery/src/index.js index e343b51eb2f..38bd11d82fb 100644 --- a/packages/bigquery/src/index.js +++ b/packages/bigquery/src/index.js @@ -43,8 +43,6 @@ var Job = require('./job.js'); */ var Table = require('./table.js'); -var PKG = require('../package.json'); - /** * In the following examples from this page and the other modules (`Dataset`, * `Table`, etc.), we are going to be using a dataset from @@ -69,7 +67,7 @@ function BigQuery(options) { var config = { baseUrl: 'https://www.googleapis.com/bigquery/v2', scopes: ['https://www.googleapis.com/auth/bigquery'], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; common.Service.call(this, config, options); diff --git a/packages/bigquery/test/index.js b/packages/bigquery/test/index.js index aeecc0bb273..bd91873e209 100644 --- a/packages/bigquery/test/index.js +++ b/packages/bigquery/test/index.js @@ -26,8 +26,6 @@ var Service = require('@google-cloud/common').Service; var Table = require('../src/table.js'); var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - var fakeUtil = extend({}, util); function FakeTable(a, b) { @@ -118,7 +116,7 @@ describe('BigQuery', function() { assert.deepEqual(calledWith.scopes, [ 'https://www.googleapis.com/auth/bigquery' ]); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); }); }); diff --git a/packages/bigtable/src/index.js b/packages/bigtable/src/index.js index 6bb8b1cabc5..8bde933bbaf 100644 --- a/packages/bigtable/src/index.js +++ b/packages/bigtable/src/index.js @@ -29,17 +29,15 @@ var util = require('util'); /** * @private - * @type {module:bigtable/instance} + * @type {module:bigtable/cluster} */ -var Instance = require('./instance.js'); +var Cluster = require('./cluster.js'); /** * @private - * @type {module:bigtable/cluster} + * @type {module:bigtable/instance} */ -var Cluster = require('./cluster.js'); - -var PKG = require('../package.json'); +var Instance = require('./instance.js'); /** * @alias module:bigtable @@ -319,7 +317,7 @@ function Bigtable(options) { 'https://www.googleapis.com/auth/bigtable.data', 'https://www.googleapis.com/auth/cloud-platform' ], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; common.GrpcService.call(this, config, options); diff --git a/packages/bigtable/test/index.js b/packages/bigtable/test/index.js index 6b719f21525..b548e7ce9f3 100644 --- a/packages/bigtable/test/index.js +++ b/packages/bigtable/test/index.js @@ -26,7 +26,6 @@ var sinon = require('sinon').sandbox.create(); var common = require('@google-cloud/common'); var Cluster = require('../src/cluster.js'); var Instance = require('../src/instance.js'); -var PKG = require('../package.json'); var fakeUtil = extend({}, common.util); var fakeStreamRouter = { @@ -143,7 +142,7 @@ describe('Bigtable', function() { 'https://www.googleapis.com/auth/cloud-platform' ]); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); }); it('should set the projectName', function() { diff --git a/packages/common/src/grpc-service.js b/packages/common/src/grpc-service.js index 7919b640ae1..98925c2d13c 100644 --- a/packages/common/src/grpc-service.js +++ b/packages/common/src/grpc-service.js @@ -161,6 +161,11 @@ function GrpcService(config, options) { this.grpcMetadata = new grpc.Metadata(); + this.grpcMetadata.add('User-Agent', [ + config.packageJson.name.replace('@google-cloud', 'gcloud-node'), + config.packageJson.version + ].join('/')); + if (config.grpcMetadata) { for (var prop in config.grpcMetadata) { if (config.grpcMetadata.hasOwnProperty(prop)) { diff --git a/packages/common/src/service.js b/packages/common/src/service.js index 75f738f948a..7af2f372c34 100644 --- a/packages/common/src/service.js +++ b/packages/common/src/service.js @@ -57,6 +57,7 @@ function Service(config, options) { this.getCredentials = this.makeAuthenticatedRequest.getCredentials; this.globalInterceptors = arrify(options.interceptors_); this.interceptors = []; + this.packageJson = config.packageJson; this.projectId = options.projectId; this.projectIdRequired = config.projectIdRequired !== false; } @@ -114,6 +115,13 @@ Service.prototype.request = function(reqOpts, callback) { delete reqOpts.interceptors_; + reqOpts.headers = extend({}, reqOpts.headers, { + 'User-Agent': [ + this.packageJson.name.replace('@google-cloud', 'gcloud-node'), + this.packageJson.version + ].join('/') + }); + return this.makeAuthenticatedRequest(reqOpts, callback); }; diff --git a/packages/common/src/util.js b/packages/common/src/util.js index f279531aff7..c8d9d9d7eea 100644 --- a/packages/common/src/util.js +++ b/packages/common/src/util.js @@ -38,12 +38,6 @@ var streamEvents = require('stream-events'); var through = require('through2'); var uniq = require('array-uniq'); -/** @const {object} @google-cloud/common's package.json file. */ -var PKG = require('../package.json'); - -/** @const {string} User agent. */ -var USER_AGENT = PKG.name + '/' + PKG.version; - var util = module.exports; var errorMessage = format([ @@ -466,11 +460,6 @@ util.makeRequest = makeRequest; */ function decorateRequest(reqOpts, config) { config = config || {}; - reqOpts.headers = reqOpts.headers || {}; - - var headers = reqOpts.headers; - var userAgent = headers['User-Agent'] || headers['user-agent']; - headers['User-Agent'] = userAgent || config.userAgent || USER_AGENT; if (is.object(reqOpts.qs)) { delete reqOpts.qs.autoPaginate; diff --git a/packages/common/test/grpc-service.js b/packages/common/test/grpc-service.js index 05392a49ea7..2c4997ae056 100644 --- a/packages/common/test/grpc-service.js +++ b/packages/common/test/grpc-service.js @@ -92,6 +92,10 @@ describe('GrpcService', function() { proto: {}, service: 'Service', apiVersion: 'v1', + packageJson: { + name: '@google-cloud/service', + version: '0.2.0' + }, grpcMetadata: { property: 'value' } @@ -243,8 +247,20 @@ describe('GrpcService', function() { assert.strictEqual(calledWith[1], OPTIONS); }); - it('should default grpcMetadata to empty metadata', function() { - var fakeGrpcMetadata = {}; + it('should set grpcMetadata with the correct User Agent', function() { + var userAgentAdded = false; + + var fakeGrpcMetadata = { + add: function(prop, value) { + assert.strictEqual(prop, 'User-Agent'); + assert.strictEqual(value, [ + CONFIG.packageJson.name.replace('@google-cloud', 'gcloud-node'), + CONFIG.packageJson.version + ].join('/')); + + userAgentAdded = true; + } + }; GrpcMetadataOverride = function() { return fakeGrpcMetadata; @@ -255,13 +271,22 @@ describe('GrpcService', function() { var grpcService = new GrpcService(config, OPTIONS); assert.strictEqual(grpcService.grpcMetadata, fakeGrpcMetadata); + assert.strictEqual(userAgentAdded, true); }); it('should create and localize grpcMetadata', function() { + var userAgentAdded = false; + var fakeGrpcMetadata = { add: function(prop, value) { + if (prop === 'User-Agent') { + return; // Already tested. + } + assert.strictEqual(prop, Object.keys(CONFIG.grpcMetadata)[0]); assert.strictEqual(value, CONFIG.grpcMetadata[prop]); + + userAgentAdded = true; } }; @@ -271,6 +296,7 @@ describe('GrpcService', function() { var grpcService = new GrpcService(CONFIG, OPTIONS); assert.strictEqual(grpcService.grpcMetadata, fakeGrpcMetadata); + assert.strictEqual(userAgentAdded, true); }); it('should localize maxRetries', function() { diff --git a/packages/common/test/service.js b/packages/common/test/service.js index b6202987aa5..d66f3e66feb 100644 --- a/packages/common/test/service.js +++ b/packages/common/test/service.js @@ -39,7 +39,11 @@ describe('Service', function() { var CONFIG = { scopes: [], baseUrl: 'base-url', - projectIdRequired: false + projectIdRequired: false, + packageJson: { + name: '@google-cloud/service', + version: '0.2.0' + } }; var OPTIONS = { @@ -129,6 +133,10 @@ describe('Service', function() { assert.deepEqual(service.interceptors, []); }); + it('should localize package.json', function() { + assert.strictEqual(service.packageJson, CONFIG.packageJson); + }); + it('should localize the projectId', function() { assert.strictEqual(service.projectId, OPTIONS.projectId); }); @@ -212,6 +220,18 @@ describe('Service', function() { service.request(reqOpts, assert.ifError); }); + it('should add the User Agent', function(done) { + service.makeAuthenticatedRequest = function(reqOpts) { + assert.strictEqual(reqOpts.headers['User-Agent'], [ + CONFIG.packageJson.name.replace('@google-cloud', 'gcloud-node'), + CONFIG.packageJson.version + ].join('/')); + done(); + }; + + service.request(reqOpts, assert.ifError); + }); + describe('projectIdRequired', function() { describe('false', function() { it('should include the projectId', function(done) { diff --git a/packages/common/test/util.js b/packages/common/test/util.js index 83c49f424ec..1c89ed9fcc5 100644 --- a/packages/common/test/util.js +++ b/packages/common/test/util.js @@ -1153,60 +1153,6 @@ describe('common/util', function() { }); describe('decorateRequest', function() { - it('should keep user agent', function() { - var USER_AGENT = 'test/v1'; - - var reqOpts = { - a: 'b', - c: 'd', - headers: { - 'User-Agent': USER_AGENT - } - }; - - var expectedReqOpts = extend({}, reqOpts, { - headers: { - 'User-Agent': USER_AGENT - } - }); - - var decoratedReqOpts = util.decorateRequest(reqOpts); - assert.deepEqual(decoratedReqOpts, expectedReqOpts); - }); - - it('should add the user agent', function() { - var USER_AGENT = 'test/v2'; - - var reqOpts = { a: 'b', c: 'd' }; - - var expectedReqOpts = extend({}, reqOpts, { - headers: { - 'User-Agent': USER_AGENT - } - }); - - var decoratedReqOpts = util.decorateRequest(reqOpts, { - userAgent: USER_AGENT - }); - assert.deepEqual(decoratedReqOpts, expectedReqOpts); - }); - - it('should add the default user agent', function() { - var PKG = require('../package.json'); - var USER_AGENT = PKG.name + '/' + PKG.version; - - var reqOpts = { a: 'b', c: 'd' }; - - var expectedReqOpts = extend({}, reqOpts, { - headers: { - 'User-Agent': USER_AGENT - } - }); - - var decoratedReqOpts = util.decorateRequest(reqOpts); - assert.deepEqual(decoratedReqOpts, expectedReqOpts); - }); - it('should delete qs.autoPaginate', function() { var decoratedReqOpts = util.decorateRequest({ qs: { diff --git a/packages/compute/src/index.js b/packages/compute/src/index.js index f282f756754..59b34da1662 100644 --- a/packages/compute/src/index.js +++ b/packages/compute/src/index.js @@ -80,8 +80,6 @@ var Snapshot = require('./snapshot.js'); */ var Zone = require('./zone.js'); -var PKG = require('../package.json'); - /** * @alias module:compute * @constructor @@ -99,7 +97,7 @@ function Compute(options) { var config = { baseUrl: 'https://www.googleapis.com/compute/v1', scopes: ['https://www.googleapis.com/auth/compute'], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; common.Service.call(this, config, options); diff --git a/packages/compute/test/index.js b/packages/compute/test/index.js index 96bcfed0e52..0c658accb2b 100644 --- a/packages/compute/test/index.js +++ b/packages/compute/test/index.js @@ -25,8 +25,6 @@ var proxyquire = require('proxyquire'); var Service = require('@google-cloud/common').Service; var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - var slice = Array.prototype.slice; var fakeUtil = extend({}, util, { @@ -183,7 +181,7 @@ describe('Compute', function() { assert.deepEqual(calledWith.scopes, [ 'https://www.googleapis.com/auth/compute' ]); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); }); }); diff --git a/packages/datastore/src/index.js b/packages/datastore/src/index.js index b73f36d446c..0bef77f26a7 100644 --- a/packages/datastore/src/index.js +++ b/packages/datastore/src/index.js @@ -49,8 +49,6 @@ var Query = require('./query.js'); */ var Transaction = require('./transaction.js'); -var PKG = require('../package.json'); - /** * @constructor * @alias module:datastore @@ -323,7 +321,7 @@ function Datastore(options) { service: 'datastore', apiVersion: 'v1', scopes: ['https://www.googleapis.com/auth/datastore'], - userAgent: PKG.name + '/' + PKG.version, + packageJson: require('../package.json'), grpcMetadata: { 'google-cloud-resource-prefix': 'projects/' + this.projectId } diff --git a/packages/datastore/test/index.js b/packages/datastore/test/index.js index 64e3e1544bb..ad7161ecf32 100644 --- a/packages/datastore/test/index.js +++ b/packages/datastore/test/index.js @@ -21,8 +21,6 @@ var extend = require('extend'); var proxyquire = require('proxyquire'); var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - var fakeEntity = { Int: function(value) { this.value = value; @@ -157,7 +155,7 @@ describe('Datastore', function() { assert.deepEqual(calledWith.scopes, [ 'https://www.googleapis.com/auth/datastore' ]); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); assert.deepEqual(calledWith.grpcMetadata, { 'google-cloud-resource-prefix': 'projects/' + datastore.projectId }); diff --git a/packages/dns/src/index.js b/packages/dns/src/index.js index ffb13b0f149..c04c552aaa1 100644 --- a/packages/dns/src/index.js +++ b/packages/dns/src/index.js @@ -32,8 +32,6 @@ var util = require('util'); */ var Zone = require('./zone.js'); -var PKG = require('../package.json'); - /** * [Google Cloud DNS](https://cloud.google.com/dns/what-is-cloud-dns) is a high- * performance, resilient, global DNS service that provides a cost-effective way @@ -60,7 +58,7 @@ function DNS(options) { 'https://www.googleapis.com/auth/ndev.clouddns.readwrite', 'https://www.googleapis.com/auth/cloud-platform' ], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; common.Service.call(this, config, options); diff --git a/packages/dns/test/index.js b/packages/dns/test/index.js index 5cb87ec79fa..9aa5a5e4a2c 100644 --- a/packages/dns/test/index.js +++ b/packages/dns/test/index.js @@ -24,8 +24,6 @@ var proxyquire = require('proxyquire'); var Service = require('@google-cloud/common').Service; var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - var extended = false; var fakeStreamRouter = { extend: function(Class, methods) { @@ -113,7 +111,7 @@ describe('DNS', function() { 'https://www.googleapis.com/auth/ndev.clouddns.readwrite', 'https://www.googleapis.com/auth/cloud-platform' ]); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); }); }); diff --git a/packages/language/src/index.js b/packages/language/src/index.js index 3eb43b5f9c8..1c8fa896bdd 100644 --- a/packages/language/src/index.js +++ b/packages/language/src/index.js @@ -32,8 +32,6 @@ var util = require('util'); */ var Document = require('./document.js'); -var PKG = require('../package.json'); - /** *

* **This is a Beta release of Google Cloud Natural Language.** This API is @@ -79,7 +77,7 @@ function Language(options) { scopes: [ 'https://www.googleapis.com/auth/cloud-platform' ], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; common.GrpcService.call(this, config, options); diff --git a/packages/language/test/index.js b/packages/language/test/index.js index 13a9af7d227..535a3e1ccd9 100644 --- a/packages/language/test/index.js +++ b/packages/language/test/index.js @@ -22,8 +22,6 @@ var googleProtoFiles = require('google-proto-files'); var proxyquire = require('proxyquire'); var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - var fakeUtil = extend(true, {}, util); function FakeDocument() { @@ -98,7 +96,7 @@ describe('Language', function() { scopes: [ 'https://www.googleapis.com/auth/cloud-platform' ], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }); }); }); diff --git a/packages/logging/src/index.js b/packages/logging/src/index.js index d52885a3410..c20563681cd 100644 --- a/packages/logging/src/index.js +++ b/packages/logging/src/index.js @@ -46,8 +46,6 @@ var Log = require('./log.js'); */ var Sink = require('./sink.js'); -var PKG = require('../package.json'); - /** *

* **This is a Beta release of Stackdriver Logging.** This API is not covered @@ -85,7 +83,7 @@ function Logging(options) { scopes: [ 'https://www.googleapis.com/auth/cloud-platform' ], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; common.GrpcService.call(this, config, options); diff --git a/packages/logging/test/index.js b/packages/logging/test/index.js index 215a88c3ab0..a5cacbd4123 100644 --- a/packages/logging/test/index.js +++ b/packages/logging/test/index.js @@ -23,8 +23,6 @@ var googleProtoFiles = require('google-proto-files'); var proxyquire = require('proxyquire'); var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - var extended = false; var fakeStreamRouter = { extend: function(Class, methods) { @@ -147,7 +145,7 @@ describe('Logging', function() { assert.deepEqual(calledWith.scopes, [ 'https://www.googleapis.com/auth/cloud-platform' ]); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); }); }); diff --git a/packages/prediction/src/index.js b/packages/prediction/src/index.js index a53df586fbc..eecb91d930c 100644 --- a/packages/prediction/src/index.js +++ b/packages/prediction/src/index.js @@ -33,8 +33,6 @@ var util = require('util'); */ var Model = require('./model.js'); -var PKG = require('../package.json'); - /** * The [Google Prediction API](https://cloud.google.com/prediction/docs/getting-started) * provides pattern-matching and machine learning capabilities. Given a set of @@ -70,7 +68,7 @@ function Prediction(options) { 'https://www.googleapis.com/auth/prediction', 'https://www.googleapis.com/auth/devstorage.read_only' ], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; common.Service.call(this, config, options); diff --git a/packages/prediction/test/index.js b/packages/prediction/test/index.js index 283c418ee04..22111064f44 100644 --- a/packages/prediction/test/index.js +++ b/packages/prediction/test/index.js @@ -24,8 +24,6 @@ var proxyquire = require('proxyquire'); var Service = require('@google-cloud/common').Service; var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - function FakeModel() { this.calledWith_ = arguments; } @@ -114,7 +112,7 @@ describe('Prediction', function() { 'https://www.googleapis.com/auth/prediction', 'https://www.googleapis.com/auth/devstorage.read_only' ]); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); }); }); diff --git a/packages/pubsub/src/index.js b/packages/pubsub/src/index.js index 8a5b237c62c..d76f4968a95 100644 --- a/packages/pubsub/src/index.js +++ b/packages/pubsub/src/index.js @@ -38,8 +38,6 @@ var Subscription = require('./subscription.js'); */ var Topic = require('./topic.js'); -var PKG = require('../package.json'); - /** * [Google Cloud Pub/Sub](https://developers.google.com/pubsub/overview) is a * reliable, many-to-many, asynchronous messaging service from Google Cloud @@ -73,7 +71,7 @@ function PubSub(options) { 'https://www.googleapis.com/auth/pubsub', 'https://www.googleapis.com/auth/cloud-platform' ], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; this.options = options; diff --git a/packages/pubsub/test/index.js b/packages/pubsub/test/index.js index f4c72c91947..111e31f098e 100644 --- a/packages/pubsub/test/index.js +++ b/packages/pubsub/test/index.js @@ -22,8 +22,6 @@ var extend = require('extend'); var proxyquire = require('proxyquire'); var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - var SubscriptionCached = require('../src/subscription.js'); var SubscriptionOverride; @@ -124,7 +122,7 @@ describe('PubSub', function() { 'https://www.googleapis.com/auth/pubsub', 'https://www.googleapis.com/auth/cloud-platform' ]); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); }); it('should set the defaultBaseUrl_', function() { diff --git a/packages/resource/src/index.js b/packages/resource/src/index.js index 88f0134cf29..0a9df2fdd31 100644 --- a/packages/resource/src/index.js +++ b/packages/resource/src/index.js @@ -31,8 +31,6 @@ var util = require('util'); */ var Project = require('./project.js'); -var PKG = require('../package.json'); - /** *

* **This is a Beta release of Cloud Resource Manager.** This feature is not @@ -69,7 +67,7 @@ function Resource(options) { baseUrl: 'https://cloudresourcemanager.googleapis.com/v1beta1', scopes: ['https://www.googleapis.com/auth/cloud-platform'], projectIdRequired: false, - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; common.Service.call(this, config, options); diff --git a/packages/resource/test/index.js b/packages/resource/test/index.js index d0ac5dc8631..07a39e6adf5 100644 --- a/packages/resource/test/index.js +++ b/packages/resource/test/index.js @@ -24,8 +24,6 @@ var proxyquire = require('proxyquire'); var Service = require('@google-cloud/common').Service; var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - function FakeProject() { this.calledWith_ = [].slice.call(arguments); } @@ -126,7 +124,7 @@ describe('Resource', function() { 'https://www.googleapis.com/auth/cloud-platform' ]); assert.strictEqual(resource.projectIdRequired, false); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); }); }); diff --git a/packages/storage/src/index.js b/packages/storage/src/index.js index 7ebb266e8c9..7531f884ed4 100644 --- a/packages/storage/src/index.js +++ b/packages/storage/src/index.js @@ -43,8 +43,6 @@ var Channel = require('./channel.js'); */ var File = require('./file.js'); -var PKG = require('../package.json'); - /*! Developer Documentation * * Invoke this method to create a new Storage object bound with pre-determined @@ -83,7 +81,7 @@ function Storage(options) { scopes: [ 'https://www.googleapis.com/auth/devstorage.full_control' ], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; common.Service.call(this, config, options); diff --git a/packages/storage/test/index.js b/packages/storage/test/index.js index b581c73c5ed..178a6fc19ed 100644 --- a/packages/storage/test/index.js +++ b/packages/storage/test/index.js @@ -24,8 +24,6 @@ var proxyquire = require('proxyquire'); var Service = require('@google-cloud/common').Service; var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - function FakeChannel() { this.calledWith_ = arguments; } @@ -110,7 +108,7 @@ describe('Storage', function() { assert.deepEqual(calledWith.scopes, [ 'https://www.googleapis.com/auth/devstorage.full_control' ]); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); }); }); diff --git a/packages/translate/src/index.js b/packages/translate/src/index.js index c05be0e6bf4..5a3cf0633b9 100644 --- a/packages/translate/src/index.js +++ b/packages/translate/src/index.js @@ -384,7 +384,10 @@ Translate.prototype.request = function(reqOpts, callback) { key: this.key }, headers: { - 'User-Agent': PKG.name + '/' + PKG.version + 'User-Agent': [ + PKG.name.replace('@google-cloud', 'gcloud-node'), + PKG.version + ].join('/') } }); diff --git a/packages/translate/test/index.js b/packages/translate/test/index.js index b6969c9ff7f..dd1b7dd0298 100644 --- a/packages/translate/test/index.js +++ b/packages/translate/test/index.js @@ -401,7 +401,7 @@ describe('Translate', function() { key: translate.key }, headers: { - 'User-Agent': PKG.name + '/' + PKG.version + 'User-Agent': 'gcloud-node/translate/' + PKG.version } }); var BASE_URL = 'https://www.googleapis.com/language/translate/v2'; diff --git a/packages/vision/src/index.js b/packages/vision/src/index.js index c4b1255bcac..662c9e5151c 100644 --- a/packages/vision/src/index.js +++ b/packages/vision/src/index.js @@ -32,8 +32,6 @@ var request = require('request'); var rgbHex = require('rgb-hex'); var util = require('util'); -var PKG = require('../package.json'); - var VERY_UNLIKELY = 0; var UNLIKELY = 1; var POSSIBLE = 2; @@ -72,7 +70,7 @@ function Vision(options) { scopes: [ 'https://www.googleapis.com/auth/cloud-platform' ], - userAgent: PKG.name + '/' + PKG.version + packageJson: require('../package.json') }; common.Service.call(this, config, options); diff --git a/packages/vision/test/index.js b/packages/vision/test/index.js index 5862407e5d6..7790d726ac2 100644 --- a/packages/vision/test/index.js +++ b/packages/vision/test/index.js @@ -29,8 +29,6 @@ var Service = require('@google-cloud/common').Service; var tmp = require('tmp'); var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - var fakeUtil = extend({}, util); function FakeService() { @@ -101,7 +99,7 @@ describe('Vision', function() { assert.deepEqual(calledWith.scopes, [ 'https://www.googleapis.com/auth/cloud-platform' ]); - assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version); + assert.deepEqual(calledWith.packageJson, require('../package.json')); }); }); From 450b2899e6f0a5ab186319121358f702e2a08130 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 9 Sep 2016 12:38:14 -0400 Subject: [PATCH 2/7] set user agent correctly --- packages/common/src/grpc-service.js | 13 ++++---- packages/common/test/grpc-service.js | 46 ++++++++++------------------ 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/packages/common/src/grpc-service.js b/packages/common/src/grpc-service.js index 98925c2d13c..c6d189cd344 100644 --- a/packages/common/src/grpc-service.js +++ b/packages/common/src/grpc-service.js @@ -161,11 +161,6 @@ function GrpcService(config, options) { this.grpcMetadata = new grpc.Metadata(); - this.grpcMetadata.add('User-Agent', [ - config.packageJson.name.replace('@google-cloud', 'gcloud-node'), - config.packageJson.version - ].join('/')); - if (config.grpcMetadata) { for (var prop in config.grpcMetadata) { if (config.grpcMetadata.hasOwnProperty(prop)) { @@ -176,6 +171,11 @@ function GrpcService(config, options) { this.maxRetries = options.maxRetries; + var hyphenatedPackageName = config.packageJson.name + .replace('@google-cloud', 'gcloud-node') // For legacy purposes. + .replace('/', '-'); // For UA spec-compliance purposes. + this.userAgent = hyphenatedPackageName + '/' + config.packageJson.version; + var apiVersion = config.apiVersion; var service = this.service = config.service; @@ -737,7 +737,8 @@ GrpcService.prototype.getService_ = function(protoOpts) { if (!service) { service = new proto[protoOpts.service]( proto.baseUrl || this.baseUrl, - this.grpcCredentials + this.grpcCredentials, + { 'grpc.primary_user_agent': this.userAgent } ); this.activeServiceMap_.set(protoOpts.service, service); diff --git a/packages/common/test/grpc-service.js b/packages/common/test/grpc-service.js index 2c4997ae056..dcdb806b636 100644 --- a/packages/common/test/grpc-service.js +++ b/packages/common/test/grpc-service.js @@ -247,20 +247,14 @@ describe('GrpcService', function() { assert.strictEqual(calledWith[1], OPTIONS); }); - it('should set grpcMetadata with the correct User Agent', function() { - var userAgentAdded = false; - - var fakeGrpcMetadata = { - add: function(prop, value) { - assert.strictEqual(prop, 'User-Agent'); - assert.strictEqual(value, [ - CONFIG.packageJson.name.replace('@google-cloud', 'gcloud-node'), - CONFIG.packageJson.version - ].join('/')); + it('should set insecure credentials if using customEndpoint', function() { + var config = extend({}, CONFIG, { customEndpoint: true }); + var grpcService = new GrpcService(config, OPTIONS); + assert.strictEqual(grpcService.grpcCredentials.name, 'createInsecure'); + }); - userAgentAdded = true; - } - }; + it('should default grpcMetadata to empty metadata', function() { + var fakeGrpcMetadata = {}; GrpcMetadataOverride = function() { return fakeGrpcMetadata; @@ -271,22 +265,13 @@ describe('GrpcService', function() { var grpcService = new GrpcService(config, OPTIONS); assert.strictEqual(grpcService.grpcMetadata, fakeGrpcMetadata); - assert.strictEqual(userAgentAdded, true); }); it('should create and localize grpcMetadata', function() { - var userAgentAdded = false; - var fakeGrpcMetadata = { add: function(prop, value) { - if (prop === 'User-Agent') { - return; // Already tested. - } - assert.strictEqual(prop, Object.keys(CONFIG.grpcMetadata)[0]); assert.strictEqual(value, CONFIG.grpcMetadata[prop]); - - userAgentAdded = true; } }; @@ -296,13 +281,16 @@ describe('GrpcService', function() { var grpcService = new GrpcService(CONFIG, OPTIONS); assert.strictEqual(grpcService.grpcMetadata, fakeGrpcMetadata); - assert.strictEqual(userAgentAdded, true); }); it('should localize maxRetries', function() { assert.strictEqual(grpcService.maxRetries, OPTIONS.maxRetries); }); + it('should set the correct user-agent', function() { + assert.strictEqual(grpcService.userAgent, 'gcloud-node-service/0.2.0'); + }); + it('should get the root directory for the proto files', function(done) { googleProtoFilesOverride = function(path) { assert.strictEqual(path, '..'); @@ -313,12 +301,6 @@ describe('GrpcService', function() { new GrpcService(CONFIG, OPTIONS); }); - it('should set insecure credentials if using customEndpoint', function() { - var config = extend({}, CONFIG, { customEndpoint: true }); - var grpcService = new GrpcService(config, OPTIONS); - assert.strictEqual(grpcService.grpcCredentials.name, 'createInsecure'); - }); - it('should localize the service', function() { assert.strictEqual(grpcService.service, CONFIG.service); }); @@ -1524,9 +1506,13 @@ describe('GrpcService', function() { grpcService.protos = { Service: { - Service: function(baseUrl, grpcCredentials) { + Service: function(baseUrl, grpcCredentials, userAgent) { assert.strictEqual(baseUrl, grpcService.baseUrl); assert.strictEqual(grpcCredentials, grpcService.grpcCredentials); + assert.deepEqual(userAgent, { + 'grpc.primary_user_agent': grpcService.userAgent + }); + return fakeService; } } From c21590c0d229547bc69e6c07d7b41fe55e34c9e7 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 9 Sep 2016 12:56:23 -0400 Subject: [PATCH 3/7] move UA formatting to util --- packages/common/src/grpc-service.js | 12 +++++++----- packages/common/src/service.js | 5 +---- packages/common/src/util.js | 16 ++++++++++++++++ packages/common/test/grpc-service.js | 17 +++++++++++++++-- packages/common/test/service.js | 14 ++++++++++---- packages/common/test/util.js | 11 +++++++++++ packages/translate/src/index.js | 5 +---- packages/translate/test/index.js | 13 ++++++++++--- 8 files changed, 71 insertions(+), 22 deletions(-) diff --git a/packages/common/src/grpc-service.js b/packages/common/src/grpc-service.js index c6d189cd344..dde6a2401af 100644 --- a/packages/common/src/grpc-service.js +++ b/packages/common/src/grpc-service.js @@ -36,6 +36,12 @@ var through = require('through2'); */ var Service = require('./service.js'); +/** + * @type {module:common/util} + * @private + */ +var util = require('./util.js'); + /** * @const {object} - A map of protobuf codes to HTTP status codes. * @private @@ -170,11 +176,7 @@ function GrpcService(config, options) { } this.maxRetries = options.maxRetries; - - var hyphenatedPackageName = config.packageJson.name - .replace('@google-cloud', 'gcloud-node') // For legacy purposes. - .replace('/', '-'); // For UA spec-compliance purposes. - this.userAgent = hyphenatedPackageName + '/' + config.packageJson.version; + this.userAgent = util.getUserAgentFromPackageJson(config.packageJson); var apiVersion = config.apiVersion; var service = this.service = config.service; diff --git a/packages/common/src/service.js b/packages/common/src/service.js index 7af2f372c34..54aea3bd33e 100644 --- a/packages/common/src/service.js +++ b/packages/common/src/service.js @@ -116,10 +116,7 @@ Service.prototype.request = function(reqOpts, callback) { delete reqOpts.interceptors_; reqOpts.headers = extend({}, reqOpts.headers, { - 'User-Agent': [ - this.packageJson.name.replace('@google-cloud', 'gcloud-node'), - this.packageJson.version - ].join('/') + 'User-Agent': util.getUserAgentFromPackageJson(this.packageJson) }); return this.makeAuthenticatedRequest(reqOpts, callback); diff --git a/packages/common/src/util.js b/packages/common/src/util.js index c8d9d9d7eea..ac7ac524475 100644 --- a/packages/common/src/util.js +++ b/packages/common/src/util.js @@ -614,3 +614,19 @@ function isCustomType(unknown, module) { } util.isCustomType = isCustomType; + +/** + * Create a properly-formatted User-Agent string from a package.json file. + * + * @param {object} packageJson - A module's package.json file. + * @return {string} userAgent - The formatted User-Agent string. + */ +function getUserAgentFromPackageJson(packageJson) { + var hyphenatedPackageName = packageJson.name + .replace('@google-cloud', 'gcloud-node') // For legacy purposes. + .replace('/', '-'); // For UA spec-compliance purposes. + + return hyphenatedPackageName + '/' + packageJson.version; +} + +util.getUserAgentFromPackageJson = getUserAgentFromPackageJson; diff --git a/packages/common/test/grpc-service.js b/packages/common/test/grpc-service.js index dcdb806b636..e2a814ee856 100644 --- a/packages/common/test/grpc-service.js +++ b/packages/common/test/grpc-service.js @@ -28,6 +28,7 @@ var sinon = require('sinon').sandbox.create(); var through = require('through2'); var util = require('../src/util.js'); +var fakeUtil = extend({}, util); function FakeService() { this.calledWith_ = arguments; @@ -43,6 +44,7 @@ function fakeRetryRequest() { return (retryRequestOverride || retryRequest).apply(null, arguments); } + var GrpcMetadataOverride; var grpcLoadOverride; var fakeGrpc = { @@ -119,7 +121,8 @@ describe('GrpcService', function() { 'google-proto-files': fakeGoogleProtoFiles, 'retry-request': fakeRetryRequest, grpc: fakeGrpc, - './service.js': FakeService + './service.js': FakeService, + './util.js': fakeUtil }); GrpcServiceCached = extend(true, {}, GrpcService); }); @@ -288,7 +291,17 @@ describe('GrpcService', function() { }); it('should set the correct user-agent', function() { - assert.strictEqual(grpcService.userAgent, 'gcloud-node-service/0.2.0'); + var userAgent = 'user-agent/0.0.0'; + + var getUserAgentFn = fakeUtil.getUserAgentFromPackageJson; + fakeUtil.getUserAgentFromPackageJson = function(packageJson) { + fakeUtil.getUserAgentFromPackageJson = getUserAgentFn; + assert.strictEqual(packageJson, CONFIG.packageJson); + return userAgent; + }; + + var grpcService = new GrpcService(CONFIG, OPTIONS); + assert.strictEqual(grpcService.userAgent, userAgent); }); it('should get the root directory for the proto files', function(done) { diff --git a/packages/common/test/service.js b/packages/common/test/service.js index d66f3e66feb..f7ade0d0aba 100644 --- a/packages/common/test/service.js +++ b/packages/common/test/service.js @@ -221,11 +221,17 @@ describe('Service', function() { }); it('should add the User Agent', function(done) { + var userAgent = 'user-agent/0.0.0'; + + var getUserAgentFn = util.getUserAgentFromPackageJson; + util.getUserAgentFromPackageJson = function(packageJson) { + util.getUserAgentFromPackageJson = getUserAgentFn; + assert.strictEqual(packageJson, service.packageJson); + return userAgent; + }; + service.makeAuthenticatedRequest = function(reqOpts) { - assert.strictEqual(reqOpts.headers['User-Agent'], [ - CONFIG.packageJson.name.replace('@google-cloud', 'gcloud-node'), - CONFIG.packageJson.version - ].join('/')); + assert.strictEqual(reqOpts.headers['User-Agent'], userAgent); done(); }; diff --git a/packages/common/test/util.js b/packages/common/test/util.js index 1c89ed9fcc5..8ba26fef3dc 100644 --- a/packages/common/test/util.js +++ b/packages/common/test/util.js @@ -1363,4 +1363,15 @@ describe('common/util', function() { }); }); }); + + describe('getUserAgentFromPackageJson', function() { + it('should format a User Agent string from a package.json', function() { + var userAgent = util.getUserAgentFromPackageJson({ + name: '@google-cloud/storage', + version: '0.1.0' + }); + + assert.strictEqual(userAgent, 'gcloud-node-storage/0.1.0'); + }); + }); }); diff --git a/packages/translate/src/index.js b/packages/translate/src/index.js index 5a3cf0633b9..1db4ca5cbdb 100644 --- a/packages/translate/src/index.js +++ b/packages/translate/src/index.js @@ -384,10 +384,7 @@ Translate.prototype.request = function(reqOpts, callback) { key: this.key }, headers: { - 'User-Agent': [ - PKG.name.replace('@google-cloud', 'gcloud-node'), - PKG.version - ].join('/') + 'User-Agent': common.util.getUserAgentFromPackageJson(PKG) } }); diff --git a/packages/translate/test/index.js b/packages/translate/test/index.js index dd1b7dd0298..bcd05f90ceb 100644 --- a/packages/translate/test/index.js +++ b/packages/translate/test/index.js @@ -21,8 +21,6 @@ var extend = require('extend'); var proxyquire = require('proxyquire'); var util = require('@google-cloud/common').util; -var PKG = require('../package.json'); - var makeRequestOverride; var fakeUtil = extend({}, util, { makeRequest: function() { @@ -386,6 +384,15 @@ describe('Translate', function() { describe('request', function() { it('should make the correct request', function(done) { + var userAgent = 'user-agent/0.0.0'; + + var getUserAgentFn = fakeUtil.getUserAgentFromPackageJson; + fakeUtil.getUserAgentFromPackageJson = function(packageJson) { + fakeUtil.getUserAgentFromPackageJson = getUserAgentFn; + assert.deepEqual(packageJson, require('../package.json')); + return userAgent; + }; + var reqOpts = { uri: '/test', a: 'b', @@ -401,7 +408,7 @@ describe('Translate', function() { key: translate.key }, headers: { - 'User-Agent': 'gcloud-node/translate/' + PKG.version + 'User-Agent': userAgent } }); var BASE_URL = 'https://www.googleapis.com/language/translate/v2'; From c2b364859c50e6087d8e81075badb290dd4066c1 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 9 Sep 2016 13:00:53 -0400 Subject: [PATCH 4/7] rm nl --- packages/common/test/grpc-service.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/common/test/grpc-service.js b/packages/common/test/grpc-service.js index e2a814ee856..f5c57a89f78 100644 --- a/packages/common/test/grpc-service.js +++ b/packages/common/test/grpc-service.js @@ -44,7 +44,6 @@ function fakeRetryRequest() { return (retryRequestOverride || retryRequest).apply(null, arguments); } - var GrpcMetadataOverride; var grpcLoadOverride; var fakeGrpc = { From a3eccbdbf4a6ffd6bf4d0615aca450f0c05734f2 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 9 Sep 2016 13:51:34 -0400 Subject: [PATCH 5/7] common: pull out common changes --- packages/common/src/grpc-service.js | 10 +---- packages/common/src/service.js | 5 --- packages/common/src/util.js | 27 +++++------- packages/common/test/grpc-service.js | 40 ++++------------- packages/common/test/service.js | 28 +----------- packages/common/test/util.js | 65 +++++++++++++++++++++++----- 6 files changed, 75 insertions(+), 100 deletions(-) diff --git a/packages/common/src/grpc-service.js b/packages/common/src/grpc-service.js index dde6a2401af..7919b640ae1 100644 --- a/packages/common/src/grpc-service.js +++ b/packages/common/src/grpc-service.js @@ -36,12 +36,6 @@ var through = require('through2'); */ var Service = require('./service.js'); -/** - * @type {module:common/util} - * @private - */ -var util = require('./util.js'); - /** * @const {object} - A map of protobuf codes to HTTP status codes. * @private @@ -176,7 +170,6 @@ function GrpcService(config, options) { } this.maxRetries = options.maxRetries; - this.userAgent = util.getUserAgentFromPackageJson(config.packageJson); var apiVersion = config.apiVersion; var service = this.service = config.service; @@ -739,8 +732,7 @@ GrpcService.prototype.getService_ = function(protoOpts) { if (!service) { service = new proto[protoOpts.service]( proto.baseUrl || this.baseUrl, - this.grpcCredentials, - { 'grpc.primary_user_agent': this.userAgent } + this.grpcCredentials ); this.activeServiceMap_.set(protoOpts.service, service); diff --git a/packages/common/src/service.js b/packages/common/src/service.js index 54aea3bd33e..75f738f948a 100644 --- a/packages/common/src/service.js +++ b/packages/common/src/service.js @@ -57,7 +57,6 @@ function Service(config, options) { this.getCredentials = this.makeAuthenticatedRequest.getCredentials; this.globalInterceptors = arrify(options.interceptors_); this.interceptors = []; - this.packageJson = config.packageJson; this.projectId = options.projectId; this.projectIdRequired = config.projectIdRequired !== false; } @@ -115,10 +114,6 @@ Service.prototype.request = function(reqOpts, callback) { delete reqOpts.interceptors_; - reqOpts.headers = extend({}, reqOpts.headers, { - 'User-Agent': util.getUserAgentFromPackageJson(this.packageJson) - }); - return this.makeAuthenticatedRequest(reqOpts, callback); }; diff --git a/packages/common/src/util.js b/packages/common/src/util.js index ac7ac524475..f279531aff7 100644 --- a/packages/common/src/util.js +++ b/packages/common/src/util.js @@ -38,6 +38,12 @@ var streamEvents = require('stream-events'); var through = require('through2'); var uniq = require('array-uniq'); +/** @const {object} @google-cloud/common's package.json file. */ +var PKG = require('../package.json'); + +/** @const {string} User agent. */ +var USER_AGENT = PKG.name + '/' + PKG.version; + var util = module.exports; var errorMessage = format([ @@ -460,6 +466,11 @@ util.makeRequest = makeRequest; */ function decorateRequest(reqOpts, config) { config = config || {}; + reqOpts.headers = reqOpts.headers || {}; + + var headers = reqOpts.headers; + var userAgent = headers['User-Agent'] || headers['user-agent']; + headers['User-Agent'] = userAgent || config.userAgent || USER_AGENT; if (is.object(reqOpts.qs)) { delete reqOpts.qs.autoPaginate; @@ -614,19 +625,3 @@ function isCustomType(unknown, module) { } util.isCustomType = isCustomType; - -/** - * Create a properly-formatted User-Agent string from a package.json file. - * - * @param {object} packageJson - A module's package.json file. - * @return {string} userAgent - The formatted User-Agent string. - */ -function getUserAgentFromPackageJson(packageJson) { - var hyphenatedPackageName = packageJson.name - .replace('@google-cloud', 'gcloud-node') // For legacy purposes. - .replace('/', '-'); // For UA spec-compliance purposes. - - return hyphenatedPackageName + '/' + packageJson.version; -} - -util.getUserAgentFromPackageJson = getUserAgentFromPackageJson; diff --git a/packages/common/test/grpc-service.js b/packages/common/test/grpc-service.js index f5c57a89f78..05392a49ea7 100644 --- a/packages/common/test/grpc-service.js +++ b/packages/common/test/grpc-service.js @@ -28,7 +28,6 @@ var sinon = require('sinon').sandbox.create(); var through = require('through2'); var util = require('../src/util.js'); -var fakeUtil = extend({}, util); function FakeService() { this.calledWith_ = arguments; @@ -93,10 +92,6 @@ describe('GrpcService', function() { proto: {}, service: 'Service', apiVersion: 'v1', - packageJson: { - name: '@google-cloud/service', - version: '0.2.0' - }, grpcMetadata: { property: 'value' } @@ -120,8 +115,7 @@ describe('GrpcService', function() { 'google-proto-files': fakeGoogleProtoFiles, 'retry-request': fakeRetryRequest, grpc: fakeGrpc, - './service.js': FakeService, - './util.js': fakeUtil + './service.js': FakeService }); GrpcServiceCached = extend(true, {}, GrpcService); }); @@ -249,12 +243,6 @@ describe('GrpcService', function() { assert.strictEqual(calledWith[1], OPTIONS); }); - it('should set insecure credentials if using customEndpoint', function() { - var config = extend({}, CONFIG, { customEndpoint: true }); - var grpcService = new GrpcService(config, OPTIONS); - assert.strictEqual(grpcService.grpcCredentials.name, 'createInsecure'); - }); - it('should default grpcMetadata to empty metadata', function() { var fakeGrpcMetadata = {}; @@ -289,20 +277,6 @@ describe('GrpcService', function() { assert.strictEqual(grpcService.maxRetries, OPTIONS.maxRetries); }); - it('should set the correct user-agent', function() { - var userAgent = 'user-agent/0.0.0'; - - var getUserAgentFn = fakeUtil.getUserAgentFromPackageJson; - fakeUtil.getUserAgentFromPackageJson = function(packageJson) { - fakeUtil.getUserAgentFromPackageJson = getUserAgentFn; - assert.strictEqual(packageJson, CONFIG.packageJson); - return userAgent; - }; - - var grpcService = new GrpcService(CONFIG, OPTIONS); - assert.strictEqual(grpcService.userAgent, userAgent); - }); - it('should get the root directory for the proto files', function(done) { googleProtoFilesOverride = function(path) { assert.strictEqual(path, '..'); @@ -313,6 +287,12 @@ describe('GrpcService', function() { new GrpcService(CONFIG, OPTIONS); }); + it('should set insecure credentials if using customEndpoint', function() { + var config = extend({}, CONFIG, { customEndpoint: true }); + var grpcService = new GrpcService(config, OPTIONS); + assert.strictEqual(grpcService.grpcCredentials.name, 'createInsecure'); + }); + it('should localize the service', function() { assert.strictEqual(grpcService.service, CONFIG.service); }); @@ -1518,13 +1498,9 @@ describe('GrpcService', function() { grpcService.protos = { Service: { - Service: function(baseUrl, grpcCredentials, userAgent) { + Service: function(baseUrl, grpcCredentials) { assert.strictEqual(baseUrl, grpcService.baseUrl); assert.strictEqual(grpcCredentials, grpcService.grpcCredentials); - assert.deepEqual(userAgent, { - 'grpc.primary_user_agent': grpcService.userAgent - }); - return fakeService; } } diff --git a/packages/common/test/service.js b/packages/common/test/service.js index f7ade0d0aba..b6202987aa5 100644 --- a/packages/common/test/service.js +++ b/packages/common/test/service.js @@ -39,11 +39,7 @@ describe('Service', function() { var CONFIG = { scopes: [], baseUrl: 'base-url', - projectIdRequired: false, - packageJson: { - name: '@google-cloud/service', - version: '0.2.0' - } + projectIdRequired: false }; var OPTIONS = { @@ -133,10 +129,6 @@ describe('Service', function() { assert.deepEqual(service.interceptors, []); }); - it('should localize package.json', function() { - assert.strictEqual(service.packageJson, CONFIG.packageJson); - }); - it('should localize the projectId', function() { assert.strictEqual(service.projectId, OPTIONS.projectId); }); @@ -220,24 +212,6 @@ describe('Service', function() { service.request(reqOpts, assert.ifError); }); - it('should add the User Agent', function(done) { - var userAgent = 'user-agent/0.0.0'; - - var getUserAgentFn = util.getUserAgentFromPackageJson; - util.getUserAgentFromPackageJson = function(packageJson) { - util.getUserAgentFromPackageJson = getUserAgentFn; - assert.strictEqual(packageJson, service.packageJson); - return userAgent; - }; - - service.makeAuthenticatedRequest = function(reqOpts) { - assert.strictEqual(reqOpts.headers['User-Agent'], userAgent); - done(); - }; - - service.request(reqOpts, assert.ifError); - }); - describe('projectIdRequired', function() { describe('false', function() { it('should include the projectId', function(done) { diff --git a/packages/common/test/util.js b/packages/common/test/util.js index 8ba26fef3dc..83c49f424ec 100644 --- a/packages/common/test/util.js +++ b/packages/common/test/util.js @@ -1153,6 +1153,60 @@ describe('common/util', function() { }); describe('decorateRequest', function() { + it('should keep user agent', function() { + var USER_AGENT = 'test/v1'; + + var reqOpts = { + a: 'b', + c: 'd', + headers: { + 'User-Agent': USER_AGENT + } + }; + + var expectedReqOpts = extend({}, reqOpts, { + headers: { + 'User-Agent': USER_AGENT + } + }); + + var decoratedReqOpts = util.decorateRequest(reqOpts); + assert.deepEqual(decoratedReqOpts, expectedReqOpts); + }); + + it('should add the user agent', function() { + var USER_AGENT = 'test/v2'; + + var reqOpts = { a: 'b', c: 'd' }; + + var expectedReqOpts = extend({}, reqOpts, { + headers: { + 'User-Agent': USER_AGENT + } + }); + + var decoratedReqOpts = util.decorateRequest(reqOpts, { + userAgent: USER_AGENT + }); + assert.deepEqual(decoratedReqOpts, expectedReqOpts); + }); + + it('should add the default user agent', function() { + var PKG = require('../package.json'); + var USER_AGENT = PKG.name + '/' + PKG.version; + + var reqOpts = { a: 'b', c: 'd' }; + + var expectedReqOpts = extend({}, reqOpts, { + headers: { + 'User-Agent': USER_AGENT + } + }); + + var decoratedReqOpts = util.decorateRequest(reqOpts); + assert.deepEqual(decoratedReqOpts, expectedReqOpts); + }); + it('should delete qs.autoPaginate', function() { var decoratedReqOpts = util.decorateRequest({ qs: { @@ -1363,15 +1417,4 @@ describe('common/util', function() { }); }); }); - - describe('getUserAgentFromPackageJson', function() { - it('should format a User Agent string from a package.json', function() { - var userAgent = util.getUserAgentFromPackageJson({ - name: '@google-cloud/storage', - version: '0.1.0' - }); - - assert.strictEqual(userAgent, 'gcloud-node-storage/0.1.0'); - }); - }); }); From 2a13718b0507e426d5db87009522f3c5ec25a846 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 9 Sep 2016 13:59:43 -0400 Subject: [PATCH 6/7] depend on @google-cloud/common@0.5.0 --- packages/bigquery/package.json | 2 +- packages/bigtable/package.json | 2 +- packages/compute/package.json | 2 +- packages/datastore/package.json | 2 +- packages/dns/package.json | 2 +- packages/language/package.json | 2 +- packages/logging/package.json | 2 +- packages/prediction/package.json | 2 +- packages/pubsub/package.json | 2 +- packages/resource/package.json | 2 +- packages/storage/package.json | 2 +- packages/translate/package.json | 2 +- packages/vision/package.json | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/bigquery/package.json b/packages/bigquery/package.json index 8f406993eda..f7351a68f10 100644 --- a/packages/bigquery/package.json +++ b/packages/bigquery/package.json @@ -50,7 +50,7 @@ "bigquery" ], "dependencies": { - "@google-cloud/common": "^0.4.0", + "@google-cloud/common": "^0.5.0", "arrify": "^1.0.0", "duplexify": "^3.2.0", "extend": "^3.0.0", diff --git a/packages/bigtable/package.json b/packages/bigtable/package.json index 901459a5d83..9d1eb93478b 100644 --- a/packages/bigtable/package.json +++ b/packages/bigtable/package.json @@ -50,7 +50,7 @@ "bigtable" ], "dependencies": { - "@google-cloud/common": "^0.4.0", + "@google-cloud/common": "^0.5.0", "arrify": "^1.0.0", "concat-stream": "^1.5.0", "create-error-class": "^3.0.2", diff --git a/packages/compute/package.json b/packages/compute/package.json index 77ca0ee8ab9..bfbc21288c7 100644 --- a/packages/compute/package.json +++ b/packages/compute/package.json @@ -50,7 +50,7 @@ "compute engine" ], "dependencies": { - "@google-cloud/common": "^0.1.0", + "@google-cloud/common": "^0.5.0", "arrify": "^1.0.0", "async": "^1.4.2", "create-error-class": "^2.0.1", diff --git a/packages/datastore/package.json b/packages/datastore/package.json index eed4f3a7e56..2ff94495a94 100644 --- a/packages/datastore/package.json +++ b/packages/datastore/package.json @@ -50,7 +50,7 @@ "datastore" ], "dependencies": { - "@google-cloud/common": "^0.4.0", + "@google-cloud/common": "^0.5.0", "arrify": "^1.0.0", "concat-stream": "^1.5.0", "create-error-class": "^3.0.2", diff --git a/packages/dns/package.json b/packages/dns/package.json index 0f95be64d53..4e98acf784f 100644 --- a/packages/dns/package.json +++ b/packages/dns/package.json @@ -50,7 +50,7 @@ "dns" ], "dependencies": { - "@google-cloud/common": "^0.1.0", + "@google-cloud/common": "^0.5.0", "arrify": "^1.0.0", "dns-zonefile": "0.1.18", "extend": "^3.0.0", diff --git a/packages/language/package.json b/packages/language/package.json index 4b847782336..0b82858c26b 100644 --- a/packages/language/package.json +++ b/packages/language/package.json @@ -52,7 +52,7 @@ "language" ], "dependencies": { - "@google-cloud/common": "^0.4.0", + "@google-cloud/common": "^0.5.0", "arguejs": "^0.2.3", "arrify": "^1.0.1", "extend": "^3.0.0", diff --git a/packages/logging/package.json b/packages/logging/package.json index 148c9afb519..2137fcc173e 100644 --- a/packages/logging/package.json +++ b/packages/logging/package.json @@ -52,7 +52,7 @@ "stackdriver" ], "dependencies": { - "@google-cloud/common": "^0.4.0", + "@google-cloud/common": "^0.5.0", "arguejs": "^0.2.3", "arrify": "^1.0.0", "extend": "^3.0.0", diff --git a/packages/prediction/package.json b/packages/prediction/package.json index e6739988f77..f888d087fa0 100644 --- a/packages/prediction/package.json +++ b/packages/prediction/package.json @@ -50,7 +50,7 @@ "prediction" ], "dependencies": { - "@google-cloud/common": "^0.1.0", + "@google-cloud/common": "^0.5.0", "JSONStream": "^1.0.7", "arrify": "^1.0.0", "extend": "^3.0.0", diff --git a/packages/pubsub/package.json b/packages/pubsub/package.json index cf6decc3391..8f9fed2b64b 100644 --- a/packages/pubsub/package.json +++ b/packages/pubsub/package.json @@ -50,7 +50,7 @@ "pubsub" ], "dependencies": { - "@google-cloud/common": "^0.4.0", + "@google-cloud/common": "^0.5.0", "arguejs": "^0.2.3", "arrify": "^1.0.0", "extend": "^3.0.0", diff --git a/packages/resource/package.json b/packages/resource/package.json index 37d969c7e4e..b9e35b85edc 100644 --- a/packages/resource/package.json +++ b/packages/resource/package.json @@ -50,7 +50,7 @@ "resource" ], "dependencies": { - "@google-cloud/common": "^0.1.0", + "@google-cloud/common": "^0.5.0", "extend": "^3.0.0", "is": "^3.0.1" }, diff --git a/packages/storage/package.json b/packages/storage/package.json index fae6ce61107..425b7c814c3 100644 --- a/packages/storage/package.json +++ b/packages/storage/package.json @@ -50,7 +50,7 @@ "storage" ], "dependencies": { - "@google-cloud/common": "^0.1.0", + "@google-cloud/common": "^0.5.0", "arrify": "^1.0.0", "async": "^1.4.2", "concat-stream": "^1.5.0", diff --git a/packages/translate/package.json b/packages/translate/package.json index 397f548e6e7..9de7d3b72dc 100644 --- a/packages/translate/package.json +++ b/packages/translate/package.json @@ -50,7 +50,7 @@ "translate" ], "dependencies": { - "@google-cloud/common": "^0.2.0", + "@google-cloud/common": "^0.5.0", "arrify": "^1.0.0", "extend": "^3.0.0", "is": "^3.0.1", diff --git a/packages/vision/package.json b/packages/vision/package.json index ab3040b3055..07f457bf0b8 100644 --- a/packages/vision/package.json +++ b/packages/vision/package.json @@ -50,7 +50,7 @@ "vision" ], "dependencies": { - "@google-cloud/common": "^0.3.0", + "@google-cloud/common": "^0.5.0", "arguejs": "^0.2.3", "arrify": "^1.0.0", "async": "^1.4.2", From e2851fc21efb6d564d82162475ff03d6c98e95e0 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Fri, 9 Sep 2016 14:58:08 -0400 Subject: [PATCH 7/7] add package.json to IAM --- packages/pubsub/src/iam.js | 3 ++- packages/pubsub/test/iam.js | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/pubsub/src/iam.js b/packages/pubsub/src/iam.js index 036e5e8751a..30501f8471d 100644 --- a/packages/pubsub/src/iam.js +++ b/packages/pubsub/src/iam.js @@ -73,7 +73,8 @@ function IAM(pubsub, id) { scopes: [ 'https://www.googleapis.com/auth/pubsub', 'https://www.googleapis.com/auth/cloud-platform' - ] + ], + packageJson: require('../package.json') }; this.id = id; diff --git a/packages/pubsub/test/iam.js b/packages/pubsub/test/iam.js index dc394497ddb..d13eda5c7d3 100644 --- a/packages/pubsub/test/iam.js +++ b/packages/pubsub/test/iam.js @@ -65,6 +65,7 @@ describe('IAM', function() { 'https://www.googleapis.com/auth/pubsub', 'https://www.googleapis.com/auth/cloud-platform' ]); + assert.deepEqual(config.packageJson, require('../package.json')); assert.strictEqual(options, PUBSUB.options); });