|
| 1 | +const assert = require('assert'); |
| 2 | +const formstream = require('formstream'); |
| 3 | +const urllib = require('urllib'); |
| 4 | +const path = require('path'); |
| 5 | +const mock = require('egg-mock'); |
| 6 | +const fs = require('fs').promises; |
| 7 | + |
| 8 | +describe('test/enable-pathToRegexpModule.test.js', () => { |
| 9 | + let app; |
| 10 | + let server; |
| 11 | + let host; |
| 12 | + before(() => { |
| 13 | + app = mock.app({ |
| 14 | + baseDir: 'apps/fileModeMatch-glob-with-pathToRegexpModule', |
| 15 | + pathToRegexpModule: require.resolve('path-to-regexp-v8'), |
| 16 | + }); |
| 17 | + return app.ready(); |
| 18 | + }); |
| 19 | + before(() => { |
| 20 | + server = app.listen(); |
| 21 | + host = 'http://127.0.0.1:' + server.address().port; |
| 22 | + }); |
| 23 | + after(() => { |
| 24 | + return fs.rm(app.config.multipart.tmpdir, { force: true, recursive: true }); |
| 25 | + }); |
| 26 | + after(() => app.close()); |
| 27 | + after(() => server.close()); |
| 28 | + beforeEach(() => app.mockCsrf()); |
| 29 | + afterEach(mock.restore); |
| 30 | + |
| 31 | + it('should upload match file mode work on /upload_file', async () => { |
| 32 | + const form = formstream(); |
| 33 | + form.field('foo', 'fengmk2').field('love', 'egg'); |
| 34 | + form.file('file1', __filename, 'foooooooo.js'); |
| 35 | + form.file('file2', __filename); |
| 36 | + // will ignore empty file |
| 37 | + form.buffer('file3', Buffer.from(''), '', 'application/octet-stream'); |
| 38 | + form.file('bigfile', path.join(__dirname, 'fixtures', 'bigfile.js')); |
| 39 | + // other form fields |
| 40 | + form.field('work', 'with Node.js'); |
| 41 | + |
| 42 | + const headers = form.headers(); |
| 43 | + const res = await urllib.request(host + '/upload_file', { |
| 44 | + method: 'POST', |
| 45 | + headers, |
| 46 | + stream: form, |
| 47 | + }); |
| 48 | + |
| 49 | + assert(res.status === 200); |
| 50 | + const data = JSON.parse(res.data); |
| 51 | + assert.deepStrictEqual(data.body, { foo: 'fengmk2', love: 'egg', work: 'with Node.js' }); |
| 52 | + assert(data.files.length === 3); |
| 53 | + assert(data.files[0].field === 'file1'); |
| 54 | + assert(data.files[0].filename === 'foooooooo.js'); |
| 55 | + assert(data.files[0].encoding === '7bit'); |
| 56 | + assert(data.files[0].mime === 'application/javascript'); |
| 57 | + assert(data.files[0].filepath.startsWith(app.config.multipart.tmpdir)); |
| 58 | + |
| 59 | + assert(data.files[1].field === 'file2'); |
| 60 | + assert(data.files[1].filename === 'enable-pathToRegexpModule.test.js'); |
| 61 | + assert(data.files[1].encoding === '7bit'); |
| 62 | + assert(data.files[1].mime === 'application/javascript'); |
| 63 | + assert(data.files[1].filepath.startsWith(app.config.multipart.tmpdir)); |
| 64 | + |
| 65 | + assert(data.files[2].field === 'bigfile'); |
| 66 | + assert(data.files[2].filename === 'bigfile.js'); |
| 67 | + assert(data.files[2].encoding === '7bit'); |
| 68 | + assert(data.files[2].mime === 'application/javascript'); |
| 69 | + assert(data.files[2].filepath.startsWith(app.config.multipart.tmpdir)); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should upload match file mode work on /upload_file/*', async () => { |
| 73 | + const form = formstream(); |
| 74 | + form.field('foo', 'fengmk2').field('love', 'egg'); |
| 75 | + form.file('file1', __filename, 'foooooooo.js'); |
| 76 | + form.file('file2', __filename); |
| 77 | + // will ignore empty file |
| 78 | + form.buffer('file3', Buffer.from(''), '', 'application/octet-stream'); |
| 79 | + form.file('bigfile', path.join(__dirname, 'fixtures', 'bigfile.js')); |
| 80 | + // other form fields |
| 81 | + form.field('work', 'with Node.js'); |
| 82 | + |
| 83 | + const headers = form.headers(); |
| 84 | + const res = await urllib.request(host + '/upload_file/foo', { |
| 85 | + method: 'POST', |
| 86 | + headers, |
| 87 | + stream: form, |
| 88 | + }); |
| 89 | + |
| 90 | + assert.equal(res.status, 200); |
| 91 | + const data = JSON.parse(res.data); |
| 92 | + assert.deepStrictEqual(data.body, { foo: 'fengmk2', love: 'egg', work: 'with Node.js' }); |
| 93 | + assert(data.files.length === 3); |
| 94 | + assert(data.files[0].field === 'file1'); |
| 95 | + assert(data.files[0].filename === 'foooooooo.js'); |
| 96 | + assert(data.files[0].encoding === '7bit'); |
| 97 | + assert(data.files[0].mime === 'application/javascript'); |
| 98 | + assert(data.files[0].filepath.startsWith(app.config.multipart.tmpdir)); |
| 99 | + |
| 100 | + assert(data.files[1].field === 'file2'); |
| 101 | + assert(data.files[1].filename === 'enable-pathToRegexpModule.test.js'); |
| 102 | + assert(data.files[1].encoding === '7bit'); |
| 103 | + assert(data.files[1].mime === 'application/javascript'); |
| 104 | + assert(data.files[1].filepath.startsWith(app.config.multipart.tmpdir)); |
| 105 | + |
| 106 | + assert(data.files[2].field === 'bigfile'); |
| 107 | + assert(data.files[2].filename === 'bigfile.js'); |
| 108 | + assert(data.files[2].encoding === '7bit'); |
| 109 | + assert(data.files[2].mime === 'application/javascript'); |
| 110 | + assert(data.files[2].filepath.startsWith(app.config.multipart.tmpdir)); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should upload not match file mode', async () => { |
| 114 | + const form = formstream(); |
| 115 | + form.field('foo', 'fengmk2').field('love', 'egg'); |
| 116 | + form.file('file1', __filename, 'foooooooo.js'); |
| 117 | + form.file('file2', __filename); |
| 118 | + // will ignore empty file |
| 119 | + form.buffer('file3', Buffer.from(''), '', 'application/octet-stream'); |
| 120 | + form.file('bigfile', path.join(__dirname, 'fixtures', 'bigfile.js')); |
| 121 | + // other form fields |
| 122 | + form.field('work', 'with Node.js'); |
| 123 | + |
| 124 | + const headers = form.headers(); |
| 125 | + const res = await urllib.request(host + '/upload', { |
| 126 | + method: 'POST', |
| 127 | + headers, |
| 128 | + stream: form, |
| 129 | + }); |
| 130 | + |
| 131 | + assert(res.status === 200); |
| 132 | + const data = JSON.parse(res.data); |
| 133 | + assert.deepStrictEqual(data, { body: {} }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments