-
Notifications
You must be signed in to change notification settings - Fork 11
/
block-003-get-late-negotiation.js
304 lines (268 loc) · 11.5 KB
/
block-003-get-late-negotiation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
Handle a blockwise response of an unacceptable size to a confirmable GET
request without a Block2 option:
1. Client sends a request:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0001
1 | CON | 0 bytes | GET | 1
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path: blocks
Uri-Path: get
==============================================================================
2. Server sends the first block of size 128:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 1 | 0x0001
1 | ACK | 0 bytes | 2.05 Content | 1
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Content-Format: text/plain;charset=utf-8
Block2 : NUM: 0, M: true, SZX: 3 (128 bytes)
------------------------------------------------------------------------------
Payload (64 bytes)
|-------------------------------------------------------------|
| BLOCK 1 |
==============================================================================
3. Client receives the #2 block. Request emits the `acknowledged` event and
the `block received` event.
4. Client sends a request for the third block (not the second one, because
the block size specified by the server (128) is too big for the client (64)):
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0002
1 | CON | 0 bytes | GET | 2
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path: blocks
Uri-Path: get
Block2 : NUM: 2, M: false, SZX: 2 (64 bytes)
==============================================================================
5. Servers responds with the third block of size 64:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 1 | 0x0002
1 | ACK | 0 bytes | 2.05 Content | 2
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Content-Format: text/plain;charset=utf-8
Block2 : NUM: 2, M: true, SZX: 2 (64 bytes)
------------------------------------------------------------------------------
Payload (64 bytes)
|-------------------------------------------------------------|
==============================================================================
6. Client receives the #5 block. Request emits the `block received` event.
7. Client sends a request for the fourth block of size 64:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0003
1 | CON | 0 bytes | GET | 3
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path: blocks
Uri-Path: get
Block2 : NUM: 3, M: false, SZX: 2 (64 bytes)
==============================================================================
8. Server responds with the fourth block of size 64:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 1 | 0x0003
1 | ACK | 0 bytes | 2.05 Content | 3
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Content-Format: text/plain;charset=utf-8
Block2 : NUM: 3, M: true, SZX: 2 (64 bytes)
------------------------------------------------------------------------------
Payload (64 bytes)
| BLOCK 2 |
==============================================================================
9. Client receives the #8 block. Request emits the `block received` event.
10. Client sends a request for the fifth block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0004
1 | CON | 0 bytes | GET | 4
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path: blocks
Uri-Path: get
Block2 : NUM: 4, M: false, SZX: 2 (64 bytes)
==============================================================================
11. Server responds with the fifth block of size 64:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 1 | 0x0004
1 | ACK | 0 bytes | 2.05 Content | 4
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Content-Format: text/plain;charset=utf-8
Block2 : NUM: 4, M: false, SZX: 2 (64 bytes)
------------------------------------------------------------------------------
Payload (63 bytes)
|-------------------------------------------------------------|
==============================================================================
12. Client receives the #11 block. Request emits the `block received` event.
13. Client recognizes that the #11 block was the last one, and so the request
emits the `response` event with a new, combined response message.
*/
'use strict';
require('should');
var sinon = require('sinon');
var helpers = require('../helpers');
var Message = require(helpers.LIB_DIR).Message;
helpers.test(__filename, function(ctx)
{
/*jshint maxstatements:999*/
var expectedRequest = {
type: Message.Type.CON,
code: Message.Code.GET,
uri: '/blocks/get'
};
var expectedResWithBlock0 = {
type: Message.Type.ACK,
code: Message.Code.CONTENT,
id: 0x0001,
block2: {num: 0, m: true, size: 128},
contentFormat: 'text/plain;charset=utf-8',
payload: new Buffer(
'|-------------------------------------------------------------|\n' +
'| BLOCK 1 |\n'
)
};
var expectedReqForBlock2 = {
type: Message.Type.CON,
code: Message.Code.GET,
id: 0x0002,
uri: '/blocks/get',
block2: {num: 2, m: false, size: 64}
};
var expectedResWithBlock2 = {
type: Message.Type.ACK,
code: Message.Code.CONTENT,
id: expectedReqForBlock2.id,
block2: {num: 2, m: true, size: 64},
contentFormat: 'text/plain;charset=utf-8',
payload: new Buffer(
'|-------------------------------------------------------------|\n'
)
};
var expectedReqForBlock3 = {
type: Message.Type.CON,
code: Message.Code.GET,
id: 0x0003,
uri: '/blocks/get',
block2: {num: 3, m: false, size: 64}
};
var expectedResWithBlock3 = {
type: Message.Type.ACK,
code: Message.Code.CONTENT,
id: expectedReqForBlock3.id,
block2: {num: 3, m: true, size: 64},
contentFormat: 'text/plain;charset=utf-8',
payload: new Buffer(
'| BLOCK 2 |\n'
)
};
var expectedReqForBlock4 = {
type: Message.Type.CON,
code: Message.Code.GET,
id: 0x0004,
uri: '/blocks/get',
block2: {num: 4, m: false, size: 64}
};
var expectedResWithBlock4 = {
type: Message.Type.ACK,
code: Message.Code.CONTENT,
id: expectedReqForBlock4.id,
block2: {num: 4, m: false, size: 64},
contentFormat: 'text/plain;charset=utf-8',
payload: new Buffer(
'|-------------------------------------------------------------|'
)
};
var expectedResponse = {
type: expectedResWithBlock4.type,
code: expectedResWithBlock4.code,
id: expectedResWithBlock4.id,
block2: expectedResWithBlock4.block2,
contentFormat: expectedResWithBlock4.contentFormat,
payload: new Buffer(
'|-------------------------------------------------------------|\n' +
'| BLOCK 1 |\n' +
'|-------------------------------------------------------------|\n' +
'| BLOCK 2 |\n' +
'|-------------------------------------------------------------|'
)
};
ctx.socket.expectRequest(expectedRequest);
ctx.socket.scheduleResponse(50, expectedResWithBlock0);
ctx.socket.expectRequest(50, expectedReqForBlock2);
ctx.socket.scheduleResponse(100, expectedResWithBlock2);
ctx.socket.expectRequest(100, expectedReqForBlock3);
ctx.socket.scheduleResponse(150, expectedResWithBlock3);
ctx.socket.expectRequest(150, expectedReqForBlock4);
ctx.socket.scheduleResponse(200, expectedResWithBlock4);
var req = ctx.client.request(Message.fromObject(expectedRequest), {
blockSize: 64
});
var eventSpy = sinon.spy(req, 'emit');
ctx.clock.tick(3600000);
return function assert()
{
ctx.socket.assert();
sinon.assert.callCount(eventSpy, 6);
sinon.assert.calledWith(
eventSpy, 'acknowledged', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(
eventSpy, 'block received', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(
eventSpy, 'response', sinon.match.instanceOf(Message)
);
eventSpy.args[0][0].should.be.equal('acknowledged');
sinon.assert.coapMessage(
eventSpy.args[0][1], expectedResWithBlock0, "Invalid ACK."
);
eventSpy.args[1][0].should.be.equal('block received');
sinon.assert.coapMessage(
eventSpy.args[1][1],
expectedResWithBlock0,
"Invalid `block received` (#1)."
);
eventSpy.args[2][0].should.be.equal('block received');
sinon.assert.coapMessage(
eventSpy.args[2][1],
expectedResWithBlock2,
"Invalid `block received` (#2)."
);
eventSpy.args[3][0].should.be.equal('block received');
sinon.assert.coapMessage(
eventSpy.args[3][1],
expectedResWithBlock3,
"Invalid `block received` (#3)."
);
eventSpy.args[4][0].should.be.equal('block received');
sinon.assert.coapMessage(
eventSpy.args[4][1],
expectedResWithBlock4,
"Invalid `block received` (#4)."
);
eventSpy.args[5][0].should.be.equal('response');
sinon.assert.coapMessage(
eventSpy.args[5][1], expectedResponse, "Invalid `response`."
);
var lastBlockReceived = eventSpy.args[4][1];
var response = eventSpy.args[5][1];
response.should.not.be.equal(lastBlockReceived);
};
});