From 1419e9ab49a0e2bc0ab595c8c1ab6f8118b94ecf Mon Sep 17 00:00:00 2001 From: Chang-Hung Liang Date: Tue, 23 Oct 2018 22:32:02 +0800 Subject: [PATCH] Fix bug: z.stashFile doesn't pick up filename in Content-Disposition --- package.json | 2 +- src/tools/create-file-stasher.js | 37 +++++++++++++++++++++----------- test/tools/file-stasher.js | 28 ++++++++++++++++++++++++ test/tools/mocky.js | 4 ++-- 4 files changed, 56 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 1dc5298..2f97c53 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "preversion": "git pull && npm test", "version": "node bin/bump-dependencies.js && git add package.json", "postversion": "git push && git push --tags", - "test": "mocha -t 5000 --recursive test", + "test": "mocha -t 10000 --recursive test", "posttest": "npm run lint", "plain-test": "mocha -t 5000 --recursive test", "integration-test": "mocha -t 10000 integration-test", diff --git a/src/tools/create-file-stasher.js b/src/tools/create-file-stasher.js index 4cf7d42..2f087c0 100644 --- a/src/tools/create-file-stasher.js +++ b/src/tools/create-file-stasher.js @@ -111,27 +111,36 @@ const createFileStasher = input => { let newBufferStringStream = response; if (_.isString(response)) { newBufferStringStream = response; - } else if (response && response.headers) { - if (response.body && typeof response.body.pipe === 'function') { + } else if (response) { + if (Buffer.isBuffer(response)) { + newBufferStringStream = response; + } else if (Buffer.isBuffer(response.dataBuffer)) { + newBufferStringStream = response.dataBuffer; + } else if ( + response.body && + typeof response.body.pipe === 'function' + ) { newBufferStringStream = response.body; } else { newBufferStringStream = response.content; } - knownLength = - knownLength || response.getHeader('content-length'); - const cd = response.getHeader('content-disposition'); - if (cd) { - filename = - filename || - contentDisposition.parse(cd).parameters.filename; + + if (response.headers) { + knownLength = + knownLength || response.getHeader('content-length'); + const cd = response.getHeader('content-disposition'); + if (cd) { + filename = + filename || + contentDisposition.parse(cd).parameters.filename; + } } - } else if (Buffer.isBuffer(response)) { - newBufferStringStream = response; } else { throw new Error( 'Cannot stash a Promise wrapped file of unknown type.' ); } + return uploader( result, newBufferStringStream, @@ -140,9 +149,13 @@ const createFileStasher = input => { fileContentType ); }; + if (isStreamed) { maybeResponse.throwForStatus(); - return maybeResponse.buffer().then(parseFinalResponse); + return maybeResponse.buffer().then(buffer => { + maybeResponse.dataBuffer = buffer; + return parseFinalResponse(maybeResponse); + }); } else { return parseFinalResponse(maybeResponse); } diff --git a/test/tools/file-stasher.js b/test/tools/file-stasher.js index b7bd8e9..66bdf45 100644 --- a/test/tools/file-stasher.js +++ b/test/tools/file-stasher.js @@ -226,4 +226,32 @@ describe('file upload', () => { }) .catch(done); }); + + it('should get filename from content-disposition', done => { + mocky.mockRpcCall(mocky.fakeSignedPostData); + + // Expect to have this part in the request body sent to S3 + mocky.mockUpload( + /name="Content-Disposition"\r\n\r\nattachment; filename="an example\.json"/ + ); + + const request = createAppRequestClient(input); + const file = request({ + url: 'https://zapier-httpbin.herokuapp.com/response-headers', + params: { + 'Content-Disposition': 'inline; filename="an example.json"' + }, + raw: true + }); + stashFile(file) + .then(url => { + should(url).eql( + `${mocky.fakeSignedPostData.url}${ + mocky.fakeSignedPostData.fields.key + }` + ); + done(); + }) + .catch(done); + }); }); diff --git a/test/tools/mocky.js b/test/tools/mocky.js index 800d092..7500f53 100644 --- a/test/tools/mocky.js +++ b/test/tools/mocky.js @@ -39,9 +39,9 @@ const fakeSignedPostData = { } }; -const mockUpload = () => { +const mockUpload = bodyMatcher => { nock('http://s3-fake.zapier.com') - .post('/') + .post('/', bodyMatcher) .reply(204, ''); };