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: type check input. fixes #500 #501

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 12 additions & 5 deletions lib/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Topic.formatMessage_ = function(message) {

/**
* Format the name of a topic. A Topic's full name is in the format of
* 'projects/{projectId}/topics/{topicName}.
* 'projects/{projectId}/topics/{topicName}'.
*
* @private
*
Expand All @@ -102,8 +102,8 @@ Topic.formatName_ = function(projectId, name) {
};

/**
* Wrapper for makeReq_ that automatically attempts to create a topic if it
* does not yet exist.
* Wrapper for makeReq_ that automatically attempts to create a topic if it does
* not yet exist.
*
* @private
*/
Expand All @@ -130,10 +130,11 @@ Topic.prototype.autoCreateWrapper_ = function(method, path, q, body, callback) {
};

/**
* Publish the provided message or array of messages. A message can be of any
* type. On success, an array of messageIds is returned in the response.
* Publish the provided message or array of messages. On success, an array of
* messageIds is returned in the response.
*
* @throws {Error} If no message is provided.
* @throws {Error} If a message is not an object.
*
* @param {object|object[]} message - The message(s) to publish.
* @param {*} message.data - The contents of the message.
Expand Down Expand Up @@ -178,6 +179,12 @@ Topic.prototype.publish = function(messages, callback) {
throw new Error('Cannot publish without a message.');
}

messages.forEach(function(message) {
if (!util.is(message, 'object')) {

This comment was marked as spam.

throw new Error('Cannot publish message:\n\t' + JSON.stringify(message));
}
});

callback = callback || util.noop;

var body = {
Expand Down
6 changes: 6 additions & 0 deletions test/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ describe('Topic', function() {
}, /Cannot publish/);
});

it('should throw if a message is not an object', function() {
assert.throws(function() {
topic.publish(message);
}, /Cannot publish message/);
});

it('should send correct api request', function(done) {
topic.makeReq_ = function(method, path, query, body) {
assert.equal(method, 'POST');
Expand Down