1
1
import { SELF , env } from "cloudflare:test" ;
2
- import { it , expect , vi } from "vitest" ;
2
+ import { it , expect , vi , afterEach } from "vitest" ;
3
3
import type Cloudflare from "cloudflare" ;
4
4
5
5
const verify = vi . fn <
@@ -17,6 +17,10 @@ vi.mock("cloudflare", () => ({
17
17
} ) ) ,
18
18
} ) ) ;
19
19
20
+ afterEach ( ( ) => {
21
+ vi . clearAllMocks ( ) ;
22
+ } ) ;
23
+
20
24
it ( "adds cache tags to the purge queue" , async ( ) => {
21
25
verify . mockResolvedValueOnce ( { status : "active" , id : "foo" } ) ;
22
26
@@ -85,3 +89,64 @@ it("adds cache tags to the purge queue with no zone", async () => {
85
89
} ,
86
90
] ) ;
87
91
} ) ;
92
+
93
+ it ( "returns a 401 when the wrong API Token is provided" , async ( ) => {
94
+ const response = await SELF . fetch (
95
+ "https://cache-tag.example.workers.dev/purge" ,
96
+ {
97
+ method : "POST" ,
98
+ body : JSON . stringify ( {
99
+ tags : [ "test" ] ,
100
+ } ) ,
101
+ headers : {
102
+ Authorization : `Bearer wrong` ,
103
+ "CF-Worker" : "example.com" ,
104
+ } ,
105
+ } ,
106
+ ) ;
107
+
108
+ expect ( response . status ) . toBe ( 401 ) ;
109
+
110
+ expect ( verify ) . not . toBeCalled ( ) ;
111
+ } ) ;
112
+
113
+ it ( "returns a 401 when the wrong API Token is expired" , async ( ) => {
114
+ verify . mockResolvedValueOnce ( { status : "expired" , id : "foo" } ) ;
115
+
116
+ const response = await SELF . fetch (
117
+ "https://cache-tag.example.workers.dev/purge" ,
118
+ {
119
+ method : "POST" ,
120
+ body : JSON . stringify ( {
121
+ tags : [ "test" ] ,
122
+ } ) ,
123
+ headers : {
124
+ Authorization : `Bearer ${ env . API_TOKEN } ` ,
125
+ } ,
126
+ } ,
127
+ ) ;
128
+
129
+ expect ( response . status ) . toBe ( 401 ) ;
130
+
131
+ expect ( verify ) . toBeCalled ( ) ;
132
+ } ) ;
133
+
134
+ it ( "returns a 400 when request is malformed" , async ( ) => {
135
+ const response = await SELF . fetch (
136
+ "https://cache-tag.example.workers.dev/purge" ,
137
+ {
138
+ method : "POST" ,
139
+ body : JSON . stringify ( {
140
+ url : "https://example.com" ,
141
+ } ) ,
142
+ headers : {
143
+ Authorization : `Bearer ${ env . API_TOKEN } ` ,
144
+ "CF-Worker" : "example.com" ,
145
+ } ,
146
+ } ,
147
+ ) ;
148
+
149
+ expect ( response . status ) . toBe ( 400 ) ;
150
+
151
+ expect ( verify ) . not . toBeCalled ( ) ;
152
+ } ) ;
0 commit comments