-
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: support custom pathToRegexpModule (#66)
part of eggjs/core#290 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Dependencies** - Updated `egg-path-matching` to version 1.2.0 - Added `path-to-regexp-v8` dependency - **Middleware** - Enhanced multipart file upload middleware with additional application context configuration - **Testing** - Improved file upload test cases - Updated error message assertions for more flexible matching - Added new test scenarios for file upload endpoints - **Configuration** - Updated file upload handling configuration - Added new routing and controller logic for file uploads <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
14 changed files
with
242 additions
and
13 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
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,135 @@ | ||
const assert = require('assert'); | ||
const formstream = require('formstream'); | ||
const urllib = require('urllib'); | ||
const path = require('path'); | ||
const mock = require('egg-mock'); | ||
const fs = require('fs').promises; | ||
|
||
describe('test/enable-pathToRegexpModule.test.js', () => { | ||
let app; | ||
let server; | ||
let host; | ||
before(() => { | ||
app = mock.app({ | ||
baseDir: 'apps/fileModeMatch-glob-with-pathToRegexpModule', | ||
pathToRegexpModule: require.resolve('path-to-regexp-v8'), | ||
}); | ||
return app.ready(); | ||
}); | ||
before(() => { | ||
server = app.listen(); | ||
host = 'http://127.0.0.1:' + server.address().port; | ||
}); | ||
after(() => { | ||
return fs.rm(app.config.multipart.tmpdir, { force: true, recursive: true }); | ||
}); | ||
after(() => app.close()); | ||
after(() => server.close()); | ||
beforeEach(() => app.mockCsrf()); | ||
afterEach(mock.restore); | ||
|
||
it('should upload match file mode work on /upload_file', async () => { | ||
const form = formstream(); | ||
form.field('foo', 'fengmk2').field('love', 'egg'); | ||
form.file('file1', __filename, 'foooooooo.js'); | ||
form.file('file2', __filename); | ||
// will ignore empty file | ||
form.buffer('file3', Buffer.from(''), '', 'application/octet-stream'); | ||
form.file('bigfile', path.join(__dirname, 'fixtures', 'bigfile.js')); | ||
// other form fields | ||
form.field('work', 'with Node.js'); | ||
|
||
const headers = form.headers(); | ||
const res = await urllib.request(host + '/upload_file', { | ||
method: 'POST', | ||
headers, | ||
stream: form, | ||
}); | ||
|
||
assert(res.status === 200); | ||
const data = JSON.parse(res.data); | ||
assert.deepStrictEqual(data.body, { foo: 'fengmk2', love: 'egg', work: 'with Node.js' }); | ||
assert(data.files.length === 3); | ||
assert(data.files[0].field === 'file1'); | ||
assert(data.files[0].filename === 'foooooooo.js'); | ||
assert(data.files[0].encoding === '7bit'); | ||
assert(data.files[0].mime === 'application/javascript'); | ||
assert(data.files[0].filepath.startsWith(app.config.multipart.tmpdir)); | ||
|
||
assert(data.files[1].field === 'file2'); | ||
assert(data.files[1].filename === 'enable-pathToRegexpModule.test.js'); | ||
assert(data.files[1].encoding === '7bit'); | ||
assert(data.files[1].mime === 'application/javascript'); | ||
assert(data.files[1].filepath.startsWith(app.config.multipart.tmpdir)); | ||
|
||
assert(data.files[2].field === 'bigfile'); | ||
assert(data.files[2].filename === 'bigfile.js'); | ||
assert(data.files[2].encoding === '7bit'); | ||
assert(data.files[2].mime === 'application/javascript'); | ||
assert(data.files[2].filepath.startsWith(app.config.multipart.tmpdir)); | ||
}); | ||
|
||
it('should upload match file mode work on /upload_file/*', async () => { | ||
const form = formstream(); | ||
form.field('foo', 'fengmk2').field('love', 'egg'); | ||
form.file('file1', __filename, 'foooooooo.js'); | ||
form.file('file2', __filename); | ||
// will ignore empty file | ||
form.buffer('file3', Buffer.from(''), '', 'application/octet-stream'); | ||
form.file('bigfile', path.join(__dirname, 'fixtures', 'bigfile.js')); | ||
// other form fields | ||
form.field('work', 'with Node.js'); | ||
|
||
const headers = form.headers(); | ||
const res = await urllib.request(host + '/upload_file/foo', { | ||
method: 'POST', | ||
headers, | ||
stream: form, | ||
}); | ||
|
||
assert.equal(res.status, 200); | ||
const data = JSON.parse(res.data); | ||
assert.deepStrictEqual(data.body, { foo: 'fengmk2', love: 'egg', work: 'with Node.js' }); | ||
assert(data.files.length === 3); | ||
assert(data.files[0].field === 'file1'); | ||
assert(data.files[0].filename === 'foooooooo.js'); | ||
assert(data.files[0].encoding === '7bit'); | ||
assert(data.files[0].mime === 'application/javascript'); | ||
assert(data.files[0].filepath.startsWith(app.config.multipart.tmpdir)); | ||
|
||
assert(data.files[1].field === 'file2'); | ||
assert(data.files[1].filename === 'enable-pathToRegexpModule.test.js'); | ||
assert(data.files[1].encoding === '7bit'); | ||
assert(data.files[1].mime === 'application/javascript'); | ||
assert(data.files[1].filepath.startsWith(app.config.multipart.tmpdir)); | ||
|
||
assert(data.files[2].field === 'bigfile'); | ||
assert(data.files[2].filename === 'bigfile.js'); | ||
assert(data.files[2].encoding === '7bit'); | ||
assert(data.files[2].mime === 'application/javascript'); | ||
assert(data.files[2].filepath.startsWith(app.config.multipart.tmpdir)); | ||
}); | ||
|
||
it('should upload not match file mode', async () => { | ||
const form = formstream(); | ||
form.field('foo', 'fengmk2').field('love', 'egg'); | ||
form.file('file1', __filename, 'foooooooo.js'); | ||
form.file('file2', __filename); | ||
// will ignore empty file | ||
form.buffer('file3', Buffer.from(''), '', 'application/octet-stream'); | ||
form.file('bigfile', path.join(__dirname, 'fixtures', 'bigfile.js')); | ||
// other form fields | ||
form.field('work', 'with Node.js'); | ||
|
||
const headers = form.headers(); | ||
const res = await urllib.request(host + '/upload', { | ||
method: 'POST', | ||
headers, | ||
stream: form, | ||
}); | ||
|
||
assert(res.status === 200); | ||
const data = JSON.parse(res.data); | ||
assert.deepStrictEqual(data, { body: {} }); | ||
}); | ||
}); |
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
9 changes: 9 additions & 0 deletions
9
test/fixtures/apps/fileModeMatch-glob-with-pathToRegexpModule/app/controller/save.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,9 @@ | ||
'use strict'; | ||
|
||
module.exports = async ctx => { | ||
await ctx.saveRequestFiles(); | ||
ctx.body = { | ||
body: ctx.request.body, | ||
files: ctx.request.files, | ||
}; | ||
}; |
8 changes: 8 additions & 0 deletions
8
test/fixtures/apps/fileModeMatch-glob-with-pathToRegexpModule/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,8 @@ | ||
'use strict'; | ||
|
||
module.exports = async ctx => { | ||
ctx.body = { | ||
body: ctx.request.body, | ||
files: ctx.request.files, | ||
}; | ||
}; |
8 changes: 8 additions & 0 deletions
8
test/fixtures/apps/fileModeMatch-glob-with-pathToRegexpModule/app/router.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,8 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
app.post('/upload', app.controller.upload); | ||
app.post('/upload_file', app.controller.upload); | ||
app.post('/upload_file/foo', app.controller.upload); | ||
app.post('/save', app.controller.save); | ||
}; |
8 changes: 8 additions & 0 deletions
8
test/fixtures/apps/fileModeMatch-glob-with-pathToRegexpModule/app/views/home.html
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_file?_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> |
6 changes: 6 additions & 0 deletions
6
test/fixtures/apps/fileModeMatch-glob-with-pathToRegexpModule/config/config.default.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,6 @@ | ||
exports.multipart = { | ||
mode: 'stream', | ||
fileModeMatch: '/upload_file{/:paths}' | ||
}; | ||
|
||
exports.keys = 'multipart'; |
8 changes: 8 additions & 0 deletions
8
test/fixtures/apps/fileModeMatch-glob-with-pathToRegexpModule/config/config.unittest.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,8 @@ | ||
'use strict'; | ||
|
||
exports.logger = { | ||
consoleLevel: 'NONE', | ||
coreLogger: { | ||
// consoleLevel: 'DEBUG', | ||
}, | ||
}; |
3 changes: 3 additions & 0 deletions
3
test/fixtures/apps/fileModeMatch-glob-with-pathToRegexpModule/package.json
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": "fileModeMatch-glob-with-pathToRegexpModule" | ||
} |
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