Skip to content

Commit 5464662

Browse files
authored
Merge pull request #45 from mqttjs/will-payload-fix
will payload and properties fix + test
2 parents bb9afc9 + c4793e5 commit 5464662

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"Matteo Collina <[email protected]> (https://github.com/mcollina)",
99
"Adam Rudd <[email protected]>",
1010
"Peter Sorowka (https://github.com/psorowka)",
11-
"Wouter Klijn <[email protected]> (https://github.com/wuhkuh)"
11+
"Wouter Klijn <[email protected]> (https://github.com/wuhkuh)",
12+
"Siarhei Buntsevich (https://github.com/scarry1992)"
1213
],
1314
"scripts": {
1415
"test": "tape test.js | tap-spec && standard",

test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,79 @@ testParseGenerate('connect MQTT 5.0', {
260260
4, 3, 2, 1// Will payload
261261
]))
262262

263+
testParseGenerate('connect MQTT 5.0 with will properties but w/o will payload', {
264+
cmd: 'connect',
265+
retain: false,
266+
qos: 0,
267+
dup: false,
268+
length: 121,
269+
protocolId: 'MQTT',
270+
protocolVersion: 5,
271+
will: {
272+
retain: true,
273+
qos: 2,
274+
properties: {
275+
willDelayInterval: 1234,
276+
payloadFormatIndicator: false,
277+
messageExpiryInterval: 4321,
278+
contentType: 'test',
279+
responseTopic: 'topic',
280+
correlationData: Buffer.from([1, 2, 3, 4]),
281+
userProperties: {
282+
'test': 'test'
283+
}
284+
},
285+
topic: 'topic',
286+
payload: Buffer.from([])
287+
},
288+
clean: true,
289+
keepalive: 30,
290+
properties: {
291+
sessionExpiryInterval: 1234,
292+
receiveMaximum: 432,
293+
maximumPacketSize: 100,
294+
topicAliasMaximum: 456,
295+
requestResponseInformation: true,
296+
requestProblemInformation: true,
297+
userProperties: {
298+
'test': 'test'
299+
},
300+
authenticationMethod: 'test',
301+
authenticationData: Buffer.from([1, 2, 3, 4])
302+
},
303+
clientId: 'test'
304+
}, Buffer.from([
305+
16, 121, // Header
306+
0, 4, // Protocol ID length
307+
77, 81, 84, 84, // Protocol ID
308+
5, // Protocol version
309+
54, // Connect flags
310+
0, 30, // Keepalive
311+
47, // properties length
312+
17, 0, 0, 4, 210, // sessionExpiryInterval
313+
33, 1, 176, // receiveMaximum
314+
39, 0, 0, 0, 100, // maximumPacketSize
315+
34, 1, 200, // topicAliasMaximum
316+
25, 1, // requestResponseInformation
317+
23, 1, // requestProblemInformation,
318+
38, 0, 4, 116, 101, 115, 116, 0, 4, 116, 101, 115, 116, // userProperties,
319+
21, 0, 4, 116, 101, 115, 116, // authenticationMethod
320+
22, 0, 4, 1, 2, 3, 4, // authenticationData
321+
0, 4, // Client ID length
322+
116, 101, 115, 116, // Client ID
323+
47, // will properties
324+
24, 0, 0, 4, 210, // will delay interval
325+
1, 0, // payload format indicator
326+
2, 0, 0, 16, 225, // message expiry interval
327+
3, 0, 4, 116, 101, 115, 116, // content type
328+
8, 0, 5, 116, 111, 112, 105, 99, // response topic
329+
9, 0, 4, 1, 2, 3, 4, // corelation data
330+
38, 0, 4, 116, 101, 115, 116, 0, 4, 116, 101, 115, 116, // user properties
331+
0, 5, // Will topic length
332+
116, 111, 112, 105, 99, // Will topic
333+
0, 0 // Will payload length
334+
]))
335+
263336
testParseGenerate('connect MQTT 5.0 w/o will properties', {
264337
cmd: 'connect',
265338
retain: false,

writeToStream.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ function connect (packet, stream, opts) {
160160
}
161161

162162
// Payload
163+
length += 2 // payload length
163164
if (will.payload) {
164165
if (will.payload.length >= 0) {
165166
if (typeof will.payload === 'string') {
166-
length += Buffer.byteLength(will.payload) + 2
167+
length += Buffer.byteLength(will.payload)
167168
} else {
168-
length += will.payload.length + 2
169+
length += will.payload.length
169170
}
170171
} else {
171172
stream.emit('error', new Error('Invalid will payload'))

0 commit comments

Comments
 (0)