Skip to content

Commit

Permalink
pubsub: assure topic names are properly used in API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Dec 8, 2015
1 parent fd002e4 commit 012e025
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
12 changes: 6 additions & 6 deletions lib/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ var ServiceObject = require('../common/service-object.js');
function Topic(pubsub, name) {
var baseUrl = '/topics';

this.name = Topic.formatName_(pubsub.projectId, name);
this.pubsub = pubsub;
this.unformattedName = name.split('/').pop();

var methods = {
/**
* Create a topic.
Expand Down Expand Up @@ -140,7 +144,7 @@ function Topic(pubsub, name) {
ServiceObject.call(this, {
parent: pubsub,
baseUrl: baseUrl,
id: name,
id: this.unformattedName,
createMethod: pubsub.createTopic.bind(pubsub),
methods: methods
});
Expand Down Expand Up @@ -173,12 +177,8 @@ function Topic(pubsub, name) {
*/
this.iam = new IAM(pubsub, {
baseUrl: baseUrl,
id: name
id: this.unformattedName
});

this.name = Topic.formatName_(pubsub.projectId, name);
this.pubsub = pubsub;
this.unformattedName = name;
}

nodeutil.inherits(Topic, ServiceObject);
Expand Down
23 changes: 13 additions & 10 deletions test/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ function FakeServiceObject() {

nodeutil.inherits(FakeServiceObject, ServiceObject);

describe('Topic', function() {
describe.only('Topic', function() {
var Topic;
var topic;

var PROJECT_ID = 'test-project';
var TOPIC_NAME = 'test-topic';
var TOPIC_NAME = 'projects/' + PROJECT_ID + '/topics/test-topic';
var TOPIC_UNFORMATTED_NAME = TOPIC_NAME.split('/').pop();
var PUBSUB = {
projectId: PROJECT_ID,
createTopic: util.noop
Expand Down Expand Up @@ -85,7 +86,7 @@ describe('Topic', function() {

assert.strictEqual(calledWith.parent, pubsubInstance);
assert.strictEqual(calledWith.baseUrl, '/topics');
assert.strictEqual(calledWith.id, TOPIC_NAME);
assert.strictEqual(calledWith.id, TOPIC_UNFORMATTED_NAME);
assert.deepEqual(calledWith.methods, {
create: true,
delete: true,
Expand All @@ -100,7 +101,7 @@ describe('Topic', function() {
PUBSUB,
{
baseUrl: '/topics',
id: TOPIC_NAME
id: TOPIC_UNFORMATTED_NAME
}
]);
});
Expand All @@ -117,6 +118,10 @@ describe('Topic', function() {
it('should assign pubsub object to `this`', function() {
assert.deepEqual(topic.pubsub, PUBSUB);
});

it('should localize the unformatted name', function() {
assert.strictEqual(topic.unformattedName, TOPIC_UNFORMATTED_NAME);
});
});

describe('formatMessage_', function() {
Expand All @@ -142,16 +147,14 @@ describe('Topic', function() {
});

describe('formatName_', function() {
var fullName = 'projects/' + PROJECT_ID + '/topics/' + TOPIC_NAME;

it('should format name', function() {
var formattedName = Topic.formatName_(PROJECT_ID, TOPIC_NAME);
assert.equal(formattedName, fullName);
var formattedName = Topic.formatName_(PROJECT_ID, TOPIC_UNFORMATTED_NAME);
assert.equal(formattedName, TOPIC_NAME);
});

it('should format name when given a complete name', function() {
var formattedName = Topic.formatName_(PROJECT_ID, fullName);
assert.equal(formattedName, fullName);
var formattedName = Topic.formatName_(PROJECT_ID, TOPIC_NAME);
assert.equal(formattedName, TOPIC_NAME);
});
});

Expand Down

0 comments on commit 012e025

Please sign in to comment.