UNPKG

2.77 kBJavaScriptView Raw
1'use strict';
2
3const path = require('path');
4const request = require('supertest');
5const assert = require('assert');
6const server = require('./server');
7const clearUploadsDir = server.clearUploadsDir;
8const fileDir = server.fileDir;
9
10describe('Test Single File Upload With File Size Limit', function() {
11 let app, limitHandlerRun;
12
13 beforeEach(function() {
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');
27
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 });
44 });
45
46 describe('Run limitHandler on limit reached.', function(){
47 before(function() {
48 app = server.setup({
49 limits: {fileSize: 200 * 1024}, // set 200kb upload limit
50 limitHandler: (req, res) => { // set limit handler
51 res.writeHead(500, { Connection: 'close', 'Content-Type': 'application/json'});
52 res.end(JSON.stringify({response: 'Limit reached!'}));
53 limitHandlerRun = true;
54 }
55 });
56 });
57
58 it(`Run limit handler when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) {
59 let filePath = path.join(fileDir, 'car.png');
60 limitHandlerRun = false;
61
62 request(app)
63 .post('/upload/single/truncated')
64 .attach('testFile', filePath)
65 .expect(500, {response: 'Limit reached!'})
66 .end(function(err){
67 if (err) return done(err);
68 if (!limitHandlerRun) return done('handler did not run');
69 done();
70 });
71 });
72
73 });
74
75 describe('pass truncated file to the next handler', function() {
76 before(function() {
77 app = server.setup({
78 limits: {fileSize: 200 * 1024} // set 200kb upload limit
79 });
80 });
81
82 it(`fail when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) {
83 let filePath = path.join(fileDir, 'car.png');
84
85 request(app)
86 .post('/upload/single/truncated')
87 .attach('testFile', filePath)
88 .expect(400)
89 .end(function(err, res) {
90 assert.ok(res.error.text === 'File too big');
91 done();
92 });
93 });
94 });
95});