Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pubsub: pass options to pubsub.subscribe #951

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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