gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
 
const path = require('path');
const request = require('supertest');
const assert = require('assert');
const server = require('./server');
const clearUploadsDir = server.clearUploadsDir;
const fileDir = server.fileDir;
 
describe('fileLimitUloads: Test Single File Upload With File Size Limit', function() {
  let app, limitHandlerRun;
 
  beforeEach(function() {
    clearUploadsDir();
  });
 
  describe('abort connection on limit reached', function() {
    before(function() {
      app = server.setup({
        limits: {fileSize: 200 * 1024}, // set 200kb upload limit
        abortOnLimit: true
      });
    });
 
    it(`upload 'basketball.png' (~154kb) with 200kb size limit`, function(done) {
      let filePath = path.join(fileDir, 'basketball.png');
 
      request(app)
        .post('/upload/single/truncated')
        .attach('testFile', filePath)
        .expect(200)
        .end(done);
    });
 
    it(`fail when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) {
      let filePath = path.join(fileDir, 'car.png');
 
      request(app)
        .post('/upload/single/truncated')
        .attach('testFile', filePath)
        .expect(413)
        .end((err) => {
          // err.code === 'ECONNRESET' that means upload has been aborted.
          done(err && err.code !== 'ECONNRESET' ? err : null);
        });
    });
  });
 
  describe('Run limitHandler on limit reached.', function(){
    before(function() {
      app = server.setup({
        limits: {fileSize: 200 * 1024},     // set 200kb upload limit
        limitHandler: (req, res) => { // set limit handler
          res.writeHead(500, { Connection: 'close', 'Content-Type': 'application/json'});
          res.end(JSON.stringify({response: 'Limit reached!'}));
          limitHandlerRun = true;
        }
      });
    });
 
    it(`Run limit handler when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) {
      let filePath = path.join(fileDir, 'car.png');
      limitHandlerRun = false;
 
      request(app)
        .post('/upload/single/truncated')
        .attach('testFile', filePath)
        .expect(500, {response: 'Limit reached!'})
        .end(function(err) {
          // err.code === 'ECONNRESET' that means upload has been aborted.
          if (err && err.code !== 'ECONNRESET') return done(err);
          if (!limitHandlerRun) return done('handler did not run');
          done();
        });
    });
 
  });
 
  describe('pass truncated file to the next handler', function() {
    before(function() {
      app = server.setup({
        limits: {fileSize: 200 * 1024} // set 200kb upload limit
      });
    });
 
    it(`fail when uploading 'car.png' (~269kb) with 200kb size limit`, function(done) {
      let filePath = path.join(fileDir, 'car.png');
 
      request(app)
        .post('/upload/single/truncated')
        .attach('testFile', filePath)
        .expect(400)
        .end(function(err, res) {
          assert.ok(res.error.text === 'File too big');
          done();
        });
    });
  });
});