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

Support UTF-8 characters in topic. #201

Merged
merged 2 commits into from
Jun 21, 2014
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
8 changes: 4 additions & 4 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ module.exports.publish = function(opts) {
if (!topic || 'string' !== typeof topic) {
return new Error('Invalid topic');
} else {
length += topic.length + 2;
length += Buffer.byteLength(topic) + 2;
}

// get the payload length
Expand All @@ -194,7 +194,7 @@ module.exports.publish = function(opts) {
} else {
length += payload.length;
}

// Message id must a number if qos > 0
if (qos && 'number' !== typeof id) {
return new Error('Invalid message id')
Expand Down Expand Up @@ -302,7 +302,7 @@ module.exports.subscribe = function(opts) {
return new Error('Invalid subscriptions - invalid qos');
}

length += topic.length + 2 + 1;
length += Buffer.byteLength(topic) + 2 + 1;
}
} else {
return new Error('Invalid subscriptions');
Expand Down Expand Up @@ -405,7 +405,7 @@ module.exports.unsubscribe = function(opts) {
if ('string' !== typeof unsubs[i]) {
return new Error('Invalid unsubscriptions');
}
length += unsubs[i].length + 2;
length += Buffer.byteLength(unsubs[i]) + 2;
}
} else {
return new Error('Invalid unsubscriptions');
Expand Down
80 changes: 80 additions & 0 deletions test/abstract_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,22 @@ module.exports = function(server, createClient, port) {
client.publish('a', 'b', opts, done);
});
});

it('should support UTF-8 characters in topic', function(done) {
var client = createClient(port);

client.once('connect', function() {
client.publish('中国', 'hello', done);
});
})

it('should support UTF-8 characters in payload', function(done) {
var client = createClient(port);

client.once('connect', function() {
client.publish('hello', '中国', done);
});
})
});

describe('unsubscribing', function() {
Expand Down Expand Up @@ -374,6 +390,22 @@ module.exports = function(server, createClient, port) {
});
});
});

it('should unsubscribe from a chinese topic', function(done) {
var client = createClient(port);
var topic = '中国';

client.once('connect', function() {
client.unsubscribe(topic);
});

server.once('client', function(client) {
client.once('unsubscribe', function(packet) {
packet.unsubscriptions.should.containEql(topic);
done();
});
});
});
});

describe('pinging', function () {
Expand Down Expand Up @@ -427,6 +459,7 @@ module.exports = function(server, createClient, port) {
});
});
});

it('should send a subscribe message', function(done) {
var client = createClient(port);

Expand Down Expand Up @@ -506,6 +539,26 @@ module.exports = function(server, createClient, port) {
});
});
});

it('should subscribe with a chinese topic', function(done) {
var client = createClient(port);

var topic = '中国';

client.once('connect', function() {
client.subscribe(topic);
});

server.once('client', function(client) {
client.once('subscribe', function(packet) {
packet.subscriptions.should.containEql({
topic: topic,
qos: 0
});
done();
});
});
});
});

describe('receiving messages', function() {
Expand Down Expand Up @@ -589,6 +642,33 @@ module.exports = function(server, createClient, port) {
});
});
});

it('should support chinese topic', function(done) {
var client = createClient(port, { encoding: 'binary' })
, testPacket = {
topic: '国',
payload: 'message',
retain: true,
qos: 1,
messageId: 5
};

client.subscribe(testPacket.topic);
client.once('message',
function(topic, message, packet) {
topic.should.equal(testPacket.topic);
message.should.be.an.instanceOf(Buffer);
message.toString().should.equal(testPacket.payload);
packet.should.equal(packet);
done();
});

server.once('client', function(client) {
client.on('subscribe', function(packet) {
client.publish(testPacket);
});
});
});
});

describe('qos handling', function() {
Expand Down