multipartUploads.spec.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. 'use strict';
  2. const fs = require('fs');
  3. const md5 = require('md5');
  4. const path = require('path');
  5. const request = require('supertest');
  6. const server = require('./server');
  7. const clearUploadsDir = server.clearUploadsDir;
  8. const fileDir = server.fileDir;
  9. const uploadDir = server.uploadDir;
  10. const mockFiles = ['car.png', 'tree.png', 'basketball.png'];
  11. let mockUser = {
  12. firstName: 'Joe',
  13. lastName: 'Schmo',
  14. email: 'joe@mailinator.com'
  15. };
  16. // Reset response body.uploadDir/uploadPath for testing.
  17. const resetBodyUploadData = (res)=>{
  18. res.body.uploadDir = '';
  19. res.body.uploadPath = '';
  20. };
  21. describe('Test Directory Cleaning Method', function() {
  22. it('emptied "uploads" directory', function(done) {
  23. clearUploadsDir();
  24. let filesFound = fs.readdirSync(uploadDir).length;
  25. done(filesFound ? `Directory not empty. Found ${filesFound} files.` : null);
  26. });
  27. });
  28. describe('Test Single File Upload', function() {
  29. const app = server.setup();
  30. for (let i = 0; i < mockFiles.length; i++) {
  31. let fileName = mockFiles[i];
  32. it(`upload ${fileName} with POST`, function(done) {
  33. let filePath = path.join(fileDir, fileName);
  34. let fileBuffer = fs.readFileSync(filePath);
  35. let fileHash = md5(fileBuffer);
  36. let fileStat = fs.statSync(filePath);
  37. let uploadedFilePath = path.join(uploadDir, fileName);
  38. clearUploadsDir();
  39. request(app)
  40. .post('/upload/single')
  41. .attach('testFile', filePath)
  42. .expect(resetBodyUploadData)
  43. .expect(200, {
  44. name: fileName,
  45. md5: fileHash,
  46. size: fileStat.size,
  47. uploadDir: '',
  48. uploadPath: ''
  49. })
  50. .end(function(err) {
  51. if (err) {
  52. return done(err);
  53. }
  54. fs.stat(uploadedFilePath, done);
  55. });
  56. });
  57. it(`upload ${fileName} with PUT`, function(done) {
  58. let filePath = path.join(fileDir, fileName);
  59. let fileBuffer = fs.readFileSync(filePath);
  60. let fileHash = md5(fileBuffer);
  61. let fileStat = fs.statSync(filePath);
  62. let uploadedFilePath = path.join(uploadDir, fileName);
  63. clearUploadsDir();
  64. request(app)
  65. .post('/upload/single')
  66. .attach('testFile', filePath)
  67. .expect(resetBodyUploadData)
  68. .expect(200, {
  69. name: fileName,
  70. md5: fileHash,
  71. size: fileStat.size,
  72. uploadDir: '',
  73. uploadPath: ''
  74. })
  75. .end(function(err) {
  76. if (err) {
  77. return done(err);
  78. }
  79. fs.stat(uploadedFilePath, done);
  80. });
  81. });
  82. }
  83. it('fail when no files were attached', function(done) {
  84. request(app)
  85. .post('/upload/single')
  86. .expect(400)
  87. .end(done);
  88. });
  89. it('fail when using GET', function(done) {
  90. let filePath = path.join(fileDir, mockFiles[0]);
  91. request(app)
  92. .get('/upload/single')
  93. .attach('testFile', filePath)
  94. .expect(400)
  95. .end(done);
  96. });
  97. it('fail when using HEAD', function(done) {
  98. let filePath = path.join(fileDir, mockFiles[0]);
  99. request(app)
  100. .head('/upload/single')
  101. .attach('testFile', filePath)
  102. .expect(400)
  103. .end(done);
  104. });
  105. });
  106. describe('Test Single File Upload w/ .mv()', function() {
  107. const app = server.setup();
  108. for (let i = 0; i < mockFiles.length; i++) {
  109. let fileName = mockFiles[i];
  110. it(`upload ${fileName} with POST w/ .mv()`, function(done) {
  111. let filePath = path.join(fileDir, fileName);
  112. let fileBuffer = fs.readFileSync(filePath);
  113. let fileHash = md5(fileBuffer);
  114. let fileStat = fs.statSync(filePath);
  115. let uploadedFilePath = path.join(uploadDir, fileName);
  116. clearUploadsDir();
  117. request(app)
  118. .post('/upload/single')
  119. .attach('testFile', filePath)
  120. .expect(resetBodyUploadData)
  121. .expect(200, {
  122. name: fileName,
  123. md5: fileHash,
  124. size: fileStat.size,
  125. uploadDir: '',
  126. uploadPath: ''
  127. })
  128. .end(function(err) {
  129. if (err) {
  130. return done(err);
  131. }
  132. fs.stat(uploadedFilePath, done);
  133. });
  134. });
  135. it(`upload ${fileName} with PUT w/ .mv()`, function(done) {
  136. let filePath = path.join(fileDir, fileName);
  137. let fileBuffer = fs.readFileSync(filePath);
  138. let fileHash = md5(fileBuffer);
  139. let fileStat = fs.statSync(filePath);
  140. let uploadedFilePath = path.join(uploadDir, fileName);
  141. clearUploadsDir();
  142. request(app)
  143. .post('/upload/single')
  144. .attach('testFile', filePath)
  145. .expect(resetBodyUploadData)
  146. .expect(200, {
  147. name: fileName,
  148. md5: fileHash,
  149. size: fileStat.size,
  150. uploadDir: '',
  151. uploadPath: ''
  152. })
  153. .end(function(err) {
  154. if (err) {
  155. return done(err);
  156. }
  157. fs.stat(uploadedFilePath, done);
  158. });
  159. });
  160. }
  161. });
  162. describe('Test Single File Upload with useTempFiles option.', function() {
  163. const app = server.setup({
  164. useTempFiles: true,
  165. tempFileDir: '/tmp/'
  166. });
  167. for (let i = 0; i < mockFiles.length; i++) {
  168. let fileName = mockFiles[i];
  169. it(`upload ${fileName} with POST`, function(done) {
  170. let filePath = path.join(fileDir, fileName);
  171. let fileBuffer = fs.readFileSync(filePath);
  172. let fileHash = md5(fileBuffer);
  173. let fileStat = fs.statSync(filePath);
  174. let uploadedFilePath = path.join(uploadDir, fileName);
  175. clearUploadsDir();
  176. request(app)
  177. .post('/upload/single')
  178. .attach('testFile', filePath)
  179. .expect(resetBodyUploadData)
  180. .expect(200, {
  181. name: fileName,
  182. md5: fileHash,
  183. size: fileStat.size,
  184. uploadDir: '',
  185. uploadPath: ''
  186. })
  187. .end((err) => {
  188. if (err) return done(err);
  189. fs.stat(uploadedFilePath, done);
  190. });
  191. });
  192. it(`upload ${fileName} with PUT`, function(done) {
  193. let filePath = path.join(fileDir, fileName);
  194. let fileBuffer = fs.readFileSync(filePath);
  195. let fileHash = md5(fileBuffer);
  196. let fileStat = fs.statSync(filePath);
  197. let uploadedFilePath = path.join(uploadDir, fileName);
  198. clearUploadsDir();
  199. request(app)
  200. .post('/upload/single')
  201. .attach('testFile', filePath)
  202. .expect(resetBodyUploadData)
  203. .expect(200, {
  204. name: fileName,
  205. md5: fileHash,
  206. size: fileStat.size,
  207. uploadDir: '',
  208. uploadPath: ''
  209. })
  210. .end((err) => {
  211. if (err) return done(err);
  212. fs.stat(uploadedFilePath, done);
  213. });
  214. });
  215. }
  216. it('fail when no files were attached', function(done) {
  217. request(app)
  218. .post('/upload/single')
  219. .expect(400)
  220. .end(done);
  221. });
  222. it('fail when using GET', function(done) {
  223. let filePath = path.join(fileDir, mockFiles[0]);
  224. request(app)
  225. .get('/upload/single')
  226. .attach('testFile', filePath)
  227. .expect(400)
  228. .end(done);
  229. });
  230. it('fail when using HEAD', function(done) {
  231. let filePath = path.join(fileDir, mockFiles[0]);
  232. request(app)
  233. .head('/upload/single')
  234. .attach('testFile', filePath)
  235. .expect(400)
  236. .end(done);
  237. });
  238. });
  239. describe('Test Single File Upload with useTempFiles option and empty tempFileDir.', function() {
  240. const app = server.setup({
  241. useTempFiles: true,
  242. tempFileDir: ''
  243. });
  244. for (let i = 0; i < mockFiles.length; i++) {
  245. let fileName = mockFiles[i];
  246. it(`upload ${fileName} with POST`, function(done) {
  247. let filePath = path.join(fileDir, fileName);
  248. let fileBuffer = fs.readFileSync(filePath);
  249. let fileHash = md5(fileBuffer);
  250. let fileStat = fs.statSync(filePath);
  251. let uploadedFilePath = path.join(uploadDir, fileName);
  252. clearUploadsDir();
  253. request(app)
  254. .post('/upload/single')
  255. .attach('testFile', filePath)
  256. .expect(resetBodyUploadData)
  257. .expect(200, {
  258. name: fileName,
  259. md5: fileHash,
  260. size: fileStat.size,
  261. uploadDir: '',
  262. uploadPath: ''
  263. })
  264. .end((err) => {
  265. if (err) return done(err);
  266. fs.stat(uploadedFilePath, done);
  267. });
  268. });
  269. }
  270. });
  271. describe('Test Single File Upload w/ .mv() Promise', function() {
  272. const app = server.setup();
  273. for (let i = 0; i < mockFiles.length; i++) {
  274. let fileName = mockFiles[i];
  275. it(`upload ${fileName} with POST w/ .mv() Promise`, function(done) {
  276. let filePath = path.join(fileDir, fileName);
  277. let fileBuffer = fs.readFileSync(filePath);
  278. let fileHash = md5(fileBuffer);
  279. let fileStat = fs.statSync(filePath);
  280. let uploadedFilePath = path.join(uploadDir, fileName);
  281. clearUploadsDir();
  282. request(app)
  283. .post('/upload/single/promise')
  284. .attach('testFile', filePath)
  285. .expect(resetBodyUploadData)
  286. .expect(200, {
  287. name: fileName,
  288. md5: fileHash,
  289. size: fileStat.size,
  290. uploadDir: '',
  291. uploadPath: ''
  292. })
  293. .end((err) => {
  294. if (err) return done(err);
  295. fs.stat(uploadedFilePath, done);
  296. });
  297. });
  298. it(`upload ${fileName} with PUT w/ .mv() Promise`, function(done) {
  299. let filePath = path.join(fileDir, fileName);
  300. let fileBuffer = fs.readFileSync(filePath);
  301. let fileHash = md5(fileBuffer);
  302. let fileStat = fs.statSync(filePath);
  303. let uploadedFilePath = path.join(uploadDir, fileName);
  304. clearUploadsDir();
  305. request(app)
  306. .post('/upload/single/promise')
  307. .attach('testFile', filePath)
  308. .expect(resetBodyUploadData)
  309. .expect(200, {
  310. name: fileName,
  311. md5: fileHash,
  312. size: fileStat.size,
  313. uploadDir: '',
  314. uploadPath: ''
  315. })
  316. .end((err) => {
  317. if (err) return done(err);
  318. fs.stat(uploadedFilePath, done);
  319. });
  320. });
  321. }
  322. it('fail when no files were attached', function(done) {
  323. request(app)
  324. .post('/upload/single')
  325. .expect(400)
  326. .end(done);
  327. });
  328. it('fail when using GET', function(done) {
  329. let filePath = path.join(fileDir, mockFiles[0]);
  330. request(app)
  331. .get('/upload/single')
  332. .attach('testFile', filePath)
  333. .expect(400)
  334. .end(done);
  335. });
  336. it('fail when using HEAD', function(done) {
  337. let filePath = path.join(fileDir, mockFiles[0]);
  338. request(app)
  339. .head('/upload/single')
  340. .attach('testFile', filePath)
  341. .expect(400)
  342. .end(done);
  343. });
  344. });
  345. describe('Test Single File Upload w/ .mv() Promise and useTempFiles set to true', function() {
  346. const app = server.setup({
  347. useTempFiles: true,
  348. tempFileDir: '/tmp/'
  349. });
  350. for (let i = 0; i < mockFiles.length; i++) {
  351. let fileName = mockFiles[i];
  352. it(`upload ${fileName} with POST w/ .mv() Promise`, function(done) {
  353. let filePath = path.join(fileDir, fileName);
  354. let fileBuffer = fs.readFileSync(filePath);
  355. let fileHash = md5(fileBuffer);
  356. let fileStat = fs.statSync(filePath);
  357. let uploadedFilePath = path.join(uploadDir, fileName);
  358. clearUploadsDir();
  359. request(app)
  360. .post('/upload/single/promise')
  361. .attach('testFile', filePath)
  362. .expect(resetBodyUploadData)
  363. .expect(200, {
  364. name: fileName,
  365. md5: fileHash,
  366. size: fileStat.size,
  367. uploadDir: '',
  368. uploadPath: ''
  369. })
  370. .end((err) => {
  371. if (err) return done(err);
  372. fs.stat(uploadedFilePath, done);
  373. });
  374. });
  375. it(`upload ${fileName} with PUT w/ .mv() Promise`, function(done) {
  376. let filePath = path.join(fileDir, fileName);
  377. let fileBuffer = fs.readFileSync(filePath);
  378. let fileHash = md5(fileBuffer);
  379. let fileStat = fs.statSync(filePath);
  380. let uploadedFilePath = path.join(uploadDir, fileName);
  381. clearUploadsDir();
  382. request(app)
  383. .post('/upload/single/promise')
  384. .attach('testFile', filePath)
  385. .expect(resetBodyUploadData)
  386. .expect(200, {
  387. name: fileName,
  388. md5: fileHash,
  389. size: fileStat.size,
  390. uploadDir: '',
  391. uploadPath: ''
  392. })
  393. .end((err) => {
  394. if (err) return done(err);
  395. fs.stat(uploadedFilePath, done);
  396. });
  397. });
  398. }
  399. it('fail when no files were attached', function(done) {
  400. request(app)
  401. .post('/upload/single')
  402. .expect(400)
  403. .end(done);
  404. });
  405. it('fail when using GET', function(done) {
  406. let filePath = path.join(fileDir, mockFiles[0]);
  407. request(app)
  408. .get('/upload/single')
  409. .attach('testFile', filePath)
  410. .expect(400)
  411. .end(done);
  412. });
  413. it('fail when using HEAD', function(done) {
  414. let filePath = path.join(fileDir, mockFiles[0]);
  415. request(app)
  416. .head('/upload/single')
  417. .attach('testFile', filePath)
  418. .expect(400)
  419. .end(done);
  420. });
  421. });
  422. describe('Test Multi-File Upload', function() {
  423. const app = server.setup();
  424. it('upload multiple files with POST', function(done) {
  425. let req = request(app).post('/upload/multiple');
  426. clearUploadsDir();
  427. let expectedResult = [];
  428. let expectedResultSorted = [];
  429. let uploadedFilesPath = [];
  430. mockFiles.forEach((fileName, index) => {
  431. let filePath = path.join(fileDir, fileName);
  432. let fileStat = fs.statSync(filePath);
  433. uploadedFilesPath.push(path.join(uploadDir, fileName));
  434. expectedResult.push({
  435. name:fileName,
  436. md5: md5(fs.readFileSync(filePath)),
  437. size: fileStat.size,
  438. uploadDir: '',
  439. uploadPath: ''
  440. });
  441. req.attach(`testFile${index+1}`, filePath);
  442. });
  443. req
  444. .expect((res) => {
  445. res.body.forEach(fileInfo => {
  446. fileInfo.uploadDir = '';
  447. fileInfo.uploadPath = '';
  448. let index = mockFiles.indexOf(fileInfo.name);
  449. expectedResultSorted.push(expectedResult[index]);
  450. });
  451. })
  452. .expect(200, expectedResultSorted)
  453. .end((err) => {
  454. if (err) return done(err);
  455. fs.stat(uploadedFilesPath[0], (err) => {
  456. if (err) return done(err);
  457. fs.stat(uploadedFilesPath[1], function(err) {
  458. if (err) return done(err);
  459. fs.stat(uploadedFilesPath[2], done);
  460. });
  461. });
  462. });
  463. });
  464. });
  465. describe('Test File Array Upload', function() {
  466. const app = server.setup();
  467. it('upload array of files with POST', function(done) {
  468. let req = request(app).post('/upload/array');
  469. clearUploadsDir();
  470. let expectedResult = [];
  471. let expectedResultSorted = [];
  472. let uploadedFilesPath = [];
  473. mockFiles.forEach((fileName) => {
  474. let filePath = path.join(fileDir, fileName);
  475. let fileStat = fs.statSync(filePath);
  476. uploadedFilesPath.push(path.join(uploadDir, fileName));
  477. expectedResult.push({
  478. name:fileName,
  479. md5: md5(fs.readFileSync(filePath)),
  480. size: fileStat.size,
  481. uploadDir: '',
  482. uploadPath: ''
  483. });
  484. req.attach('testFiles', filePath);
  485. });
  486. req
  487. .expect((res)=>{
  488. res.body.forEach(fileInfo => {
  489. fileInfo.uploadDir = '';
  490. fileInfo.uploadPath = '';
  491. let index = mockFiles.indexOf(fileInfo.name);
  492. expectedResultSorted.push(expectedResult[index]);
  493. });
  494. })
  495. .expect(200, expectedResultSorted)
  496. .end((err) => {
  497. if (err) return done(err);
  498. uploadedFilesPath.forEach((uploadedFilePath) => {
  499. fs.statSync(uploadedFilePath);
  500. });
  501. done();
  502. });
  503. });
  504. });
  505. describe('Test Upload With Fields', function() {
  506. const app = server.setup();
  507. for (let i = 0; i < mockFiles.length; i++) {
  508. let fileName = mockFiles[i];
  509. it(`upload ${fileName} and submit fields at the same time with POST`, function(done) {
  510. let filePath = path.join(fileDir, fileName);
  511. let fileBuffer = fs.readFileSync(filePath);
  512. let fileHash = md5(fileBuffer);
  513. let fileStat = fs.statSync(filePath);
  514. let uploadedFilePath = path.join(uploadDir, fileName);
  515. clearUploadsDir();
  516. request(app)
  517. .post('/upload/single/withfields')
  518. .attach('testFile', filePath)
  519. .field('firstName', mockUser.firstName)
  520. .field('lastName', mockUser.lastName)
  521. .field('email', mockUser.email)
  522. .expect(resetBodyUploadData)
  523. .expect(200, {
  524. firstName: mockUser.firstName,
  525. lastName: mockUser.lastName,
  526. email: mockUser.email,
  527. name: fileName,
  528. md5: fileHash,
  529. size: fileStat.size,
  530. uploadDir: '',
  531. uploadPath: ''
  532. },
  533. function(err) {
  534. if (err) return done(err);
  535. fs.stat(uploadedFilePath, done);
  536. });
  537. });
  538. it(`upload ${fileName} and submit fields at the same time with PUT`, function(done) {
  539. let filePath = path.join(fileDir, fileName);
  540. let fileBuffer = fs.readFileSync(filePath);
  541. let fileStat = fs.statSync(filePath);
  542. let fileHash = md5(fileBuffer);
  543. let uploadedFilePath = path.join(uploadDir, fileName);
  544. clearUploadsDir();
  545. request(app)
  546. .put('/upload/single/withfields')
  547. .attach('testFile', filePath)
  548. .field('firstName', mockUser.firstName)
  549. .field('lastName', mockUser.lastName)
  550. .field('email', mockUser.email)
  551. .expect(resetBodyUploadData)
  552. .expect(200, {
  553. firstName: mockUser.firstName,
  554. lastName: mockUser.lastName,
  555. email: mockUser.email,
  556. name: fileName,
  557. md5: fileHash,
  558. size: fileStat.size,
  559. uploadDir: '',
  560. uploadPath: ''
  561. },
  562. function(err) {
  563. if (err) return done(err);
  564. fs.stat(uploadedFilePath, done);
  565. });
  566. });
  567. }
  568. });