1- import { createExecutionContext , env , SELF } from "cloudflare:test" ;
1+ import { env , exports } from "cloudflare:workers" ;
2+ import {
3+ createExecutionContext ,
4+ waitOnExecutionContext ,
5+ } from "cloudflare:test" ;
26import assert from "node:assert" ;
37import { vi , describe , test , expect , beforeEach , afterEach } from "vitest" ;
48import {
@@ -27,7 +31,7 @@ async function searchZLSRelease(
2731
2832async function sendPublishForm ( body : PublishRequest ) : Promise < Response > {
2933 assert ( typeof env . API_TOKEN === "string" && env . API_TOKEN ) ;
30- return await SELF . fetch (
34+ return await exports . default . fetch (
3135 new Request ( "https://example.com/v1/zls/publish" , {
3236 body : JSON . stringify ( body ) ,
3337 method : "POST" ,
@@ -121,8 +125,14 @@ function getSampleArtifacts(version: string): Record<string, ArtifactMetadata> {
121125}
122126
123127describe ( "/v1/zls/publish" , ( ) => {
128+ afterEach ( async ( ) => {
129+ await env . ZIGTOOLS_DB . exec ( "DELETE FROM ZLSReleases" ) ;
130+ } ) ;
131+
124132 test ( "expect POST method" , async ( ) => {
125- const response = await SELF . fetch ( "https://example.com/v1/zls/publish" ) ;
133+ const response = await exports . default . fetch (
134+ "https://example.com/v1/zls/publish" ,
135+ ) ;
126136 expect ( await response . text ( ) ) . toBe ( "method must be 'POST'" ) ;
127137 expect ( response . status ) . toBe ( 405 ) ;
128138 } ) ;
@@ -146,103 +156,127 @@ describe("/v1/zls/publish", () => {
146156
147157 describe ( "check authorization" , ( ) => {
148158 test ( "missing Authorization header" , async ( ) => {
149- const response = await SELF . fetch ( "https://example.com/v1/zls/publish" , {
150- method : "POST" ,
151- } ) ;
159+ const response = await exports . default . fetch (
160+ "https://example.com/v1/zls/publish" ,
161+ {
162+ method : "POST" ,
163+ } ,
164+ ) ;
152165
153166 expect ( await response . text ( ) ) . toBe ( "Authorization failed" ) ;
154167 expect ( response . status ) . toBe ( 401 ) ;
155168 } ) ;
156169
157170 test ( "invalid Authorization header" , async ( ) => {
158- const response = await SELF . fetch ( "https://example.com/v1/zls/publish" , {
159- body : null ,
160- method : "POST" ,
161- headers : {
162- Authorization : "invalid" ,
171+ const response = await exports . default . fetch (
172+ "https://example.com/v1/zls/publish" ,
173+ {
174+ body : null ,
175+ method : "POST" ,
176+ headers : {
177+ Authorization : "invalid" ,
178+ } ,
163179 } ,
164- } ) ;
180+ ) ;
165181 expect ( await response . text ( ) ) . toContain (
166182 "Unexpected Authorization header" ,
167183 ) ;
168184 expect ( response . status ) . toBe ( 400 ) ;
169185 } ) ;
170186
171187 test ( "non Basic Authorization header" , async ( ) => {
172- const response = await SELF . fetch ( "https://example.com/v1/zls/publish" , {
173- body : null ,
174- method : "POST" ,
175- headers : {
176- Authorization : "Bearer foo" ,
188+ const response = await exports . default . fetch (
189+ "https://example.com/v1/zls/publish" ,
190+ {
191+ body : null ,
192+ method : "POST" ,
193+ headers : {
194+ Authorization : "Bearer foo" ,
195+ } ,
177196 } ,
178- } ) ;
197+ ) ;
179198 expect ( await response . text ( ) ) . toBe (
180199 "Expected 'Basic' authentication scheme!" ,
181200 ) ;
182201 expect ( response . status ) . toBe ( 400 ) ;
183202 } ) ;
184203
185204 test ( "invalid Basic Authorization header" , async ( ) => {
186- const response = await SELF . fetch ( "https://example.com/v1/zls/publish" , {
187- body : null ,
188- method : "POST" ,
189- headers : {
190- Authorization : "Basic :" ,
205+ const response = await exports . default . fetch (
206+ "https://example.com/v1/zls/publish" ,
207+ {
208+ body : null ,
209+ method : "POST" ,
210+ headers : {
211+ Authorization : "Basic :" ,
212+ } ,
191213 } ,
192- } ) ;
214+ ) ;
193215 expect ( await response . text ( ) ) . toContain (
194216 "Unexpected Authorization header" ,
195217 ) ;
196218 expect ( response . status ) . toBe ( 401 ) ;
197219 } ) ;
198220
199221 test ( "wrong username" , async ( ) => {
200- const response = await SELF . fetch ( "https://example.com/v1/zls/publish" , {
201- body : null ,
202- method : "POST" ,
203- headers : {
204- Authorization : `Basic ${ Buffer . from ( `wrong:${ env . API_TOKEN } ` ) . toString ( "base64" ) } ` ,
222+ const response = await exports . default . fetch (
223+ "https://example.com/v1/zls/publish" ,
224+ {
225+ body : null ,
226+ method : "POST" ,
227+ headers : {
228+ Authorization : `Basic ${ Buffer . from ( `wrong:${ env . API_TOKEN } ` ) . toString ( "base64" ) } ` ,
229+ } ,
205230 } ,
206- } ) ;
231+ ) ;
207232 expect ( await response . text ( ) ) . toBe ( "Authorization failed" ) ;
208233 expect ( response . status ) . toBe ( 401 ) ;
209234 } ) ;
210235
211236 test ( "wrong password" , async ( ) => {
212- const response = await SELF . fetch ( "https://example.com/v1/zls/publish" , {
213- body : null ,
214- method : "POST" ,
215- headers : {
216- Authorization : `Basic ${ Buffer . from ( "admin:wrong" ) . toString ( "base64" ) } ` ,
237+ const response = await exports . default . fetch (
238+ "https://example.com/v1/zls/publish" ,
239+ {
240+ body : null ,
241+ method : "POST" ,
242+ headers : {
243+ Authorization : `Basic ${ Buffer . from ( "admin:wrong" ) . toString ( "base64" ) } ` ,
244+ } ,
217245 } ,
218- } ) ;
246+ ) ;
219247 expect ( await response . text ( ) ) . toBe ( "Authorization failed" ) ;
220248 expect ( response . status ) . toBe ( 401 ) ;
221249 } ) ;
222250 } ) ;
223251
224252 describe ( "validate request body" , ( ) => {
225253 test ( "body is not a json" , async ( ) => {
226- const response = await SELF . fetch ( "https://example.com/v1/zls/publish" , {
227- body : null ,
228- method : "POST" ,
229- headers : {
230- Authorization : `Basic ${ Buffer . from ( `admin:${ env . API_TOKEN } ` ) . toString ( "base64" ) } ` ,
254+ const response = await exports . default . fetch (
255+ "https://example.com/v1/zls/publish" ,
256+ {
257+ body : null ,
258+ method : "POST" ,
259+ headers : {
260+ Authorization : `Basic ${ Buffer . from ( `admin:${ env . API_TOKEN } ` ) . toString ( "base64" ) } ` ,
261+ } ,
231262 } ,
232- } ) ;
263+ ) ;
233264 expect ( await response . text ( ) ) . toBe ( `Unexpected end of JSON input` ) ;
234265 expect ( response . status ) . toBe ( 400 ) ;
235266 } ) ;
236267
237268 test ( "body is not a JSON object" , async ( ) => {
238- const response = await SELF . fetch ( "https://example.com/v1/zls/publish" , {
239- body : JSON . stringify ( 5 ) ,
240- method : "POST" ,
241- headers : {
242- "content-type" : "application/json;charset=UTF-8" ,
243- Authorization : `Basic ${ Buffer . from ( `admin:${ env . API_TOKEN } ` ) . toString ( "base64" ) } ` ,
269+ const response = await exports . default . fetch (
270+ "https://example.com/v1/zls/publish" ,
271+ {
272+ body : JSON . stringify ( 5 ) ,
273+ method : "POST" ,
274+ headers : {
275+ "content-type" : "application/json;charset=UTF-8" ,
276+ Authorization : `Basic ${ Buffer . from ( `admin:${ env . API_TOKEN } ` ) . toString ( "base64" ) } ` ,
277+ } ,
244278 } ,
245- } ) ;
279+ ) ;
246280 expect ( await response . text ( ) ) . toBe ( `request body is not a JSON object!` ) ;
247281 expect ( response . status ) . toBe ( 400 ) ;
248282 } ) ;
0 commit comments