11import { expect , describe , it , beforeAll , afterAll } from "bun:test" ;
2+ import type { ExecutionContext } from "@cloudflare/workers-types" ;
3+ import { handleRequest as handleFilesRequest } from "unpkg-files" ;
24
35import { packageInfo , packageTarballs } from "../test/fixtures.ts" ;
4-
6+ import type { Env } from "./env.ts" ;
57import { handleRequest } from "./request-handler.tsx" ;
68
9+ const env : Env = {
10+ APP_ORIGIN : "https://app.unpkg.com" ,
11+ ASSETS_ORIGIN : "https://unpkg.com" ,
12+ DEV : false ,
13+ FILES_ORIGIN : "https://files.unpkg.com" ,
14+ MODE : "test" ,
15+ ORIGIN : "https://unpkg.com" ,
16+ } ;
17+
18+ const context : ExecutionContext = {
19+ waitUntil ( ) { } ,
20+ } as unknown as ExecutionContext ;
21+
722function dispatchFetch ( input : RequestInfo | URL , init ?: RequestInit ) : Promise < Response > {
823 let request = input instanceof Request ? input : new Request ( input , init ) ;
9- return handleRequest ( request ) ;
24+ return handleRequest ( request , env , context ) ;
25+ }
26+
27+ function fileResponse ( path : string ) : Response {
28+ return new Response ( Bun . file ( path ) ) ;
1029}
1130
1231describe ( "handleRequest" , ( ) => {
32+ let globalCaches : CacheStorage | undefined ;
1333 let globalFetch : typeof fetch | undefined ;
1434
15- function fileResponse ( path : string ) : Response {
16- return new Response ( Bun . file ( path ) ) ;
17- }
18-
1935 beforeAll ( ( ) => {
36+ globalCaches = globalThis . caches ;
2037 globalFetch = globalThis . fetch ;
2138
22- // Does not implement Bun's non-spec fetch.preconnect API - https://bun.sh/docs/api/fetch#preconnect-to-a-host
23- // @ts -expect-error
24- globalThis . fetch = async ( input : RequestInfo | URL ) => {
25- let url = input instanceof Request ? input . url : input ;
39+ globalThis . caches = {
40+ async open ( ) {
41+ return {
42+ async match ( ) {
43+ return null ;
44+ } ,
45+ async put ( ) { } ,
46+ } ;
47+ } ,
48+ } as unknown as CacheStorage ;
49+
50+ globalThis . fetch = ( async ( input : RequestInfo | URL , init ?: RequestInit ) => {
51+ let request = input instanceof Request ? input : new Request ( input , init ) ;
52+ let url = new URL ( request . url ) ;
53+
54+ if ( url . origin === env . FILES_ORIGIN ) {
55+ // Run the request through the file server. This allows us to write integration tests
56+ // that run without booting the file server.
57+ return handleFilesRequest ( request ) ;
58+ }
2659
27- switch ( url . toString ( ) ) {
60+ switch ( url . href ) {
2861 case "https://registry.npmjs.org/lodash" :
2962 return fileResponse ( packageInfo . lodash ) ;
3063 case "https://registry.npmjs.org/preact" :
@@ -44,13 +77,12 @@ describe("handleRequest", () => {
4477 default :
4578 throw new Error ( `Unexpected URL: ${ url } ` ) ;
4679 }
47- } ;
80+ } ) as unknown as typeof fetch ;
4881 } ) ;
4982
5083 afterAll ( ( ) => {
51- if ( globalFetch ) {
52- globalThis . fetch = globalFetch ;
53- }
84+ if ( globalCaches ) globalThis . caches = globalCaches ;
85+ if ( globalFetch ) globalThis . fetch = globalFetch ;
5486 } ) ;
5587
5688 describe ( "file requests" , ( ) => {
0 commit comments