Skip to content

Instantly share code, notes, and snippets.

@zavr-1
Created January 10, 2020 04:08
Show Gist options
  • Save zavr-1/819a603517814abd83758a5c066d8f03 to your computer and use it in GitHub Desktop.
Save zavr-1/819a603517814abd83758a5c066d8f03 to your computer and use it in GitHub Desktop.
import { join } from 'path'
import { ok, deepEqual } from '@zoroaster/assert'
import TempContext from 'temp-context'
import Context from '../../context'
/** @type {Object.<string, (c: Context, t: TempContext)>} */
const T = {
context: [Context, TempContext],
async'handles any file upload'({ createApp, startApp, staticDir }, { TEMP, snapshot }) {
const s = join(staticDir, 'small.txt')
const { middleware: { form }, app, router } = await createApp({
form: {
dest: TEMP,
},
})
app.use(router.routes())
router.post('/test', form.any(), (ctx) => {
ok(ctx.req.files[0])
ok(ctx.files[0])
ok(ctx.files[0] === ctx.req.files[0])
delete ctx.req.files[0].stream
delete ctx.req.files[0].filename
delete ctx.req.files[0].path
ctx.body = ctx.req.files
})
await startApp()
.postForm('/test', async (f) => {
await f.addFile(s, 'upload')
})
.assert(200, [{
fieldname: 'upload',
originalname: 'small.txt',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'test/temp',
size: 12,
}])
const st = await snapshot()
return st.replace(/# .+/, '# file')
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment