|
@@ -2,8 +2,10 @@
|
|
|
|
|
|
jest.mock('tus-js-client')
|
|
|
|
|
|
-const intoStream = require('into-stream')
|
|
|
+const { Readable } = require('node:stream')
|
|
|
const fs = require('node:fs')
|
|
|
+const { createServer } = require('node:http')
|
|
|
+const { once } = require('node:events')
|
|
|
const nock = require('nock')
|
|
|
|
|
|
const Uploader = require('../../src/server/Uploader')
|
|
@@ -52,7 +54,7 @@ describe('uploader with tus protocol', () => {
|
|
|
|
|
|
test('upload functions with tus protocol', async () => {
|
|
|
const fileContent = Buffer.from('Some file content')
|
|
|
- const stream = intoStream(fileContent)
|
|
|
+ const stream = Readable.from([fileContent])
|
|
|
const opts = {
|
|
|
companionOptions,
|
|
|
endpoint: 'http://url.myendpoint.com/files',
|
|
@@ -108,7 +110,7 @@ describe('uploader with tus protocol', () => {
|
|
|
|
|
|
test('upload functions with tus protocol without size', async () => {
|
|
|
const fileContent = Buffer.alloc(1e6)
|
|
|
- const stream = intoStream(fileContent)
|
|
|
+ const stream = Readable.from([fileContent])
|
|
|
const opts = {
|
|
|
companionOptions,
|
|
|
endpoint: 'http://url.myendpoint.com/files',
|
|
@@ -153,7 +155,7 @@ describe('uploader with tus protocol', () => {
|
|
|
})
|
|
|
socketClient.onUploadSuccess(uploadToken, (message) => {
|
|
|
try {
|
|
|
- expect(firstReceivedProgress.bytesUploaded).toBe(8192)
|
|
|
+ expect(firstReceivedProgress.bytesUploaded).toBe(500_000)
|
|
|
|
|
|
// see __mocks__/tus-js-client.js
|
|
|
expect(message.payload.url).toBe('https://tus.endpoint/files/foo-bar')
|
|
@@ -164,13 +166,13 @@ describe('uploader with tus protocol', () => {
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- async function runMultipartTest ({ metadata, useFormData, includeSize = true } = {}) {
|
|
|
+ async function runMultipartTest ({ metadata, useFormData, includeSize = true, address = 'localhost' } = {}) {
|
|
|
const fileContent = Buffer.from('Some file content')
|
|
|
- const stream = intoStream(fileContent)
|
|
|
+ const stream = Readable.from([fileContent])
|
|
|
|
|
|
const opts = {
|
|
|
companionOptions,
|
|
|
- endpoint: 'http://localhost',
|
|
|
+ endpoint: `http://${address}`,
|
|
|
protocol: 'multipart',
|
|
|
size: includeSize ? fileContent.length : undefined,
|
|
|
metadata,
|
|
@@ -183,14 +185,30 @@ describe('uploader with tus protocol', () => {
|
|
|
}
|
|
|
|
|
|
test('upload functions with xhr protocol', async () => {
|
|
|
- nock('http://localhost').post('/').reply(200)
|
|
|
-
|
|
|
- const ret = await runMultipartTest()
|
|
|
- expect(ret).toMatchObject({ url: null, extraData: { response: expect.anything(), bytesUploaded: 17 } })
|
|
|
+ let alreadyCalled = false
|
|
|
+ // We are creating our own test server for this test
|
|
|
+ // instead of using nock because of a bug when passing a Node.js stream to got.
|
|
|
+ // Ref: https://github.com/nock/nock/issues/2595
|
|
|
+ const server = createServer((req,res) => {
|
|
|
+ if (alreadyCalled) throw new Error('already called')
|
|
|
+ alreadyCalled = true
|
|
|
+ if (req.url === '/' && req.method === 'POST') {
|
|
|
+ res.writeHead(200)
|
|
|
+ res.end('OK')
|
|
|
+ }
|
|
|
+ }).listen()
|
|
|
+ try {
|
|
|
+ await once(server, 'listening')
|
|
|
+
|
|
|
+ const ret = await runMultipartTest({ address: `localhost:${server.address().port}` })
|
|
|
+ expect(ret).toMatchObject({ url: null, extraData: { response: expect.anything(), bytesUploaded: 17 } })
|
|
|
+ } finally {
|
|
|
+ server.close()
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
- const formDataNoMetaMatch = /^----------------------------\d+\r\nContent-Disposition: form-data; name="files\[\]"; filename="uppy-file-[^"]+"\r\nContent-Type: application\/octet-stream\r\n\r\nSome file content\r\n----------------------------\d+--\r\n$/
|
|
|
+ const formDataNoMetaMatch = /^--form-data-boundary-[a-z0-9]+\r\nContent-Disposition: form-data; name="files\[\]"; filename="uppy-file-[^"]+"\r\nContent-Type: application\/octet-stream\r\n\r\nSome file content\r\n--form-data-boundary-[a-z0-9]+--\r\n\r\n$/
|
|
|
|
|
|
test('upload functions with xhr formdata', async () => {
|
|
|
nock('http://localhost').post('/', formDataNoMetaMatch)
|
|
@@ -201,7 +219,6 @@ describe('uploader with tus protocol', () => {
|
|
|
})
|
|
|
|
|
|
test('upload functions with unknown file size', async () => {
|
|
|
- // eslint-disable-next-line max-len
|
|
|
nock('http://localhost').post('/', formDataNoMetaMatch)
|
|
|
.reply(200)
|
|
|
|
|
@@ -211,8 +228,7 @@ describe('uploader with tus protocol', () => {
|
|
|
|
|
|
// https://github.com/transloadit/uppy/issues/3477
|
|
|
test('upload functions with xhr formdata and metadata', async () => {
|
|
|
- // eslint-disable-next-line max-len
|
|
|
- nock('http://localhost').post('/', /^----------------------------\d+\r\nContent-Disposition: form-data; name="key1"\r\n\r\nnull\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="key2"\r\n\r\ntrue\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="key3"\r\n\r\n\d+\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="key4"\r\n\r\n\[object Object\]\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="key5"\r\n\r\n\(\) => {}\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="key6"\r\n\r\nSymbol\(\)\r\n----------------------------\d+\r\nContent-Disposition: form-data; name="files\[\]"; filename="uppy-file-[^"]+"\r\nContent-Type: application\/octet-stream\r\n\r\nSome file content\r\n----------------------------\d+--\r\n$/)
|
|
|
+ nock('http://localhost').post('/', /^--form-data-boundary-[a-z0-9]+\r\nContent-Disposition: form-data; name="key1"\r\n\r\nnull\r\n--form-data-boundary-[a-z0-9]+\r\nContent-Disposition: form-data; name="key2"\r\n\r\ntrue\r\n--form-data-boundary-[a-z0-9]+\r\nContent-Disposition: form-data; name="key3"\r\n\r\n\d+\r\n--form-data-boundary-[a-z0-9]+\r\nContent-Disposition: form-data; name="key4"\r\n\r\n\[object Object\]\r\n--form-data-boundary-[a-z0-9]+\r\nContent-Disposition: form-data; name="key5"\r\n\r\n\(\) => \{\}\r\n--form-data-boundary-[a-z0-9]+\r\nContent-Disposition: form-data; name="key6"\r\n\r\nSymbol\(\)\r\n--form-data-boundary-[a-z0-9]+\r\nContent-Disposition: form-data; name="files\[\]"; filename="uppy-file-[^"]+"\r\nContent-Type: application\/octet-stream\r\n\r\nSome file content\r\n--form-data-boundary-[a-z0-9]+--\r\n\r\n$/)
|
|
|
.reply(200)
|
|
|
|
|
|
const metadata = {
|
|
@@ -257,7 +273,7 @@ describe('uploader with tus protocol', () => {
|
|
|
|
|
|
test('uploader respects maxFileSize with unknown size', async () => {
|
|
|
const fileContent = Buffer.alloc(10000)
|
|
|
- const stream = intoStream(fileContent)
|
|
|
+ const stream = Readable.from([fileContent])
|
|
|
const opts = {
|
|
|
companionOptions: { ...companionOptions, maxFileSize: 1000 },
|
|
|
endpoint: 'http://url.myendpoint.com/files',
|