forked from coapjs/coap-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
262 lines (212 loc) · 6.92 KB
/
test.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
var expect = require('chai').expect
, coap = require('coap')
, concat = require('concat-stream')
, spawn = require('child_process').spawn
, replace = require('event-stream').replace
describe('coap', function() {
var server
before(function(done) {
server = coap.createServer()
// CoAP default port
server.listen(5683, done)
})
after(function() {
server.close()
})
function call() {
var child = spawn('node', ['index.js'].concat(Array.prototype.slice.call(arguments)))
// piping child.stderr to stdout but filtering out the status codes
child.stderr
.pipe(replace('\x1b[1m(4.04)\x1b[0m\n', ''))
.pipe(replace('\x1b[1m(2.05)\x1b[0m\n', ''))
.pipe(process.stdout);
return child
}
it('should error with a wrong URL', function(done) {
call('abcde').stdout.pipe(concat(function(data) {
expect(data.toString()).to.eql('Wrong URL. Protocol is not coap or no hostname found.\n')
done()
}))
})
it('should GET a given resource by default', function(done) {
var child = call('coap://localhost')
child.stderr.pipe(concat(function(data) {
expect(data.toString()).to.eql('\x1b[1m(2.05)\x1b[0m\t')
child.stdout.pipe(concat(function(data) {
expect(data.toString()).to.eql('hello world\n')
done()
}))
}))
server.once('request', function(req, res) {
res.end('hello world')
})
})
it('should GET a given resource by default (bis)', function(done) {
var child = call('coap://localhost')
child.stderr.pipe(concat(function(data) {
expect(data.toString()).to.eql('\x1b[1m(2.05)\x1b[0m\t')
child.stdout.pipe(concat(function(data) {
expect(data.toString()).to.eql('hello matteo\n')
done()
}))
}))
server.once('request', function(req, res) {
res.end('hello matteo')
})
})
it('should GET not adding a newline (short)', function(done) {
var child = call('-n', 'coap://localhost')
child.stderr.pipe(concat(function(data) {
expect(data.toString()).to.eql('\x1b[1m(2.05)\x1b[0m\t')
child.stdout.pipe(concat(function(data) {
expect(data.toString()).to.eql('hello matteo')
done()
}))
}))
server.once('request', function(req, res) {
res.end('hello matteo')
})
})
it('should GET non-confirmable (short)', function(done) {
call('-c', 'coap://localhost')
server.once('request', function(req, res) {
res.end('')
expect(req._packet.confirmable).to.be.false
done()
})
})
it('should print help if no url', function(done) {
call().stdout.pipe(concat(function(data) {
expect(data.toString()).to.match(/Usage/)
done()
}))
})
it('should not print status code on quiet option', function(done) {
call('-q','coap://localhost').stderr.pipe(concat(function(data) {
expect(null)
done()
}))
server.once('request', function(req, res) {
res.end('hello world')
})
})
it('should only emit status code if the response was a 4.04', function(done) {
var child = call('coap://localhost')
child.stderr.pipe(concat(function(data) {
expect(data.toString()).to.eql('\x1b[1m(4.04)\x1b[0m\n')
child.stdout.pipe(concat(function(data) {
expect(null)
done()
}))
}))
server.once('request', function(req, res) {
res.statusCode = 404
res.end()
})
})
it('should GET a given resource by specifying the verb', function(done) {
var child = call('get', 'coap://localhost')
child.stderr.pipe(concat(function(data) {
expect(data.toString()).to.eql('\x1b[1m(2.05)\x1b[0m\t')
child.stdout.pipe(concat(function(data) {
expect(data.toString()).to.eql('hello world\n')
done()
}))
}))
server.once('request', function(req, res) {
res.end('hello world')
})
})
;['PUT', 'POST'].forEach(function(method) {
it('should ' + method + ' a given resource', function(done) {
call(method.toLowerCase(), 'coap://localhost').stdin.end('hello world')
server.once('request', function(req, res) {
res.end('')
expect(req.method).to.eql(method)
req.pipe(concat(function(data) {
expect(data.toString()).to.eql('hello world')
done()
}))
})
})
it('should ' + method + ' a given resource from an option', function(done) {
call(method.toLowerCase(), '-p', 'hello world', 'coap://localhost')
server.once('request', function(req, res) {
res.end('')
expect(req.method).to.eql(method)
req.pipe(concat(function(data) {
expect(data.toString()).to.eql('hello world')
done()
}))
})
})
})
it('should DELETE a given resource', function(done) {
call('delete', 'coap://localhost')
server.once('request', function(req, res) {
res.end('deleted')
expect(req.method).to.eql('DELETE')
done()
})
})
it('should observe the given resource and emit the values', function(done) {
var child = call('-o', 'coap://localhost')
child.stderr.once('data', function(data) {
expect(data.toString()).to.eql('\x1b[1m(2.05)\x1b[0m\t')
child.stdout.once('data', function(data) {
expect(data.toString()).to.eql('hello\n')
child.stderr.once('data', function(data) {
expect(data.toString()).to.eql('\x1b[1m(2.05)\x1b[0m\t')
child.stdout.once('data', function(data) {
expect(data.toString()).to.eql('matteo\n')
child.kill();
done();
})
})
})
})
server.once('request', function(req, res) {
setTimeout(function() {
res.write('hello')
setTimeout(function() {
res.end('matteo')
}, 10)
}, 10)
})
})
it('should observe the given resource and emit the values without a new line', function(done) {
var child = call('-o', '-n', 'coap://localhost')
child.stderr.once('data', function(data) {
expect(data.toString()).to.eql('\x1b[1m(2.05)\x1b[0m\t')
child.stdout.once('data', function(data) {
expect(data.toString()).to.eql('hello')
child.stderr.once('data', function(data) {
expect(data.toString()).to.eql('\x1b[1m(2.05)\x1b[0m\t')
child.stdout.once('data', function(data) {
expect(data.toString()).to.eql('matteo')
child.kill();
done();
})
})
})
})
server.once('request', function(req, res) {
setTimeout(function() {
res.write('hello')
setTimeout(function() {
res.end('matteo')
}, 10)
}, 10)
})
})
it('should support a 4.04 for an observe', function(done) {
call('-o', 'coap://localhost').stderr.pipe(concat(function(data) {
expect(data.toString()).to.eql('\x1b[1m(4.04)\x1b[0m\n')
done()
}))
server.once('request', function(req, res) {
res.statusCode = 404
res.end()
})
})
})