Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@tus/server: allow onUploadFinish hook to override response data #615

Merged
merged 8 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/server/src/handlers/PostHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,15 @@ export class PostHandler extends BaseHandler {
}
}

//Only append Location header if its valid for the final http status (201 or 3xx)
if (responseData.status === 201 || responseData.status.toString().at(0) === '3') {
netdown marked this conversation as resolved.
Show resolved Hide resolved
responseData.headers['Location'] = url
}

const writtenRes = this.write(
res,
responseData.status,
Object.assign({Location: url}, responseData.headers),
responseData.headers,
responseData.body
)

Expand Down
18 changes: 18 additions & 0 deletions packages/server/test/PostHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,24 @@ describe('PostHandler', () => {
assert.equal(upload.offset, 0)
assert.equal(upload.size, 0)
})

it('does not set Location header if onUploadFinish hook returned a not eligible status code', async function () {
const store = sinon.createStubInstance(DataStore)
const handler = new PostHandler(store, {
path: '/test/output',
locker: new MemoryLocker(),
onUploadFinish: async (req, res) => ({res, status_code: 200}),
})

req.headers = {
'upload-length': '0',
host: 'localhost:3000',
}
store.create.resolvesArg(0)

await handler.send(req, res, context)
assert.equal('location' in res._getHeaders(), false)
})
})
})
})
Loading