Skip to content

Commit

Permalink
Allow batched operations again.
Browse files Browse the repository at this point in the history
  • Loading branch information
krasivyy3954 committed Apr 25, 2019
1 parent c44d474 commit 5549f9f
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Patch

- Updated dependencies.
- Allow batched operations again, fixing [#142](https://github.com/jaydenseric/graphql-upload/issues/142).
- Simplify tests by writing JSON as strings instead of using `JSON.stringify`.
- Use async middleware for Express tests.
- Added the Open Graph image design to the logo Sketch file.
Expand Down
2 changes: 1 addition & 1 deletion src/processRequest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export const processRequest = (
)
}

if (!isObject(operations))
if (!isObject(operations) && !Array.isArray(operations))
return exit(
createError(
400,
Expand Down
105 changes: 105 additions & 0 deletions src/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,111 @@ t.test('Single file.', async t => {
})
})

t.test('Single file batched.', async t => {
const sendRequest = async port => {
const body = new FormData()

body.append(
'operations',
'[{ "variables": { "file": null } }, { "variables": { "file": null } }]'
)
body.append(
'map',
'{ "1": ["0.variables.file"], "2": ["1.variables.file"] }'
)
body.append('1', 'a', { filename: 'a.txt' })
body.append('2', 'b', { filename: 'b.txt' })

await fetch(`http://localhost:${port}`, { method: 'POST', body })
}

const uploadATest = upload => async t => {
const resolved = await upload
const stream = resolved.createReadStream()

t.matchSnapshot(JSON.stringify(resolved, null, 2), 'Enumerable properties.')
t.type(stream, ReadStream, 'Stream type.')
t.equals(await streamToString(stream), 'a', 'Contents.')
}

const uploadBTest = upload => async t => {
const resolved = await upload
const stream = resolved.createReadStream()

t.matchSnapshot(JSON.stringify(resolved, null, 2), 'Enumerable properties.')
t.type(stream, ReadStream, 'Stream type.')
t.equals(await streamToString(stream), 'b', 'Contents.')
}

await t.test('Koa middleware.', async t => {
t.plan(4)

let operations

const app = new Koa().use(graphqlUploadKoa()).use(async (ctx, next) => {
operations = ctx.request.body

await Promise.all([
t.test('Upload A.', uploadATest(ctx.request.body[0].variables.file)),
t.test('Upload B.', uploadBTest(ctx.request.body[1].variables.file))
])

ctx.status = 204
await next()
})

const port = await startServer(t, app)

await sendRequest(port)

const fileA = await operations[0].variables.file
if (!fileA.capacitor.closed)
await new Promise(resolve => fileA.capacitor.once('close', resolve))
t.false(fs.existsSync(fileA.capacitor.path), 'Cleanup A.')

const fileB = await operations[1].variables.file
if (!fileB.capacitor.closed)
await new Promise(resolve => fileB.capacitor.once('close', resolve))
t.false(fs.existsSync(fileB.capacitor.path), 'Cleanup B.')
})

await t.test('Express middleware.', async t => {
t.plan(4)

let operations

const app = express()
.use(graphqlUploadExpress())
.use(async (request, response, next) => {
operations = request.body

try {
await Promise.all([
t.test('Upload A.', uploadATest(request.body[0].variables.file)),
t.test('Upload B.', uploadBTest(request.body[1].variables.file))
])
next()
} catch (error) {
next(error)
}
})

const port = await startServer(t, app)

await sendRequest(port)

const fileA = await operations[0].variables.file
if (!fileA.capacitor.closed)
await new Promise(resolve => fileA.capacitor.once('close', resolve))
t.false(fs.existsSync(fileA.capacitor.path), 'Cleanup A.')

const fileB = await operations[1].variables.file
if (!fileB.capacitor.closed)
await new Promise(resolve => fileB.capacitor.once('close', resolve))
t.false(fs.existsSync(fileB.capacitor.path), 'Cleanup B.')
})
})

t.test('Invalid ‘operations’ JSON.', async t => {
const sendRequest = async (t, port) => {
const body = new FormData()
Expand Down
32 changes: 32 additions & 0 deletions tap-snapshots/lib-test-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,38 @@ exports[`lib/test TAP Missing ‘operations’, ‘map’ and files. Koa middlew
}
`

exports[`lib/test TAP Single file batched. Express middleware. Upload A. > Enumerable properties. 1`] = `
{
"filename": "a.txt",
"mimetype": "text/plain",
"encoding": "7bit"
}
`

exports[`lib/test TAP Single file batched. Express middleware. Upload B. > Enumerable properties. 1`] = `
{
"filename": "b.txt",
"mimetype": "text/plain",
"encoding": "7bit"
}
`

exports[`lib/test TAP Single file batched. Koa middleware. Upload A. > Enumerable properties. 1`] = `
{
"filename": "a.txt",
"mimetype": "text/plain",
"encoding": "7bit"
}
`

exports[`lib/test TAP Single file batched. Koa middleware. Upload B. > Enumerable properties. 1`] = `
{
"filename": "b.txt",
"mimetype": "text/plain",
"encoding": "7bit"
}
`

exports[`lib/test TAP Single file. Express middleware. Upload. > Enumerable properties. 1`] = `
{
"filename": "a.txt",
Expand Down

0 comments on commit 5549f9f

Please sign in to comment.