1- import { once } from 'node:events'
21import fs from 'node:fs'
3- import { createServer } from 'node:http'
42import { Readable } from 'node:stream'
53import nock from 'nock'
6- import { afterAll , describe , expect , test , vi } from 'vitest'
4+ import { afterAll , afterEach , describe , expect , test , vi } from 'vitest'
75import Emitter from '../src/server/emitter/index.js'
86import Uploader , { ValidationError } from '../src/server/Uploader.js'
97import standalone from '../src/standalone/index.js'
@@ -12,8 +10,10 @@ import * as socketClient from './mocksocket.js'
1210vi . mock ( 'tus-js-client' )
1311vi . mock ( 'express-prom-bundle' )
1412
15- afterAll ( ( ) => {
13+ afterEach ( ( ) => {
1614 nock . cleanAll ( )
15+ } )
16+ afterAll ( ( ) => {
1717 nock . restore ( )
1818} )
1919
@@ -24,7 +24,7 @@ const { companionOptions } = standalone()
2424
2525const mockReq = { }
2626
27- describe ( 'uploader with tus protocol ' , ( ) => {
27+ describe ( 'uploader' , ( ) => {
2828 test ( 'uploader respects uploadUrls' , async ( ) => {
2929 const opts = {
3030 endpoint : 'http://localhost/files' ,
@@ -207,12 +207,14 @@ describe('uploader with tus protocol', () => {
207207 useFormData,
208208 includeSize = true ,
209209 address = 'localhost' ,
210+ // @ts -ignore
211+ extraCompanionOpts,
210212 } = { } ) {
211213 const fileContent = Buffer . from ( 'Some file content' )
212214 const stream = Readable . from ( [ fileContent ] )
213215
214216 const opts = {
215- companionOptions,
217+ companionOptions : { ... companionOptions , ... extraCompanionOpts } ,
216218 endpoint : `http://${ address } ` ,
217219 protocol : 'multipart' ,
218220 size : includeSize ? fileContent . length : undefined ,
@@ -227,32 +229,33 @@ describe('uploader with tus protocol', () => {
227229 }
228230
229231 test ( 'upload functions with xhr protocol' , async ( ) => {
230- let alreadyCalled = false
231- // We are creating our own test server for this test
232- // instead of using nock because of a bug when passing a Node.js stream to got.
233- // Ref: https://github.com/nock/nock/issues/2595
234- const server = createServer ( ( req , res ) => {
235- if ( alreadyCalled ) throw new Error ( 'already called' )
236- alreadyCalled = true
237- if ( req . url === '/' && req . method === 'POST' ) {
238- res . writeHead ( 200 )
239- res . end ( 'OK' )
240- }
241- } ) . listen ( )
242- try {
243- await once ( server , 'listening' )
244-
245- const ret = await runMultipartTest ( {
246- // @ts -ignore
247- address : `localhost:${ server . address ( ) . port } ` ,
248- } )
249- expect ( ret ) . toMatchObject ( {
250- url : null ,
251- extraData : { response : expect . anything ( ) , bytesUploaded : 17 } ,
252- } )
253- } finally {
254- server . close ( )
255- }
232+ nock ( 'http://localhost' ) . post ( '/' ) . reply ( 200 , 'OK' )
233+ const ret = await runMultipartTest ( )
234+ expect ( ret ) . toMatchObject ( {
235+ url : null ,
236+ extraData : { response : expect . anything ( ) , bytesUploaded : 17 } ,
237+ } )
238+ } )
239+
240+ test ( 'header companion option gets passed along to destination endpoint' , async ( ) => {
241+ nock ( 'http://localhost' )
242+ . post ( '/' )
243+ . matchHeader ( 'header-a' , '1' )
244+ . matchHeader ( 'header-b' , '2' )
245+ . reply ( 200 , ( ) => '' )
246+
247+ const ret = await runMultipartTest ( {
248+ // @ts -ignore
249+ extraCompanionOpts : {
250+ uploadHeaders : { 'header-a' : '1' , 'header-b' : '2' } ,
251+ } ,
252+ } )
253+ expect ( ret ) . toMatchObject ( {
254+ url : null ,
255+ extraData : { response : expect . anything ( ) , bytesUploaded : 17 } ,
256+ } )
257+
258+ expect ( ret . extraData . response ?. headers ?. [ 'header-a' ] ) . toBeUndefined ( ) // headers sent to destination, not received back
256259 } )
257260
258261 const formDataNoMetaMatch =
0 commit comments