Skip to content

Commit

Permalink
Merge pull request #951 from stephenplusplus/spp--pubsub-subscribe-op…
Browse files Browse the repository at this point in the history
…tions

pubsub: pass options to pubsub.subscribe
  • Loading branch information
callmehiphop committed Nov 17, 2015
2 parents fc00eb5 + 0e80108 commit 55951bb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
16 changes: 8 additions & 8 deletions lib/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'use strict';

var arrify = require('arrify');
var extend = require('extend');
var is = require('is');
var nodeutil = require('util');

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down
29 changes: 26 additions & 3 deletions test/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 55951bb

Please sign in to comment.