-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: saveRequestFiles support options (#37)
- Loading branch information
Showing
10 changed files
with
112 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const formstream = require('formstream'); | ||
const urllib = require('urllib'); | ||
const mock = require('egg-mock'); | ||
const rimraf = require('mz-modules/rimraf'); | ||
|
||
describe('test/dynamic-option.test.js', () => { | ||
let app; | ||
let server; | ||
let host; | ||
before(() => { | ||
app = mock.app({ | ||
baseDir: 'apps/dynamic-option', | ||
}); | ||
return app.ready(); | ||
}); | ||
before(() => { | ||
server = app.listen(); | ||
host = 'http://127.0.0.1:' + server.address().port; | ||
}); | ||
after(() => { | ||
return rimraf(app.config.multipart.tmpdir); | ||
}); | ||
after(() => app.close()); | ||
after(() => server.close()); | ||
beforeEach(() => app.mockCsrf()); | ||
afterEach(mock.restore); | ||
|
||
it('should work with saveRequestFiles options', async () => { | ||
const form = formstream(); | ||
form.buffer('file', Buffer.alloc(1 * 1024 * 1024), '1mb.js', 'application/octet-stream'); | ||
|
||
const headers = form.headers(); | ||
const res = await urllib.request(host + '/upload', { | ||
method: 'POST', | ||
headers, | ||
stream: form, | ||
}); | ||
|
||
assert(res.status === 413); | ||
assert(res.data.toString().includes('Request_fileSize_limitError: Reach fileSize limit')); | ||
}); | ||
}); |
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
18 changes: 18 additions & 0 deletions
18
test/fixtures/apps/dynamic-option/app/controller/upload.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,18 @@ | ||
'use strict'; | ||
|
||
module.exports = (app) => { | ||
return class extends app.Controller { | ||
async index(ctx) { | ||
try { | ||
const file = await ctx.saveRequestFiles({ | ||
limits: { | ||
fileSize: 10000 | ||
} | ||
}); | ||
ctx.body = file; | ||
} finally { | ||
await ctx.cleanupRequestFiles(); | ||
} | ||
}; | ||
} | ||
} |
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,5 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
app.post('/upload', app.controller.upload.index); | ||
}; |
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,8 @@ | ||
<form method="POST" action="/upload?_csrf={{ ctx.csrf | safe }}" enctype="multipart/form-data"> | ||
title: <input name="title" /> | ||
file1: <input name="file1" type="file" /> | ||
file2: <input name="file2" type="file" /> | ||
file3: <input name="file3" type="file" /> | ||
other: <input name="other" /> | ||
<button type="submit">上传</button> | ||
</form> |
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,7 @@ | ||
'use strict'; | ||
|
||
exports.multipart = { | ||
mode: 'stream', | ||
}; | ||
|
||
exports.keys = 'multipart'; |
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,8 @@ | ||
'use strict'; | ||
|
||
exports.logger = { | ||
consoleLevel: 'NONE', | ||
coreLogger: { | ||
// consoleLevel: 'DEBUG', | ||
}, | ||
}; |
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,3 @@ | ||
{ | ||
"name": "dynamic-options-demo" | ||
} |