forked from transloadit/uppy
/
uploader.js
89 lines (75 loc) · 2.96 KB
/
uploader.js
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
/* global jest:false, test:false, expect:false, describe:false */
jest.mock('tus-js-client')
const fs = require('fs')
const Uploader = require('../../src/server/Uploader')
const socketClient = require('../mocksocket')
const standalone = require('../../src/standalone')
const { companionOptions } = standalone()
describe('uploader with tus protocol', () => {
test('uploader respects uploadUrls', async () => {
const opts = {
endpoint: 'http://localhost/files',
companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/url.myendpoint.com\//] },
}
expect(new Uploader(opts).hasError()).toBe(true)
})
test('uploader respects uploadUrls, valid', async () => {
const opts = {
endpoint: 'http://url.myendpoint.com/files',
companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/url.myendpoint.com\//] },
}
expect(new Uploader(opts).hasError()).toBe(false)
})
test('uploader respects uploadUrls, localhost', async () => {
const opts = {
endpoint: 'http://localhost:1337/',
companionOptions: { ...companionOptions, uploadUrls: [/^http:\/\/localhost:1337\//] },
}
expect(new Uploader(opts).hasError()).toBe(false)
})
test('upload functions with tus protocol', () => {
const fileContent = Buffer.from('Some file content')
const opts = {
companionOptions,
endpoint: 'http://url.myendpoint.com/files',
protocol: 'tus',
size: fileContent.length,
pathPrefix: companionOptions.filePath,
}
const uploader = new Uploader(opts)
const uploadToken = uploader.token
expect(uploader.hasError()).toBe(false)
expect(uploadToken).toBeTruthy()
return new Promise((resolve) => {
// validate that the test is resolved on socket connection
uploader.onSocketReady(() => {
const fileInfo = fs.statSync(uploader.path)
expect(fileInfo.isFile()).toBe(true)
expect(fileInfo.size).toBe(0)
uploader.handleChunk(null, fileContent)
uploader.handleChunk(null, null)
})
let progressReceived = 0
// emulate socket connection
socketClient.connect(uploadToken)
socketClient.onProgress(uploadToken, (message) => {
// validate that the file has been downloaded and saved into the file path
const fileInfo = fs.statSync(uploader.path)
expect(fileInfo.isFile()).toBe(true)
expect(fileInfo.size).toBe(fileContent.length)
progressReceived = message.payload.bytesUploaded
expect(message.payload.bytesTotal).toBe(fileContent.length)
})
socketClient.onUploadSuccess(uploadToken, (message) => {
expect(progressReceived).toBe(fileContent.length)
// see __mocks__/tus-js-client.js
expect(message.payload.url).toBe('https://tus.endpoint/files/foo-bar')
setTimeout(() => {
// check that file has been cleaned up
expect(fs.existsSync(uploader.path)).toBe(false)
resolve()
}, 100)
})
})
})
})