-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
rest.js
444 lines (393 loc) · 15.2 KB
/
rest.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
const test = require('tape')
const { readFileSync, createReadStream, createWriteStream, unlinkSync } = require('fs')
const { getRestClient, connect } = require('../../index')
const { envOptions } = require('../connection')
// rest client interactions
test('with rest client', async function (t) {
const testCollection = '/db/rest-test'
const _query = '<c name="/db/rest-test">{' +
'for $r in xmldb:get-child-resources("/db/rest-test") order by $r return <r name="{$r}" />' +
'}</c>'
const xqueryMainModule = `xquery version "3.1";
for $r in xmldb:get-child-resources("/db/rest-test")
order by $r
return $r
`
const db = connect(envOptions)
const rc = await getRestClient(envOptions)
await db.collections.create(testCollection)
const testBuffer = Buffer.from('<collection>\n <item property="value"/>\n</collection>')
t.test('upload file (buffer) returns Created', async function (st) {
try {
const res = await rc.put(readFileSync('spec/files/test.xml'), 'db/rest-test/from-buffer.xml')
st.equal(res.statusCode, 201)
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('create file from string returns Created', async function (st) {
try {
const res = await rc.put('this is a test', 'db/rest-test/from-string.txt')
st.equal(res.statusCode, 201)
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('upload invalid file (buffer) fails with Bad Request', async function (st) {
try {
const res = await rc.put(readFileSync('spec/files/invalid.xml'), 'db/rest-test/from-buffer-invalid.xml')
st.fail(res)
st.end()
} catch (e) {
st.equal(e.response.statusCode, 400)
st.end()
}
})
t.test('upload file (stream) returns Created', async function (st) {
try {
const res = await rc.put(createReadStream('spec/files/test.xml'), 'db/rest-test/from-stream.xml')
st.equal(res.statusCode, 201)
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('upload invalid file (stream) fails with Bad Request', async function (st) {
try {
const res = await rc.put(createReadStream('spec/files/invalid.xml'), 'db/rest-test/from-stream-invalid.xml')
st.fail(res)
st.end()
} catch (e) {
st.equal(e.response.statusCode, 400)
st.end()
}
})
t.test('create file from Generator returns Created', async function (st) {
try {
const generator = function * () {
let index = 0
while (index < 3) {
yield `${index++}\n`
}
}
const res = await rc.put(generator, 'db/rest-test/from-generator.txt')
st.equal(res.statusCode, 201)
const { body } = await rc.get('db/rest-test/from-generator.txt')
st.equal(body, '0\n1\n2\n')
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('create file from Async Generator returns Created', async function (st) {
try {
const generator = async function * () {
let index = 0
while (index < 3) {
yield await new Promise(resolve => setTimeout(resolve(`${index++}\n`), 100))
}
}
const res = await rc.put(generator, 'db/rest-test/from-async-generator.txt')
st.equal(res.statusCode, 201)
const { body } = await rc.get('db/rest-test/from-async-generator.txt')
st.equal(body, '0\n1\n2\n')
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('create file from FormData returns Created', async function (st) {
st.skip('FormData is still experimental')
// try {
// const data = new FormData()
// data.append('test', 'value')
// data.append('another', 'value')
// const res = await rc.put(data, 'db/rest-test/from-form-data.txt')
// st.equal(res.statusCode, 201)
// const { body } = await rc.get('db/rest-test/from-form-data.txt')
// st.equal(body, '0\n1\n2\n')
// } catch (e) {
// st.fail(e)
// }
})
t.test('read xml file (buffer)', async function (st) {
try {
await rc.put(testBuffer, 'db/rest-test/read-test-buffer.xml')
const res = await rc.get('db/rest-test/read-test-buffer.xml')
st.equal(res.statusCode, 200)
st.equal(res.body, '<collection>\n <item property="value"/>\n</collection>')
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('non-existent file returns 404', async function (st) {
try {
const res = await rc.get('db/rest-test/non-existent.file')
st.fail(res)
st.end()
} catch (e) {
st.equal(e.response.statusCode, 404)
st.end()
}
})
t.test('stream xml file from db', async function (st) {
try {
await rc.put(testBuffer, 'db/rest-test/read-test-stream.xml')
const writeStream = createWriteStream('stream-test.xml')
const res = await rc.get('db/rest-test/read-test-stream.xml', {}, writeStream)
st.equal(res.statusCode, 200)
const written = readFileSync('stream-test.xml')
st.equal(written.toString(), '<collection>\n <item property="value"/>\n</collection>')
unlinkSync('stream-test.xml')
st.end()
} catch (e) {
st.equal(e.response.statusCode, 400)
st.end()
}
})
t.test('stream non-existent file returns error, does not write file', async function (st) {
const writeStream = createWriteStream('stream-fail-test.xml')
try {
const res = await rc.get('db/rest-test/non-existent.file', {}, writeStream)
st.fail(res)
st.end()
} catch (e) {
st.ok(writeStream.destroyed)
st.equal(writeStream.bytesWritten, 0)
st.equal(e.response.statusCode, 404)
st.end()
}
})
t.test('stream xml file from db', async function (st) {
try {
await rc.put(testBuffer, 'db/rest-test/read-test-stream.xml')
const writeStream = createWriteStream('stream-test.xml')
const res = await rc.get('db/rest-test/read-test-stream.xml', {}, writeStream)
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
const written = readFileSync('stream-test.xml')
st.equal(written.toString(), '<collection>\n <item property="value"/>\n</collection>')
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('getting a collection will return the file listing as xml', async function (st) {
try {
const res = await rc.get('db/rest-test')
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
const lines = res.body.split('\n')
st.equal(lines[0], '<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist">')
st.ok(lines[1].startsWith(' <exist:collection name="/db/rest-test"'))
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('collection listing does honor neither _wrap nor _indent', async function (st) {
try {
const res = await rc.get('db/rest-test', { _wrap: 'no', _indent: 'no' })
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
const lines = res.body.split('\n')
st.equal(lines[0], '<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist">')
st.ok(lines[1].startsWith(' <exist:collection name="/db/rest-test"'))
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('get in combination with _query allows to get unindented, unwrapped result', async function (st) {
try {
const res = await rc.get('db/rest-test', { _wrap: 'no', _indent: 'no', _query })
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
const lines = res.body.split('\n')
st.equal(lines.length, 1, 'body is a single line')
st.ok(lines[0].startsWith('<c name="/db/rest-test"><r name="from-async-generator.txt"/><r name='), lines[0])
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('get in combination with _query allows to get indented, wrapped result', async function (st) {
try {
const res = await rc.get('db/rest-test', { _wrap: 'yes', _indent: 'yes', _query })
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
st.equal(res.hits, 1, 'result returned ' + res.hits + ' hit(s)')
st.equal(res.start, 1, 'start is ' + res.start)
st.equal(res.count, 1, 'count is ' + res.count)
const lines = res.body.split('\n')
st.ok(lines[0].startsWith('<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"'))
st.equal(lines[1], ' <c name="/db/rest-test">')
st.equal(lines[2], ' <r name="from-async-generator.txt"/>')
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('post returns wrapped and indented result', async function (st) {
try {
const res = await rc.post(_query, 'db/rest-test')
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
st.equal(res.hits, 1, 'result returned ' + res.hits + ' hit(s)')
st.equal(res.start, 1, 'start is ' + res.start)
st.equal(res.count, 1, 'count is ' + res.count)
const lines = res.body.split('\n')
st.ok(lines[0].startsWith('<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"'))
st.equal(lines[1], ' <c name="/db/rest-test">')
st.equal(lines[2], ' <r name="from-async-generator.txt"/>')
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('post returns wrapped and unindented result', async function (st) {
try {
const res = await rc.post(_query, 'db/rest-test', { indent: 'no' })
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
st.equal(res.hits, 1, 'result returned ' + res.hits + ' hit(s)')
st.equal(res.start, 1, 'start is ' + res.start)
st.equal(res.count, 1, 'count is ' + res.count)
const lines = res.body.split('\n')
st.equal(lines.length, 1, 'body consists of a single line')
st.ok(lines[0].startsWith('<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"'))
st.ok(lines[0].includes('<c name="/db/rest-test">'), 'contains result')
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('post honors start and max', async function (st) {
try {
const res = await rc.post(xqueryMainModule, 'db/rest-test', { start: 1, max: 1 })
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
st.equal(res.hits, 7, 'result returned ' + res.hits + ' hit(s)')
st.equal(res.start, 1, 'start is ' + res.start)
st.equal(res.count, 1, 'count is ' + res.count)
const lines = res.body.split('\n')
st.equal(lines.length, 3, 'body consists of 3 lines')
st.ok(lines[0].startsWith('<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"'))
st.equal(lines[1], ' <exist:value exist:type="xs:string">from-async-generator.txt</exist:value>')
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('post honors just start', async function (st) {
try {
const res = await rc.post(xqueryMainModule, 'db/rest-test', { start: 2 })
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
st.equal(res.hits, 7, 'result returned ' + res.hits + ' hit(s)')
st.equal(res.start, 2, 'start is ' + res.start)
st.equal(res.count, 6, 'count is ' + res.count)
const lines = res.body.split('\n')
st.equal(lines.length, 8, 'body consists of 8 lines')
st.ok(lines[0].startsWith('<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"'))
st.equal(lines[1], ' <exist:value exist:type="xs:string">from-buffer.xml</exist:value>')
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
t.test('post honors cache', async function (st) {
try {
const res = await rc.post(xqueryMainModule, 'db/rest-test', { cache: 'yes', start: 1, max: 1 })
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
st.isNotEqual(res.session, -1, 'Got session ' + res.session)
st.equal(res.hits, 7, 'result returned ' + res.hits + ' hit(s)')
st.equal(res.start, 1, 'start is ' + res.start)
st.equal(res.count, 1, 'count is ' + res.count)
const lines = res.body.split('\n')
st.equal(lines.length, 3, 'body consists of 3 lines')
st.equal(lines[1], ' <exist:value exist:type="xs:string">from-async-generator.txt</exist:value>')
const res2 = await rc.post(xqueryMainModule, 'db/rest-test', { session: res.session, start: 2, max: 1 })
const lines2 = res2.body.split('\n')
st.equal(res.session, res2.session, 'same session was returned (' + res.session + ', ' + res2.session + ')')
st.equal(res2.hits, 7, 'result returned ' + res2.hits + ' hit(s)')
st.equal(res2.start, 2, 'start is ' + res2.start)
st.equal(res2.count, 1, 'count is ' + res2.count)
st.equal(lines2.length, 3, 'body consists of 3 lines')
st.equal(lines2[1], ' <exist:value exist:type="xs:string">from-buffer.xml</exist:value>')
const { statusCode } = await rc.get('db', { _release: res.session })
st.equal(statusCode, 200, 'session ' + res.session + ' released')
st.end()
} catch (e) {
console.error(e)
st.fail(e)
st.end()
}
})
t.teardown(async _ => {
await db.collections.remove(testCollection)
unlinkSync('stream-fail-test.xml')
unlinkSync('stream-test.xml')
})
})
test('with rest client over http', async function (t) {
const modifiedOptions = Object.assign({ protocol: 'http:', port: '8080' }, envOptions)
const rc = await getRestClient(modifiedOptions)
t.test('non-existent file returns 404', async function (st) {
try {
const res = await rc.get('db/rest-test/non-existent.file')
st.fail(res)
st.end()
} catch (e) {
st.equal(e.response.statusCode, 404)
st.end()
}
})
t.test('getting a collection will return the file listing as xml', async function (st) {
try {
const res = await rc.get('db')
st.equal(res.statusCode, 200, 'server responded with status ' + res.statusCode)
const lines = res.body.split('\n')
st.equal(lines[0], '<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist">')
st.end()
} catch (e) {
st.fail(e)
st.end()
}
})
})
test('with rest client connecting to exist-db.org as guest with standard port', async function (t) {
const rc = await getRestClient({ host: 'exist-db.org', port: 443 })
t.test('getting a collection listing is rejected as unauthorized', async function (st) {
try {
const res = await rc.get('db')
st.fail(res)
} catch (e) {
st.equal(e.response.statusCode, 401)
}
st.end()
})
})
test('with rest client connecting to exist-db.org from URL', async function (t) {
const { protocol, hostname, port } = new URL('https://exist-db.org/')
// NOTE: that host is mapped to hostname
const rc = await getRestClient({ protocol, host: hostname, port })
t.test('getting a collection listing is rejected as unauthorized', async function (st) {
try {
const res = await rc.get('db')
st.fail(res)
} catch (e) {
st.equal(e.response.statusCode, 401)
}
st.end()
})
})