test: ensure DELETE actually deletes
Massimo Melina committed
Jul 4, 2025 at 16:54 UTC
424482c6541eb16e218cfe0dde58a5e8c619cc79
1 file changed
+12
-5
tests/test.ts
+12
-5
@@ -8,7 +8,7 @@ import _ from 'lodash'
8
import { findDefined, randomId, try_, tryJson, wait } from '../src/cross'
9
import { httpStream, stream2string, XRequestOptions } from '../src/util-http'
10
import { ThrottledStream, ThrottleGroup } from '../src/ThrottledStream'
11
-import { rm, writeFile } from 'fs/promises'
11
+import { access, rm, writeFile } from 'fs/promises'
12
import { Readable } from 'stream'
13
/*
14
import { PORT, srv } from '../src'
@@ -27,7 +27,7 @@ const BASE_URL_127 = 'http://127.0.0.1:81'
27
const UPLOAD_ROOT = '/for-admins/upload/'
28
const UPLOAD_RELATIVE = 'temp/gpl.png'
29
const UPLOAD_DEST = UPLOAD_ROOT + UPLOAD_RELATIVE
30
-const BIG_CONTENT = _.repeat(randomId(10), 200_000) // 2MB, big enough to saturate buffers
30
+const BIG_CONTENT = _.repeat(randomId(10), 300_000) // 3MB, big enough to saturate buffers
31
const throttle = BIG_CONTENT.length /1000 /0.8 // KB, finish in 0.8s, quick but still overlapping downloads
32
const SAMPLE_FILE_PATH = resolve(__dirname, 'page/gpl.png')
33
let defaultBaseUrl = BASE_URL
@@ -206,7 +206,7 @@ describe('after-login', () => {
206
const r = reqUpload(UPLOAD_DEST + '?supposedToAbort', 0, makeReadableThatTakes(neededTime))()
207
setTimeout(r.abort, afterMs)
208
return r.catch(() => {}) // wait for it to fail
209
- .then(() => wait(1)) // aborted requests don't guarantee that the server has finished and released the file, so we wait some arbitrary time
209
+ .then(() => wait(10)) // aborted requests don't guarantee that the server has finished and released the file, so we wait some arbitrary time
210
}
211
const timeFirstRequest = neededTime * .5 // not enough to finish
212
await makeAbortedRequest(timeFirstRequest)
@@ -219,7 +219,7 @@ describe('after-login', () => {
219
throw Error("modified temp file")
220
await makeAbortedRequest(timeFirstRequest * 1.5) // upload more than r1
221
if (!(size < getTempSize()!)) // should be increased, as secondary temp file got bigger and replaced primary one
222
- throw Error("temp file not enlarged")
222
+ throw Error(`temp file not enlarged, it was ${size} and now it's ${getTempSize()}`)
223
await reqUpload(UPLOAD_DEST, 200, makeReadableThatTakes(0))() // quickly complete the upload, and check for final size
224
if (getTempSize())
225
throw Error("temp file should be cleared")
@@ -233,7 +233,12 @@ describe('after-login', () => {
233
const renameTo = 'z'
234
test('rename.ok', reqApi('rename', { uri: UPLOAD_DEST, dest: renameTo }, 200))
235
test('delete.miss renamed', req(UPLOAD_DEST, 404, { method: 'delete' }))
236
- test('delete.ok', req(dirname(UPLOAD_DEST) + '/' + renameTo, 200, { method: 'delete' }))
236
+ test('delete.ok', async () => {
237
+ const fn = resolve(__dirname, dirname(UPLOAD_RELATIVE), renameTo)
238
+ await access(fn)
239
+ await req(dirname(UPLOAD_DEST) + '/' + renameTo, 200, { method: 'delete' })()
240
+ await access(fn).then(() => { throw "not deleted" }, () => {})
241
+ })
242
test('reupload', reqUpload(UPLOAD_DEST, 200))
243
test('delete.method', req(UPLOAD_DEST, 200, { method: 'DELETE' }))
244
test('delete.miss deleted', req(UPLOAD_DEST, 404, { method: 'delete' }))
@@ -336,6 +341,8 @@ function req(url: string, test:Tester, { baseUrl, throttle, ...requestOptions }:
341
)
342
343
async function process(res:any) {
344
+ if (!res)
345
+ return console.log('got', { res })
346
//console.debug('sent', requestOptions, 'got', res instanceof Error ? String(res) : [res.status])
347
if (test && test instanceof RegExp)
348
test = { re:test }