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: assure topic names are properly used in API requests #1008

Merged
merged 1 commit into from
Dec 8, 2015
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
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
21 changes: 12 additions & 9 deletions test/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ describe('Topic', function() {
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