@samitouri / QOSami-HFS / commits / c26dddde

test: upload using curl/post

Massimo Melina committed Jan 25, 2025 at 10:58 UTC c26dddde88f0e2f01579cbb7701e270c01cee822
1 file changed +19 -4
tests/test.ts
+19 -4
@@ -1,6 +1,7 @@
1 import { srpClientSequence } from '../src/srp'
2 import { createReadStream, statSync } from 'fs'
3 -import { dirname, join } from 'path'
3 +import { basename, dirname, join, resolve } from 'path'
4 +import { exec } from 'child_process'
5 import _ from 'lodash'
6 import { findDefined, randomId, tryJson, wait } from '../src/cross'
7 import { httpStream, stream2string, XRequestOptions } from '../src/util-http'
@@ -25,6 +26,7 @@ const UPLOAD_RELATIVE = 'temp/gpl.png'
26 const UPLOAD_DEST = UPLOAD_ROOT + UPLOAD_RELATIVE
27 const BIG_CONTENT = _.repeat(randomId(10), 200_000) // 2MB, big enough to saturate buffers
28 const throttle = BIG_CONTENT.length /1000 /0.5 // KB, finish in 0.5s, quick but still overlapping downloads
29 +const SAMPLE_FILE_PATH = resolve(__dirname, 'page/gpl.png')
30 let defaultBaseUrl = BASE_URL
31
32 class StringRepeaterStream extends Readable {
@@ -119,6 +121,20 @@ describe('basics', () => {
121 }))
122
123 it('upload.need account', reqUpload( UPLOAD_DEST, 401))
124 + it('upload.post', done => {
125 + const cmd = `curl -u ${username}:${password} -F upload=@${SAMPLE_FILE_PATH} ${BASE_URL}${UPLOAD_ROOT}`;
126 + exec(cmd, (err, out) => {
127 + if (err) return done(err)
128 + const fn = resolve(__dirname, basename(decodeURI(tryJson(out)?.uris?.[0])))
129 + if (!fn) return done("unexpected output " + out)
130 + try {
131 + const stats = statSync(fn)
132 + rm(fn).catch(() => {}) // clear
133 + done(stats?.size !== statSync(SAMPLE_FILE_PATH).size && "unexpected size for " + fn)
134 + }
135 + catch (e) { done(e) }
136 + })
137 + })
138 it('create_folder', reqApi('create_folder', { uri: UPLOAD_ROOT, name: 'temp' }, 401))
139 it('delete.no perm', reqApi('delete', { uri: '/for-admins/' }, 405))
140 it('delete.need account', reqApi('delete', { uri: UPLOAD_ROOT }, 401))
@@ -205,8 +221,7 @@ function login(usr: string, pwd=password) {
221 }
222
223 function reqUpload(dest: string, tester: Tester, body?: string | Readable, size?: number) {
208 - const fn = join(__dirname, 'page/gpl.png')
209 - size ??= (body as any)?.length ?? statSync(fn).size // it's ok that Readable.length is undefined
224 + size ??= (body as any)?.length ?? statSync(SAMPLE_FILE_PATH).size // it's ok that Readable.length is undefined
225 if (tester === 200)
226 tester = {
227 status: tester,
@@ -223,7 +238,7 @@ function reqUpload(dest: string, tester: Tester, body?: string | Readable, size?
238 return req(dest, tester, {
239 method: 'PUT',
240 headers: { 'content-length': size },
226 - body: body ?? createReadStream(fn)
241 + body: body ?? createReadStream(SAMPLE_FILE_PATH)
242 })
243 }
244