-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrest.js
More file actions
400 lines (355 loc) · 15.9 KB
/
rest.js
File metadata and controls
400 lines (355 loc) · 15.9 KB
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
import { describe, it } from 'node:test'
import assert from 'node:assert'
import { readFileSync, createReadStream, createWriteStream, unlinkSync } from 'node:fs'
import { getRestClient, getXmlRpcClient } from '../../index.js'
import { envOptions } from '../connection.js'
// rest client interactions
await describe('with rest client', async function () {
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 = getXmlRpcClient(envOptions)
const rc = getRestClient(envOptions)
await db.collections.create(testCollection)
const testBuffer = Buffer.from('<collection>\n <item property="value"/>\n</collection>')
await it('upload file (buffer) returns Created', async function () {
try {
const buffer = readFileSync('spec/files/test.xml')
const res = await rc.put(buffer, 'db/rest-test/from-buffer.xml')
assert.strictEqual(res.statusCode, 201)
// const { body } = await rc.get('db/rest-test/from-buffer.xml')
// assert.strictEqual(await body.text(), testBuffer.toString())
} catch (e) {
assert.fail(e)
}
})
await it('create file from string returns Created', async function () {
try {
const res = await rc.put('this is a test', 'db/rest-test/from-string.txt')
assert.strictEqual(res.statusCode, 201)
} catch (e) {
assert.fail(e)
}
})
await it('upload invalid file (buffer) fails with Bad Request', async function () {
try {
const { statusCode } = await rc.put(readFileSync('spec/files/invalid.xml'), 'db/rest-test/from-buffer-invalid.xml')
assert.fail(statusCode)
} catch (e) {
assert.strictEqual(e.statusCode, 400)
}
})
await it('upload file (stream) returns Created', async function () {
try {
const res = await rc.put(createReadStream('spec/files/test.xml'), 'db/rest-test/from-stream.xml')
assert.strictEqual(res.statusCode, 201)
} catch (e) {
assert.fail(e)
}
})
await it('upload invalid file (stream) fails with Bad Request', async function () {
try {
const res = await rc.put(createReadStream('spec/files/invalid.xml'), 'db/rest-test/from-stream-invalid.xml')
assert.fail(res)
} catch (e) {
assert.strictEqual(e.statusCode, 400)
}
})
await it('create file from Generator returns Created', async function () {
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')
assert.strictEqual(res.statusCode, 201)
const { bodyText } = await rc.get('db/rest-test/from-generator.txt')
assert.strictEqual(bodyText, '0\n1\n2\n')
} catch (e) {
assert.fail(e)
}
})
await it('create file from Async Generator returns Created', async function () {
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')
assert.strictEqual(res.statusCode, 201)
const { bodyText } = await rc.get('db/rest-test/from-async-generator.txt')
assert.strictEqual(bodyText, '0\n1\n2\n')
} catch (e) {
assert.fail(e)
}
})
// await t.test('create file from FormData returns Created', async function () {
// 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')
// assert.strictEqual(res.statusCode, 201)
// const { body } = await rc.get('db/rest-test/from-form-data.txt')
// assert.strictEqual(body, '0\n1\n2\n')
// } catch (e) {
// assert.fail(e)
// }
// })
await it('read xml file (buffer)', async function () {
try {
await rc.put(testBuffer, 'db/rest-test/read-test-buffer.xml')
const { statusCode, bodyText } = await rc.get('db/rest-test/read-test-buffer.xml')
assert.strictEqual(statusCode, 200)
assert.strictEqual(bodyText, '<collection>\n <item property="value"/>\n</collection>')
} catch (e) {
assert.fail(e)
}
})
await it('non-existent file returns 404', async function () {
try {
const res = await rc.get('db/rest-test/non-existent.file')
assert.fail(res)
} catch (e) {
assert.strictEqual(e.statusCode, 404)
}
})
await it('stream xml file from db', async function () {
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)
assert.strictEqual(res.statusCode, 200)
const written = readFileSync('stream-test.xml')
assert.strictEqual(written.toString(), '<collection>\n <item property="value"/>\n</collection>')
unlinkSync('stream-test.xml')
} catch (e) {
console.log(e)
assert.strictEqual(e.statusCode, 400)
}
})
await it('stream non-existent file returns error, does not write file', async function () {
const writeStream = createWriteStream('stream-fail-test.xml')
try {
const res = await rc.get('db/rest-test/non-existent.file', {}, writeStream)
assert.fail(res)
} catch (e) {
assert.strictEqual(e.statusCode, 404)
assert.strictEqual(writeStream.bytesWritten, 0)
} finally {
// TODO: got destroyed the stream on error but the implementation with undici does not
writeStream.destroy()
assert.ok(writeStream.destroyed)
}
})
await it('stream xml file from db', async function () {
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)
assert.strictEqual(res.statusCode, 200, 'server responded with status ' + res.statusCode)
const written = readFileSync('stream-test.xml')
assert.strictEqual(written.toString(), '<collection>\n <item property="value"/>\n</collection>')
assert.ok(writeStream.destroyed)
} catch (e) {
assert.fail(e)
}
})
await it('getting a collection will return the file listing as xml', async function () {
try {
const { statusCode, bodyText } = await rc.get('db/rest-test')
assert.strictEqual(statusCode, 200, 'server responded with status ' + statusCode)
const lines = bodyText.split('\n')
assert.strictEqual(lines[0], '<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist">')
assert.ok(lines[1].startsWith(' <exist:collection name="/db/rest-test"'))
} catch (e) {
assert.fail(e)
}
})
await it('collection listing does honor neither _wrap nor _indent', async function () {
try {
const { statusCode, bodyText } = await rc.get('db/rest-test', { _wrap: 'no', _indent: 'no' })
assert.strictEqual(statusCode, 200, 'server responded with status ' + statusCode)
const lines = bodyText.split('\n')
assert.strictEqual(lines[0], '<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist">')
assert.ok(lines[1].startsWith(' <exist:collection name="/db/rest-test"'))
} catch (e) {
assert.fail(e)
}
})
await it('get in combination with _query allows to get unindented, unwrapped result', async function () {
try {
const { statusCode, bodyText } = await rc.get('db/rest-test', { _wrap: 'no', _indent: 'no', _query })
assert.strictEqual(statusCode, 200, 'server responded with status ' + statusCode)
const lines = bodyText.split('\n')
assert.strictEqual(lines.length, 1, 'body is a single line')
assert.ok(lines[0].startsWith('<c name="/db/rest-test"><r name="from-async-generator.txt"/><r name='), lines[0])
} catch (e) {
assert.fail(e)
}
})
await it('get in combination with _query allows to get indented, wrapped result', async function () {
try {
const { statusCode, bodyText, hits, start, count } = await rc.get('db/rest-test', { _wrap: 'yes', _indent: 'yes', _query })
assert.strictEqual(statusCode, 200, 'server responded with status ' + statusCode)
assert.strictEqual(hits, 1, 'result returned ' + hits + ' hit(s)')
assert.strictEqual(start, 1, 'start is ' + start)
assert.strictEqual(count, 1, 'count is ' + count)
const lines = bodyText.split('\n')
assert.ok(lines[0].startsWith('<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"'))
assert.strictEqual(lines[1], ' <c name="/db/rest-test">')
assert.strictEqual(lines[2], ' <r name="from-async-generator.txt"/>')
} catch (e) {
assert.fail(e)
}
})
await it('post returns wrapped and indented result', async function () {
try {
const res = await rc.post(_query, 'db/rest-test')
assert.strictEqual(res.statusCode, 200, 'server responded with status ' + res.statusCode)
assert.strictEqual(res.hits, 1, 'result returned ' + res.hits + ' hit(s)')
assert.strictEqual(res.start, 1, 'start is ' + res.start)
assert.strictEqual(res.count, 1, 'count is ' + res.count)
const lines = res.bodyText.split('\n')
assert.ok(lines[0].startsWith('<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"'))
assert.strictEqual(lines[1], ' <c name="/db/rest-test">')
assert.strictEqual(lines[2], ' <r name="from-async-generator.txt"/>')
} catch (e) {
assert.fail(e)
}
})
await it('post returns wrapped and unindented result', async function () {
try {
const res = await rc.post(_query, 'db/rest-test', { indent: 'no' })
assert.strictEqual(res.statusCode, 200, 'server responded with status ' + res.statusCode)
assert.strictEqual(res.hits, 1, 'result returned ' + res.hits + ' hit(s)')
assert.strictEqual(res.start, 1, 'start is ' + res.start)
assert.strictEqual(res.count, 1, 'count is ' + res.count)
const lines = res.bodyText.split('\n')
assert.strictEqual(lines.length, 1, 'body consists of a single line')
assert.ok(lines[0].startsWith('<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"'))
assert.ok(lines[0].includes('<c name="/db/rest-test">'), 'contains result')
} catch (e) {
assert.fail(e)
}
})
await it('post honors start and max', async function () {
try {
const res = await rc.post(xqueryMainModule, 'db/rest-test', { start: 1, max: 1 })
assert.strictEqual(res.statusCode, 200, 'server responded with status ' + res.statusCode)
assert.strictEqual(res.hits, 7, 'result returned ' + res.hits + ' hit(s)')
assert.strictEqual(res.start, 1, 'start is ' + res.start)
assert.strictEqual(res.count, 1, 'count is ' + res.count)
const lines = res.bodyText.split('\n')
assert.strictEqual(lines.length, 3, 'body consists of 3 lines')
assert.ok(lines[0].startsWith('<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"'))
assert.strictEqual(lines[1], ' <exist:value exist:type="xs:string">from-async-generator.txt</exist:value>')
} catch (e) {
assert.fail(e)
}
})
await it('post honors just start', async function () {
try {
const res = await rc.post(xqueryMainModule, 'db/rest-test', { start: 2 })
assert.strictEqual(res.statusCode, 200, 'server responded with status ' + res.statusCode)
assert.strictEqual(res.hits, 7, 'result returned ' + res.hits + ' hit(s)')
assert.strictEqual(res.start, 2, 'start is ' + res.start)
assert.strictEqual(res.count, 6, 'count is ' + res.count)
const lines = res.bodyText.split('\n')
assert.strictEqual(lines.length, 8, 'body consists of 8 lines')
assert.ok(lines[0].startsWith('<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"'))
assert.strictEqual(lines[1], ' <exist:value exist:type="xs:string">from-buffer.xml</exist:value>')
} catch (e) {
assert.fail(e)
}
})
await it('post honors cache', async function () {
try {
const res = await rc.post(xqueryMainModule, 'db/rest-test', { cache: 'yes', start: 1, max: 1 })
assert.strictEqual(res.statusCode, 200, 'server responded with status ' + res.statusCode)
assert.notStrictEqual(res.session, -1, 'Got session ' + res.session)
assert.strictEqual(res.hits, 7, 'result returned ' + res.hits + ' hit(s)')
assert.strictEqual(res.start, 1, 'start is ' + res.start)
assert.strictEqual(res.count, 1, 'count is ' + res.count)
const lines = res.bodyText.split('\n')
assert.strictEqual(lines.length, 3, 'body consists of 3 lines')
assert.strictEqual(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.bodyText.split('\n')
assert.strictEqual(res.session, res2.session, 'same session was returned (' + res.session + ', ' + res2.session + ')')
assert.strictEqual(res2.hits, 7, 'result returned ' + res2.hits + ' hit(s)')
assert.strictEqual(res2.start, 2, 'start is ' + res2.start)
assert.strictEqual(res2.count, 1, 'count is ' + res2.count)
assert.strictEqual(lines2.length, 3, 'body consists of 3 lines')
assert.strictEqual(lines2[1], ' <exist:value exist:type="xs:string">from-buffer.xml</exist:value>')
const { statusCode } = await rc.get('db', { _release: res.session })
assert.strictEqual(statusCode, 200, 'session ' + res.session + ' released')
} catch (e) {
// console.error(e)
assert.fail(e)
}
})
await it('teardown', async _ => {
await db.collections.remove(testCollection)
unlinkSync('stream-fail-test.xml')
unlinkSync('stream-test.xml')
})
})
await describe('with rest client over http', async function () {
const modifiedOptions = Object.assign({ protocol: 'http:', port: '8080' }, envOptions)
const rc = getRestClient(modifiedOptions)
await it('non-existent file returns 404', async function () {
try {
const res = await rc.get('db/rest-test/non-existent.file')
assert.fail(res)
} catch (e) {
assert.strictEqual(e.statusCode, 404)
}
})
await it('getting a collection will return the file listing as xml', async function () {
try {
const res = await rc.get('db')
assert.strictEqual(res.statusCode, 200, 'server responded with status ' + res.statusCode)
const lines = res.bodyText.split('\n')
assert.strictEqual(lines[0], '<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist">')
} catch (e) {
assert.fail(e)
}
})
})
await describe('with rest client connecting to exist-db.org as guest with standard port', async function () {
// const rc = await getRestClient(envOptions)
const rc = getRestClient({ host: 'exist-db.org', port: 443 })
await it('getting a collection listing is rejected as unauthorized', async function () {
try {
const { statusCode } = await rc.get('db/apps')
assert.fail(statusCode)
} catch (e) {
assert.strictEqual(e.statusCode, 401)
}
})
})
await describe('with rest client connecting to exist-db.org from URL', async function () {
const { protocol, hostname, port } = new URL('https://exist-db.org/')
// NOTE: that host is mapped to hostname
const rc = getRestClient({ protocol, host: hostname, port, basic_auth: { user: 'idonotexist', pass: '' } })
await it('getting a collection listing is rejected as unauthorized', async function () {
try {
const { statusCode } = await rc.get('db')
assert.fail(statusCode)
} catch (e) {
assert.strictEqual(e.statusCode, 401)
}
})
})