-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(http): process file uploads correctly (#3232)
- Loading branch information
Showing
27 changed files
with
305 additions
and
18 deletions.
There are no files selected for viewing
File renamed without changes.
4 changes: 1 addition & 3 deletions
4
examples/file-uploads/README.md → examples/http-file-uploads/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+1.2 MB
packages/artillery/test/integration/core/fixtures/files/artillery-installation.pdf
Binary file not shown.
Binary file added
BIN
+3.79 KB
packages/artillery/test/integration/core/fixtures/files/artillery-logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions
11
packages/artillery/test/integration/core/fixtures/http-file-upload-processor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
function getResponse(req, res, context, ee, next) { | ||
// We log the response body here so we can access it from the output | ||
console.log('RESPONSE BODY: ', res.body, ' RESPONSE BODY END'); | ||
next(); | ||
} | ||
|
||
module.exports = { | ||
getResponse | ||
}; |
21 changes: 21 additions & 0 deletions
21
packages/artillery/test/integration/core/fixtures/http-file-upload.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
config: | ||
phases: | ||
- duration: 1 | ||
arrivalRate: 1 | ||
processor: "../fixtures/http-file-upload-processor.js" | ||
|
||
variables: | ||
filename: | ||
- "artillery-installation.pdf" | ||
scenarios: | ||
- name: "Hello" | ||
flow: | ||
- post: | ||
url: "/upload" | ||
afterResponse: "getResponse" | ||
formData: | ||
name: "Artillery" | ||
logo: | ||
fromFile: "./files/artillery-logo.jpg" | ||
guide: | ||
fromFile: "./files/{{ filename }}" |
126 changes: 126 additions & 0 deletions
126
packages/artillery/test/integration/core/http-file-upload.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
'use strict'; | ||
|
||
const { createTestServer } = require('../../targets/http-file-upload-server'); | ||
const { test, beforeEach, afterEach } = require('tap'); | ||
const fs = require('fs'); | ||
const crypto = require('crypto'); | ||
const { $ } = require('zx'); | ||
|
||
let server; | ||
let port; | ||
|
||
beforeEach(async () => { | ||
server = await createTestServer(); | ||
port = server.info.port; | ||
}); | ||
|
||
afterEach((t) => { | ||
server.stop(); | ||
}); | ||
|
||
async function calculateFileHash(filePath) { | ||
return new Promise((resolve, reject) => { | ||
const hash = crypto.createHash('sha256'); | ||
const stream = fs.createReadStream(filePath); | ||
|
||
stream.on('data', (data) => hash.update(data)); | ||
stream.on('end', () => resolve(hash.digest('hex'))); | ||
stream.on('error', reject); | ||
}); | ||
} | ||
|
||
test('HTTP engine successfully handles file uploads', async (t) => { | ||
const expectedFiles = [ | ||
{ | ||
fieldName: 'guide', | ||
fileName: 'artillery-installation.pdf', | ||
contentType: 'application/pdf' | ||
}, | ||
{ | ||
fieldName: 'logo', | ||
fileName: 'artillery-logo.jpg', | ||
contentType: 'image/jpeg' | ||
} | ||
]; | ||
|
||
const expectedOtherFields = { | ||
name: 'Artillery' | ||
}; | ||
|
||
const override = { | ||
config: { | ||
target: `http://127.0.0.1:${port}` | ||
} | ||
}; | ||
|
||
/// Run the test | ||
let output; | ||
try { | ||
output = | ||
await $`artillery run ${__dirname}/fixtures/http-file-upload.yml --overrides ${JSON.stringify( | ||
override | ||
)}`; | ||
} catch (err) { | ||
console.error('There has been an error in test run execution: ', err); | ||
t.fail(err); | ||
} | ||
// We log the response body from the processor so we can parse it from output | ||
const match = output.stdout.match(/RESPONSE BODY: (.*) RESPONSE BODY END/s); | ||
let data; | ||
if (match) { | ||
try { | ||
data = JSON.parse(match[1].trim()); | ||
} catch (err) { | ||
console.error('Error parsing response body: ', err); | ||
} | ||
} else { | ||
console.error('Response body not found in output'); | ||
} | ||
|
||
const files = data?.files; | ||
const fields = data?.fields; | ||
t.ok( | ||
data?.files && data?.fields, | ||
'Should successfully upload a combination of file and non-file form fields' | ||
); | ||
t.equal(data.status, 'success', 'Should have a success status'); | ||
t.equal( | ||
files.length, | ||
expectedFiles.length, | ||
`${expectedFiles.length} files should be uploaded` | ||
); | ||
t.match(fields, expectedOtherFields, 'Should have the expected other fields'); | ||
|
||
for (const expectedFile of expectedFiles) { | ||
const uploadedFile = files.find( | ||
(f) => f.fieldName === expectedFile.fieldName | ||
); | ||
|
||
if (!uploadedFile) { | ||
t.fail( | ||
`Could not find uploaded file with fieldName ${expectedFile.fieldName}` | ||
); | ||
continue; | ||
} | ||
|
||
const expectedHash = await calculateFileHash( | ||
`${__dirname}/fixtures/files/${expectedFile.fileName}` | ||
); | ||
|
||
t.equal( | ||
uploadedFile.originalFilename, | ||
expectedFile.fileName, | ||
`Should have uploaded the ${expectedFile.fileName} file under the correct field` | ||
); | ||
t.equal( | ||
uploadedFile.fileHash, | ||
expectedHash, | ||
'Uploaded file should match the sent file' | ||
); | ||
t.equal( | ||
uploadedFile.headers['content-type'], | ||
expectedFile.contentType, | ||
'Should have uploaded file with correct content type' | ||
); | ||
} | ||
}); |
85 changes: 85 additions & 0 deletions
85
packages/artillery/test/targets/http-file-upload-server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const Hapi = require('@hapi/hapi'); | ||
const path = require('path'); | ||
const crypto = require('crypto'); | ||
|
||
const createTestServer = async (port) => { | ||
const server = Hapi.server({ | ||
port: port || 0, | ||
host: '127.0.0.1' | ||
}); | ||
|
||
server.route({ | ||
method: 'GET', | ||
path: '/', | ||
handler: (request, h) => { | ||
return { | ||
status: 'success', | ||
message: 'Hello!' | ||
}; | ||
} | ||
}); | ||
|
||
server.route({ | ||
method: 'POST', | ||
path: '/upload', | ||
options: { | ||
payload: { | ||
maxBytes: 10485760, // 10 MB | ||
output: 'stream', | ||
parse: true, | ||
multipart: { | ||
output: 'stream' | ||
} | ||
} | ||
}, | ||
handler: async (request, h) => { | ||
const data = request.payload; | ||
const files = []; | ||
const fields = {}; | ||
|
||
for (const key in data) { | ||
if (!data[key].hapi || !data[key]._data) { | ||
// Handle non-file fields | ||
fields[key] = data[key]; | ||
continue; | ||
} | ||
|
||
// Handle file fields | ||
const file = data[key]; | ||
const filename = path.basename(file.hapi.filename); | ||
|
||
// calculate a hash of the file so it can be compared in tests | ||
const hash = crypto.createHash('sha256'); | ||
await new Promise((resolve, reject) => { | ||
file.on('end', () => resolve()); | ||
file.on('error', (err) => reject(err)); | ||
file.on('data', (chunk) => { | ||
hash.update(chunk); | ||
}); | ||
}); | ||
|
||
files.push({ | ||
fieldName: key, | ||
originalFilename: filename, | ||
fileHash: hash.digest('hex'), | ||
headers: file.hapi.headers | ||
}); | ||
} | ||
|
||
return { | ||
status: 'success', | ||
message: 'Files and fields uploaded successfully', | ||
files, | ||
fields | ||
}; | ||
} | ||
}); | ||
|
||
await server.start(); | ||
console.log(`File upload server listening on ${server.info.uri}`); | ||
return server; | ||
}; | ||
|
||
module.exports = { | ||
createTestServer | ||
}; |
Oops, something went wrong.