uploader.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* global jest:false, test:false, expect:false, describe:false */
  2. jest.mock('tus-js-client')
  3. const fs = require('fs')
  4. const Uploader = require('../../src/server/Uploader')
  5. const socketClient = require('../mocksocket')
  6. const standalone = require('../../src/standalone')
  7. const { companionOptions } = standalone()
  8. describe('uploader with tus protocol', () => {
  9. test('uploader respects uploadUrls', async () => {
  10. const opts = {
  11. endpoint: 'http://localhost/files',
  12. companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/url.myendpoint.com\//] },
  13. }
  14. expect(new Uploader(opts).hasError()).toBe(true)
  15. })
  16. test('uploader respects uploadUrls, valid', async () => {
  17. const opts = {
  18. endpoint: 'http://url.myendpoint.com/files',
  19. companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/url.myendpoint.com\//] },
  20. }
  21. expect(new Uploader(opts).hasError()).toBe(false)
  22. })
  23. test('uploader respects uploadUrls, localhost', async () => {
  24. const opts = {
  25. endpoint: 'http://localhost:1337/',
  26. companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/localhost:1337\//] },
  27. }
  28. expect(new Uploader(opts).hasError()).toBe(false)
  29. })
  30. test('upload functions with tus protocol', () => {
  31. const fileContent = Buffer.from('Some file content')
  32. const opts = {
  33. companionOptions,
  34. endpoint: 'http://url.myendpoint.com/files',
  35. protocol: 'tus',
  36. size: fileContent.length,
  37. pathPrefix: companionOptions.filePath,
  38. }
  39. const uploader = new Uploader(opts)
  40. const uploadToken = uploader.token
  41. expect(uploader.hasError()).toBe(false)
  42. expect(uploadToken).toBeTruthy()
  43. return new Promise((resolve) => {
  44. // validate that the test is resolved on socket connection
  45. uploader.onSocketReady(() => {
  46. const fileInfo = fs.statSync(uploader.path)
  47. expect(fileInfo.isFile()).toBe(true)
  48. expect(fileInfo.size).toBe(0)
  49. uploader.handleChunk(null, fileContent)
  50. uploader.handleChunk(null, null)
  51. })
  52. let progressReceived = 0
  53. // emulate socket connection
  54. socketClient.connect(uploadToken)
  55. socketClient.onProgress(uploadToken, (message) => {
  56. // validate that the file has been downloaded and saved into the file path
  57. const fileInfo = fs.statSync(uploader.path)
  58. expect(fileInfo.isFile()).toBe(true)
  59. expect(fileInfo.size).toBe(fileContent.length)
  60. progressReceived = message.payload.bytesUploaded
  61. expect(message.payload.bytesTotal).toBe(fileContent.length)
  62. })
  63. socketClient.onUploadSuccess(uploadToken, (message) => {
  64. expect(progressReceived).toBe(fileContent.length)
  65. // see __mocks__/tus-js-client.js
  66. expect(message.payload.url).toBe('https://tus.endpoint/files/foo-bar')
  67. setTimeout(() => {
  68. // check that file has been cleaned up
  69. expect(fs.existsSync(uploader.path)).toBe(false)
  70. resolve()
  71. }, 100)
  72. })
  73. })
  74. })
  75. })