|
2 | 2 |
|
3 | 3 | const path = require('path'); |
4 | 4 | const request = require('supertest'); |
| 5 | +const assert = require('assert'); |
5 | 6 | const server = require('./server'); |
6 | | -const app = server.setup({ |
7 | | - limits: {fileSize: 200 * 1024} // set 200kb upload limit |
8 | | -}); |
9 | 7 | const clearUploadsDir = server.clearUploadsDir; |
10 | 8 | const fileDir = server.fileDir; |
11 | 9 |
|
12 | 10 | describe('Test Single File Upload With File Size Limit', function() { |
13 | | - it(`upload 'basketball.png' (~154kb) with 200kb size limit`, function(done) { |
14 | | - let filePath = path.join(fileDir, 'basketball.png'); |
| 11 | + let app; |
15 | 12 |
|
| 13 | + beforeEach(function() { |
16 | 14 | clearUploadsDir(); |
| 15 | + }); |
| 16 | + |
| 17 | + describe('abort connection on limit reached', function() { |
| 18 | + before(function() { |
| 19 | + app = server.setup({ |
| 20 | + limits: {fileSize: 200 * 1024}, // set 200kb upload limit |
| 21 | + abortOnLimit: true |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it(`upload 'basketball.png' (~154kb) with 200kb size limit`, function(done) { |
| 26 | + let filePath = path.join(fileDir, 'basketball.png'); |
17 | 27 |
|
18 | | - request(app) |
19 | | - .post('/upload/single') |
20 | | - .attach('testFile', filePath) |
21 | | - .expect(200) |
22 | | - .end(done); |
| 28 | + request(app) |
| 29 | + .post('/upload/single/truncated') |
| 30 | + .attach('testFile', filePath) |
| 31 | + .expect(200) |
| 32 | + .end(done); |
| 33 | + }); |
| 34 | + |
| 35 | + it(`fail when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) { |
| 36 | + let filePath = path.join(fileDir, 'car.png'); |
| 37 | + |
| 38 | + request(app) |
| 39 | + .post('/upload/single/truncated') |
| 40 | + .attach('testFile', filePath) |
| 41 | + .expect(413) |
| 42 | + .end(done); |
| 43 | + }); |
23 | 44 | }); |
24 | 45 |
|
25 | | - it(`fail when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) { |
26 | | - let filePath = path.join(fileDir, 'car.png'); |
| 46 | + describe('pass truncated file to the next handler', function() { |
| 47 | + before(function() { |
| 48 | + app = server.setup({ |
| 49 | + limits: {fileSize: 200 * 1024} // set 200kb upload limit |
| 50 | + }); |
| 51 | + }); |
27 | 52 |
|
28 | | - clearUploadsDir(); |
| 53 | + it(`fail when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) { |
| 54 | + let filePath = path.join(fileDir, 'car.png'); |
29 | 55 |
|
30 | | - request(app) |
31 | | - .post('/upload/single') |
32 | | - .attach('testFile', filePath) |
33 | | - .expect(413) |
34 | | - .end(done); |
| 56 | + request(app) |
| 57 | + .post('/upload/single/truncated') |
| 58 | + .attach('testFile', filePath) |
| 59 | + .expect(400) |
| 60 | + .end(function(err, res) { |
| 61 | + assert.ok(res.error.text === 'File too big'); |
| 62 | + done(); |
| 63 | + }); |
| 64 | + }); |
35 | 65 | }); |
36 | 66 | }); |
0 commit comments