1- import { SELF , env , ctx } from "cloudflare:test"
1+ import { env } from "cloudflare:test"
22import { test , expect } from "vitest"
33
44import { params , genRandStr } from "../src/common.js"
5-
6- import * as crypto from "crypto"
7-
8- // for auto reload
9- import worker from "../src/index.js"
10-
11- const BASE_URL = env [ "BASE_URL" ]
12- const RAND_NAME_REGEX = / ^ [ A B C D E F G H J K M N P Q R S T W X Y Z a b c d e f h i j k m n p r s t w x y z 2 3 4 5 6 7 8 ] + $ /
13-
14- async function workerFetch ( req , options ) {
15- // we are not using SELF.fetch since it sometimes do not print worker log to console
16- // return await SELF.fetch(req, options)
17- return await worker . fetch ( new Request ( req , options ) , env , ctx )
18- }
19-
20- async function upload ( kv ) {
21- const uploadResponse = await workerFetch ( new Request ( BASE_URL , {
22- method : "POST" ,
23- body : createFormData ( kv ) ,
24- } ) )
25- expect ( uploadResponse . status ) . toStrictEqual ( 200 )
26- expect ( uploadResponse . headers . get ( "Content-Type" ) ) . toStrictEqual ( "application/json;charset=UTF-8" )
27- return JSON . parse ( await uploadResponse . text ( ) )
28- }
29-
30- function createFormData ( kv ) {
31- const fd = new FormData ( )
32- Object . entries ( kv ) . forEach ( ( [ k , v ] ) => {
33- if ( ( v === Object ( v ) ) && "filename" in v && "value" in v ) {
34- fd . set ( k , new File ( [ v . value ] , v . filename ) )
35- } else {
36- fd . set ( k , v )
37- }
38- } )
39- return fd
40- }
41-
42- function randomBlob ( len ) {
43- const buf = Buffer . alloc ( len )
44- return new Blob ( [ crypto . randomFillSync ( buf , 0 , len ) ] )
45- }
46-
47- async function areBlobsEqual ( blob1 , blob2 ) {
48- return Buffer . from ( await blob1 . arrayBuffer ( ) ) . compare (
49- Buffer . from ( await blob2 . arrayBuffer ( ) ) ,
50- ) === 0
51- }
5+ import {
6+ randomBlob , areBlobsEqual , createFormData , workerFetch , upload ,
7+ BASE_URL , RAND_NAME_REGEX , staticPages ,
8+ } from "./testUtils.js"
529
5310test ( "static page" , async ( ) => {
54- const staticPages = [ "" , "index.html" , "index" , "tos" , "tos.html" , "api" , "api.html" ]
5511 for ( const page of staticPages ) {
5612 expect ( ( await workerFetch ( `${ BASE_URL } /${ page } ` ) ) . status ) . toStrictEqual ( 200 )
5713 }
@@ -133,7 +89,7 @@ test("basic", async () => {
13389
13490 // check delete
13591 const deleteResponse = await workerFetch ( new Request ( admin , { method : "DELETE" } ) )
136- expect ( putResponse . status ) . toStrictEqual ( 200 )
92+ expect ( deleteResponse . status ) . toStrictEqual ( 200 )
13793
13894 // check visit modified
13995 const revisitDeletedResponse = await workerFetch ( url )
@@ -181,7 +137,7 @@ test("expire", async () => {
181137 await testExpireParse ( "1M" , 18144000 )
182138 await testExpireParse ( "100 m" , 6000 )
183139
184- const testFailParse = async ( expire , expireSecs ) => {
140+ const testFailParse = async ( expire ) => {
185141 const uploadResponse = await workerFetch ( new Request ( BASE_URL , {
186142 method : "POST" ,
187143 body : createFormData ( { "c" : blob1 , "e" : expire } ) ,
@@ -375,10 +331,18 @@ test("cache control", async () => {
375331 const uploadResp = await upload ( ( { "c" : randomBlob ( 1024 ) } ) )
376332 const url = uploadResp [ "url" ]
377333 const resp = await workerFetch ( url )
378- expect ( resp . headers . get ( "Cache-Control" ) ) . toStrictEqual ( `public, max-age=${ env . CACHE_PASTE_AGE } ` )
334+ if ( "CACHE_PASTE_AGE" in env ) {
335+ expect ( resp . headers . get ( "Cache-Control" ) ) . toStrictEqual ( `public, max-age=${ env . CACHE_PASTE_AGE } ` )
336+ } else {
337+ expect ( resp . headers . get ( "Cache-Control" ) ) . toBeUndefined ( )
338+ }
379339
380340 const indexResp = await workerFetch ( BASE_URL )
381- expect ( indexResp . headers . get ( "Cache-Control" ) ) . toStrictEqual ( `public, max-age=${ env . CACHE_STATIC_PAGE_AGE } ` )
341+ if ( "CACHE_STATIC_PAGE_AGE" in env ) {
342+ expect ( indexResp . headers . get ( "Cache-Control" ) ) . toStrictEqual ( `public, max-age=${ env . CACHE_STATIC_PAGE_AGE } ` )
343+ } else {
344+ expect ( indexResp . headers . get ( "Cache-Control" ) ) . toBeUndefined ( )
345+ }
382346
383347 const staleResp = await workerFetch ( url , {
384348 headers : {
@@ -388,6 +352,4 @@ test("cache control", async () => {
388352 expect ( staleResp . status ) . toStrictEqual ( 304 )
389353} )
390354
391- // TODO: add tests for basic auth
392-
393355// TODO: add tests for CORS
0 commit comments