-
Notifications
You must be signed in to change notification settings - Fork 11
/
observe-012-reregistration-failure.js
223 lines (192 loc) · 7.73 KB
/
observe-012-reregistration-failure.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
/*
Handle a failed re-registration of an observer after Max-Age ends:
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
------------------------------------------------------------------------------
Observe : 0
Uri-Path: observer
Uri-Path: expire
==============================================================================
2. Server adds the #1 request as an observer and sends a piggy-backed response:
==============================================================================
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
------------------------------------------------------------------------------
Observe : 1
Content-Format: text/plain;charset=utf-8
Max-Age : 5
------------------------------------------------------------------------------
Payload (1 byte)
1
==============================================================================
3. Client receives the #2 response. The #1 request emits the `acknowledged`
event and the `response` event.
4. After 2s, server sends the first notification:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 1 0 0 0 1 0 1 | 0x4321
1 | CON | 0 bytes | 2.05 Content | 17185
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Observe : 2
Content-Format: text/plain;charset=utf-8
Max-Age : 5
------------------------------------------------------------------------------
Payload (1 byte)
2
==============================================================================
5. Client receives the #4 notification and sends an acknowledgement:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0x4321
1 | ACK | 0 bytes | Empty | 17185
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
==============================================================================
6. The #1 request emits the `response` event.
7. After Max-Age ends (5s), client emits the `exchange timeout` event and
the #1 request emits the `timeout` event.
8. Client resends the #1 request with an updated message ID and token
(a new exchange is created):
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 1 | 0 0 0 0 0 0 0 1 | 0x0002
1 | CON | 1 byte | GET | 2
------------------------------------------------------------------------------
Token | Remote Endpoint
01 | 127.0.0.1
------------------------------------------------------------------------------
Observe : 0
Uri-Path: observer
Uri-Path: expire
==============================================================================
9. Server reboots with Observe support disabled...
10. Server receives the #8 request and sends a piggy-backed response, but
without an Observe option:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 1 | 0 1 0 0 0 1 0 1 | 0x0002
1 | ACK | 1 byte | 2.05 Content | 2
------------------------------------------------------------------------------
Token | Remote Endpoint
01 | 127.0.0.1
------------------------------------------------------------------------------
Content-Format: text/plain;charset=utf-8
------------------------------------------------------------------------------
Payload (1 byte)
3
==============================================================================
11. Client receives the #10 response. The #8 request emits the `acknowledged`
event, the `response` event and the `cancelled` event.
*/
'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 expectedRequest1 = {
type: Message.Type.CON,
code: Message.Code.GET,
id: 0x0001,
token: new Buffer([]),
uri: '/observer/expire',
observe: 0
};
var expectedResponse1 = {
type: Message.Type.ACK,
code: Message.Code.CONTENT,
id: expectedRequest1.id,
token: expectedRequest1.token,
contentFormat: 'text/plain;charset=utf-8',
maxAge: 5,
observe: 1,
payload: new Buffer('1')
};
var expectedNotification1 = {
type: Message.Type.CON,
code: Message.Code.CONTENT,
id: 0x4321,
token: expectedRequest1.token,
contentFormat: 'text/plain;charset=utf-8',
maxAge: 5,
observe: 2,
payload: new Buffer('2')
};
var expectedNotificationAck1 = {
type: Message.Type.ACK,
code: Message.Type.EMPTY,
id: expectedNotification1.id
};
var expectedRequest2 = {
type: Message.Type.CON,
code: Message.Code.GET,
id: 0x0002,
token: new Buffer([0x01]),
uri: '/observer/expire',
observe: 0
};
var expectedResponse2 = {
type: Message.Type.ACK,
code: Message.Code.CONTENT,
id: expectedRequest2.id,
token: expectedRequest2.token,
contentFormat: 'text/plain;charset=utf-8',
payload: new Buffer('2')
};
ctx.socket.expectRequest(expectedRequest1);
ctx.socket.scheduleResponse(50, expectedResponse1);
ctx.socket.scheduleResponse(2050, expectedNotification1);
ctx.socket.expectRequest(2050, expectedNotificationAck1);
ctx.socket.expectRequest(7050, expectedRequest2);
ctx.socket.scheduleResponse(7100, expectedResponse2);
var req = ctx.client.request(Message.fromObject(expectedRequest1));
var eventSpy = sinon.spy(req, 'emit');
ctx.clock.tick(3600000);
return function assert()
{
ctx.socket.assert();
sinon.assert.callCount(eventSpy, 7);
sinon.assert.calledWith(
eventSpy, 'acknowledged', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(
eventSpy, 'response', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(eventSpy, 'cancelled');
var c = -1;
eventSpy.args[++c][0].should.be.equal('acknowledged');
sinon.assert.coapMessage(
eventSpy.args[c][1], expectedResponse1, "Invalid ACK #1."
);
eventSpy.args[++c][0].should.be.equal('response');
sinon.assert.coapMessage(
eventSpy.args[c][1], expectedResponse1, "Invalid `response` #1."
);
eventSpy.args[++c][0].should.be.equal('response');
sinon.assert.coapMessage(
eventSpy.args[c][1], expectedNotification1, "Invalid notification #1."
);
eventSpy.args[++c][0].should.be.equal('timeout');
eventSpy.args[++c][0].should.be.equal('acknowledged');
sinon.assert.coapMessage(
eventSpy.args[c][1], expectedResponse2, "Invalid ACK #2."
);
eventSpy.args[++c][0].should.be.equal('response');
sinon.assert.coapMessage(
eventSpy.args[c][1], expectedResponse2, "Invalid `response` #2."
);
eventSpy.args[++c][0].should.be.equal('cancelled');
};
});