Skip to content

Commit 5549066

Browse files
committed
Merge pull request #1008 from stephenplusplus/spp--pubsub-1005
pubsub: assure topic names are properly used in API requests
2 parents fd002e4 + c1bfd64 commit 5549066

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

lib/pubsub/topic.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ var ServiceObject = require('../common/service-object.js');
6464
function Topic(pubsub, name) {
6565
var baseUrl = '/topics';
6666

67+
this.name = Topic.formatName_(pubsub.projectId, name);
68+
this.pubsub = pubsub;
69+
this.unformattedName = name.split('/').pop();
70+
6771
var methods = {
6872
/**
6973
* Create a topic.
@@ -140,7 +144,7 @@ function Topic(pubsub, name) {
140144
ServiceObject.call(this, {
141145
parent: pubsub,
142146
baseUrl: baseUrl,
143-
id: name,
147+
id: this.unformattedName,
144148
createMethod: pubsub.createTopic.bind(pubsub),
145149
methods: methods
146150
});
@@ -173,12 +177,8 @@ function Topic(pubsub, name) {
173177
*/
174178
this.iam = new IAM(pubsub, {
175179
baseUrl: baseUrl,
176-
id: name
180+
id: this.unformattedName
177181
});
178-
179-
this.name = Topic.formatName_(pubsub.projectId, name);
180-
this.pubsub = pubsub;
181-
this.unformattedName = name;
182182
}
183183

184184
nodeutil.inherits(Topic, ServiceObject);

test/pubsub/topic.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ describe('Topic', function() {
4040
var topic;
4141

4242
var PROJECT_ID = 'test-project';
43-
var TOPIC_NAME = 'test-topic';
43+
var TOPIC_NAME = 'projects/' + PROJECT_ID + '/topics/test-topic';
44+
var TOPIC_UNFORMATTED_NAME = TOPIC_NAME.split('/').pop();
4445
var PUBSUB = {
4546
projectId: PROJECT_ID,
4647
createTopic: util.noop
@@ -85,7 +86,7 @@ describe('Topic', function() {
8586

8687
assert.strictEqual(calledWith.parent, pubsubInstance);
8788
assert.strictEqual(calledWith.baseUrl, '/topics');
88-
assert.strictEqual(calledWith.id, TOPIC_NAME);
89+
assert.strictEqual(calledWith.id, TOPIC_UNFORMATTED_NAME);
8990
assert.deepEqual(calledWith.methods, {
9091
create: true,
9192
delete: true,
@@ -100,7 +101,7 @@ describe('Topic', function() {
100101
PUBSUB,
101102
{
102103
baseUrl: '/topics',
103-
id: TOPIC_NAME
104+
id: TOPIC_UNFORMATTED_NAME
104105
}
105106
]);
106107
});
@@ -117,6 +118,10 @@ describe('Topic', function() {
117118
it('should assign pubsub object to `this`', function() {
118119
assert.deepEqual(topic.pubsub, PUBSUB);
119120
});
121+
122+
it('should localize the unformatted name', function() {
123+
assert.strictEqual(topic.unformattedName, TOPIC_UNFORMATTED_NAME);
124+
});
120125
});
121126

122127
describe('formatMessage_', function() {
@@ -142,16 +147,14 @@ describe('Topic', function() {
142147
});
143148

144149
describe('formatName_', function() {
145-
var fullName = 'projects/' + PROJECT_ID + '/topics/' + TOPIC_NAME;
146-
147150
it('should format name', function() {
148-
var formattedName = Topic.formatName_(PROJECT_ID, TOPIC_NAME);
149-
assert.equal(formattedName, fullName);
151+
var formattedName = Topic.formatName_(PROJECT_ID, TOPIC_UNFORMATTED_NAME);
152+
assert.equal(formattedName, TOPIC_NAME);
150153
});
151154

152155
it('should format name when given a complete name', function() {
153-
var formattedName = Topic.formatName_(PROJECT_ID, fullName);
154-
assert.equal(formattedName, fullName);
156+
var formattedName = Topic.formatName_(PROJECT_ID, TOPIC_NAME);
157+
assert.equal(formattedName, TOPIC_NAME);
155158
});
156159
});
157160

0 commit comments

Comments
 (0)