|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const assert = require('assert'); |
| 5 | +const mm = require('egg-mock'); |
| 6 | +const rimraf = require('mz-modules/rimraf'); |
| 7 | + |
| 8 | +describe('example multipart test', () => { |
| 9 | + let app; |
| 10 | + before(async () => { |
| 11 | + app = mm.app(); |
| 12 | + await app.ready(); |
| 13 | + }); |
| 14 | + after(() => app.close()); |
| 15 | + after(() => rimraf(path.join(app.config.baseDir, 'app/public'))); |
| 16 | + |
| 17 | + describe('form', () => { |
| 18 | + it('should upload', async () => { |
| 19 | + app.mockCsrf(); |
| 20 | + await app.httpRequest() |
| 21 | + .post('/form') |
| 22 | + .field('name', 'form') |
| 23 | + .attach('file', path.join(__dirname, 'mc.jpeg')) |
| 24 | + .expect('Location', '/public/form.jpeg') |
| 25 | + .expect(302); |
| 26 | + |
| 27 | + await app.httpRequest() |
| 28 | + .get('/public/form.jpeg') |
| 29 | + .expect('content-length', '6635') |
| 30 | + .expect(200); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('multiple', () => { |
| 35 | + it('should upload', async () => { |
| 36 | + app.mockCsrf(); |
| 37 | + await app.httpRequest() |
| 38 | + .post('/multiple-file') |
| 39 | + .field('name1', '1') |
| 40 | + .attach('file1', path.join(__dirname, 'mc.jpeg')) |
| 41 | + .field('name2', '2') |
| 42 | + .attach('file2', path.join(__dirname, 'kfc.jpeg')) |
| 43 | + .field('name3', '3') |
| 44 | + .expect(res => { |
| 45 | + assert(res.text.includes('name1: 1')); |
| 46 | + assert(res.text.includes('name2: 2')); |
| 47 | + assert(res.text.includes('name3: 3')); |
| 48 | + assert(res.text.includes('href="/public/mc.jpeg"')); |
| 49 | + assert(res.text.includes('href="/public/kfc.jpeg"')); |
| 50 | + }) |
| 51 | + .expect(200); |
| 52 | + |
| 53 | + await app.httpRequest() |
| 54 | + .get('/public/mc.jpeg') |
| 55 | + .expect('content-length', '6635') |
| 56 | + .expect(200); |
| 57 | + |
| 58 | + await app.httpRequest() |
| 59 | + .get('/public/kfc.jpeg') |
| 60 | + .expect('content-length', '28810') |
| 61 | + .expect(200); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('ajax', () => { |
| 66 | + it('should upload', async () => { |
| 67 | + app.mockCsrf(); |
| 68 | + await app.httpRequest() |
| 69 | + .post('/ajax') |
| 70 | + .field('name', 'ajax') |
| 71 | + .attach('file', path.join(__dirname, 'mc.jpeg')) |
| 72 | + .expect({ |
| 73 | + url: '/public/ajax.jpeg', |
| 74 | + }) |
| 75 | + .expect(200); |
| 76 | + |
| 77 | + await app.httpRequest() |
| 78 | + .get('/public/ajax.jpeg') |
| 79 | + .expect('content-length', '6635') |
| 80 | + .expect(200); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments