Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion lib/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,6 @@ export class BaseUpload {
if (this._uploadLengthDeferred && done) {
this._size = this._offset + sizeOfValue
req.setHeader('Upload-Length', `${this._size}`)
this._uploadLengthDeferred = false
}

// The specified uploadSize might not match the actual amount of data that a source
Expand Down
75 changes: 75 additions & 0 deletions test/spec/test-web-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,81 @@ describe('tus', () => {
await options.onSuccess.toBeCalled()
})

it('should retry the final PATCH request with Upload-Length header', async () => {
const reader = makeReader('hello world')

const testStack = new TestHttpStack()
const options = {
httpStack: testStack,
endpoint: 'http://tus.io/files/',
chunkSize: 6,
retryDelays: [10, 10, 10],
onSuccess: waitableFunction('onSuccess'),
uploadLengthDeferred: true,
}

const upload = new Upload(reader, options)
upload.start()

let req = await testStack.nextRequest()
expect(req.url).toBe('http://tus.io/files/')
expect(req.method).toBe('POST')
expect(req.requestHeaders['Upload-Defer-Length']).toBe('1')

req.respondWith({
status: 201,
responseHeaders: {
Location: '/files/foo',
},
})

req = await testStack.nextRequest()
expect(req.url).toBe('http://tus.io/files/foo')
expect(req.method).toBe('PATCH')

req.respondWith({
status: 204,
responseHeaders: {
'Upload-Offset': '6',
},
})

req = await testStack.nextRequest()
expect(req.url).toBe('http://tus.io/files/foo')
expect(req.method).toBe('PATCH')
expect(req.requestHeaders['Upload-Length']).toBe('11')

req.respondWith({
status: 500,
})

req = await testStack.nextRequest()
expect(req.url).toBe('http://tus.io/files/foo')
expect(req.method).toBe('HEAD')

req.respondWith({
status: 204,
responseHeaders: {
'Upload-Offset': '6',
'Upload-Defer-Length': '1',
},
})

req = await testStack.nextRequest()
expect(req.url).toBe('http://tus.io/files/foo')
expect(req.method).toBe('PATCH')
expect(req.requestHeaders['Upload-Length']).toBe('11')

req.respondWith({
status: 204,
responseHeaders: {
'Upload-Offset': '11',
},
})

await options.onSuccess.toBeCalled()
})

it('should throw an error if the source provides less data than uploadSize', async () => {
const reader = makeReader('hello world')

Expand Down
Loading