11import { TextDecoder } from 'util'
22
3- import fetchMock from 'fetch-mock'
4- import { afterEach , expect , test } from 'vitest'
3+ import { afterEach , beforeEach , expect , test , vi } from 'vitest'
54
65import { RemoteFile } from '../src/'
76
8- fetchMock . config . sendAsJson = false
9-
107function toString ( a : Uint8Array < ArrayBuffer > ) {
118 return new TextDecoder ( 'utf8' ) . decode ( a )
129}
13- afterEach ( ( ) => fetchMock . restore ( ) )
10+
11+ // Create a Response object from a buffer or string
12+ function createResponse (
13+ body : Uint8Array < ArrayBuffer > | string ,
14+ status : number ,
15+ headers : Record < string , string > = { } ,
16+ ) {
17+ return {
18+ ok : status >= 200 && status < 300 ,
19+ status,
20+ headers : {
21+ get ( name : string ) {
22+ return headers [ name ] || null
23+ } ,
24+ } ,
25+ arrayBuffer : async ( ) => {
26+ if ( typeof body === 'string' ) {
27+ const encoder = new TextEncoder ( )
28+ return encoder . encode ( body ) . buffer
29+ }
30+ return body . buffer
31+ } ,
32+ text : async ( ) => {
33+ if ( typeof body === 'string' ) {
34+ return body
35+ }
36+ return toString ( body )
37+ } ,
38+ }
39+ }
40+
41+ // Mock implementation for fetch
42+ let mockFetch : ( input : RequestInfo , init ?: RequestInit ) => Promise < Response >
43+
44+ beforeEach ( ( ) => {
45+ // Reset the mock fetch implementation before each test
46+ mockFetch = vi . fn ( ) . mockImplementation ( async ( url : string ) => {
47+ throw new Error ( `Unhandled fetch request to ${ url } ` )
48+ } )
49+ } )
50+
51+ afterEach ( ( ) => {
52+ vi . resetAllMocks ( )
53+ } )
1454
1555test ( 'auth token' , async ( ) => {
16- fetchMock . mock ( 'http://fakehost/test.txt' , ( url : string , args : any ) => {
56+ mockFetch = vi . fn ( ) . mockImplementation ( async ( _url : string , args : any ) => {
1757 return args . headers . Authorization
18- ? {
19- status : 200 ,
20- body : 'hello world' ,
21- }
22- : {
23- status : 403 ,
24- }
58+ ? createResponse ( 'hello world' , 200 )
59+ : createResponse ( 'Unauthorized' , 403 )
2560 } )
61+
2662 const f = new RemoteFile ( 'http://fakehost/test.txt' , {
63+ fetch : mockFetch ,
2764 overrides : {
2865 headers : {
2966 Authorization : 'Basic YWxhZGRpbjpvcGVuc2VzYW1l' ,
@@ -33,20 +70,21 @@ test('auth token', async () => {
3370 const stat = await f . readFile ( 'utf8' )
3471 expect ( stat ) . toBe ( 'hello world' )
3572} )
73+
3674test ( 'auth token with range request' , async ( ) => {
37- fetchMock . mock ( 'http://fakehost/test.txt' , ( url : string , args : any ) => {
75+ mockFetch = vi . fn ( ) . mockImplementation ( async ( _url : string , args : any ) => {
3876 if ( args . headers . Authorization && args . headers . range ) {
39- return {
40- status : 206 ,
41- body : 'hello' ,
42- }
77+ return createResponse ( 'hello' , 206 )
4378 } else if ( ! args . headers . Authorization ) {
44- return { status : 403 }
79+ return createResponse ( 'Unauthorized' , 403 )
4580 } else if ( ! args . headers . Range ) {
46- return { status : 400 }
81+ return createResponse ( 'Bad Request' , 400 )
4782 }
83+ return createResponse ( 'Unknown error' , 500 )
4884 } )
85+
4986 const f = new RemoteFile ( 'http://fakehost/test.txt' , {
87+ fetch : mockFetch ,
5088 overrides : {
5189 headers : {
5290 Authorization : 'Basic YWxhZGRpbjpvcGVuc2VzYW1l' ,
0 commit comments