diff --git a/lib/pubsub/index.js b/lib/pubsub/index.js index 672f2d1e7c8..3a4c745f1fb 100644 --- a/lib/pubsub/index.js +++ b/lib/pubsub/index.js @@ -21,6 +21,7 @@ 'use strict'; var arrify = require('arrify'); +var extend = require('extend'); var is = require('is'); var nodeutil = require('util'); @@ -380,7 +381,8 @@ PubSub.prototype.getTopics = function(query, callback) { * @param {module:pubsub/topic|string} - topic - The Topic to create a * subscription to. * @param {string} subName - The name of the subscription. - * @param {object=} options - Configuration object. + * @param {object=} options - See a + * [Subscription resource](https://cloud.google.com/pubsub/reference/rest/v1/projects.subscriptions) * @param {number} options.ackDeadlineSeconds - The maximum time after receiving * a message that you must ack a message before it is redelivered. * @param {boolean} options.autoAck - Automatically acknowledge the message once @@ -441,19 +443,17 @@ PubSub.prototype.subscribe = function(topic, subName, options, callback) { options = {}; } - options = options || {}; - if (is.string(topic)) { topic = this.topic(topic); } - var body = { + var body = extend(true, {}, options, { topic: topic.name - }; + }); - if (options.ackDeadlineSeconds) { - body.ackDeadlineSeconds = options.ackDeadlineSeconds; - } + delete body.autoAck; + delete body.interval; + delete body.reuseExisting; this.request({ method: 'PUT', diff --git a/test/pubsub/index.js b/test/pubsub/index.js index bd9bde925a8..2960a30b769 100644 --- a/test/pubsub/index.js +++ b/test/pubsub/index.js @@ -489,14 +489,37 @@ describe('PubSub', function() { }); it('should pass options to the api request', function(done) { - var opts = { ackDeadlineSeconds: 90 }; + var options = { + autoAck: true, + interval: 3, + reuseExisting: false, + ackDeadlineSeconds: 90, + pushConfig: { + pushEndpoint: 'https://domain/push' + } + }; + + var expectedBody = extend({}, options, { + topic: TOPIC_NAME + }); + + delete expectedBody.autoAck; + delete expectedBody.interval; + delete expectedBody.reuseExisting; + + pubsub.topic = function() { + return { + name: TOPIC_NAME + }; + }; pubsub.request = function(reqOpts) { - assert.strictEqual(reqOpts.json.ackDeadlineSeconds, 90); + assert.notStrictEqual(reqOpts.json, options); + assert.deepEqual(reqOpts.json, expectedBody); done(); }; - pubsub.subscribe(TOPIC_NAME, SUB_NAME, opts, assert.ifError); + pubsub.subscribe(TOPIC_NAME, SUB_NAME, options, assert.ifError); }); describe('error', function() {