Skip to content

Commit e7e5cfc

Browse files
committed
Support UTF-8 characters in topic.
1 parent f30b93b commit e7e5cfc

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

lib/generate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ module.exports.subscribe = function(opts) {
302302
return new Error('Invalid subscriptions - invalid qos');
303303
}
304304

305-
length += topic.length + 2 + 1;
305+
length += Buffer.byteLength(topic) + 2 + 1;
306306
}
307307
} else {
308308
return new Error('Invalid subscriptions');
@@ -405,7 +405,7 @@ module.exports.unsubscribe = function(opts) {
405405
if ('string' !== typeof unsubs[i]) {
406406
return new Error('Invalid unsubscriptions');
407407
}
408-
length += unsubs[i].length + 2;
408+
length += Buffer.byteLength(unsubs[i]) + 2;
409409
}
410410
} else {
411411
return new Error('Invalid unsubscriptions');

test/abstract_client.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,22 @@ module.exports = function(server, createClient, port) {
310310
client.publish('a', 'b', opts, done);
311311
});
312312
});
313+
314+
it('should support UTF-8 characters in topic', function(done) {
315+
var client = createClient(port);
316+
317+
client.once('connect', function() {
318+
client.publish('中国', 'hello', done);
319+
});
320+
})
321+
322+
it('should support UTF-8 characters in payload', function(done) {
323+
var client = createClient(port);
324+
325+
client.once('connect', function() {
326+
client.publish('hello', '中国', done);
327+
});
328+
})
313329
});
314330

315331
describe('unsubscribing', function() {
@@ -374,6 +390,22 @@ module.exports = function(server, createClient, port) {
374390
});
375391
});
376392
});
393+
394+
it('should unsubscribe from a chinese topic', function(done) {
395+
var client = createClient(port);
396+
var topic = '中国';
397+
398+
client.once('connect', function() {
399+
client.unsubscribe(topic);
400+
});
401+
402+
server.once('client', function(client) {
403+
client.once('unsubscribe', function(packet) {
404+
packet.unsubscriptions.should.containEql(topic);
405+
done();
406+
});
407+
});
408+
});
377409
});
378410

379411
describe('pinging', function () {
@@ -427,6 +459,7 @@ module.exports = function(server, createClient, port) {
427459
});
428460
});
429461
});
462+
430463
it('should send a subscribe message', function(done) {
431464
var client = createClient(port);
432465

@@ -506,6 +539,26 @@ module.exports = function(server, createClient, port) {
506539
});
507540
});
508541
});
542+
543+
it('should subscribe with a chinese topic', function(done) {
544+
var client = createClient(port);
545+
546+
var topic = '中国';
547+
548+
client.once('connect', function() {
549+
client.subscribe(topic);
550+
});
551+
552+
server.once('client', function(client) {
553+
client.once('subscribe', function(packet) {
554+
packet.subscriptions.should.containEql({
555+
topic: topic,
556+
qos: 0
557+
});
558+
done();
559+
});
560+
});
561+
});
509562
});
510563

511564
describe('receiving messages', function() {

0 commit comments

Comments
 (0)