fileLimitUploads.spec.js 2.7 KB

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