Skip to content

Commit

Permalink
pubsub: type check input. fixes #500
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Apr 14, 2015
1 parent 3ce39f6 commit b6ba3e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
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')) {
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

0 comments on commit b6ba3e9

Please sign in to comment.