11import { assert , assertEquals , assertExists } from "jsr:@std/assert" ;
22import {
3- compareETags ,
43 create304Response ,
54 generateETag ,
6- getDefaultConditionalConfig ,
7- parseETag ,
8- parseHttpDate ,
9- parseIfNoneMatch ,
105 validateConditionalRequest ,
116} from "../../src/conditional.ts" ;
127import { createCacheHandler } from "../../src/handlers.ts" ;
@@ -24,73 +19,6 @@ Deno.test("Conditional Requests - ETag generation", async () => {
2419 assert ( etag . endsWith ( '"' ) ) ;
2520} ) ;
2621
27- Deno . test ( "Conditional Requests - ETag parsing" , ( ) => {
28- // Strong ETag
29- const strongETag = parseETag ( '"abc123"' ) ;
30- assertEquals ( strongETag . value , "abc123" ) ;
31- assertEquals ( strongETag . weak , false ) ;
32-
33- // Weak ETag
34- const weakETag = parseETag ( 'W/"abc123"' ) ;
35- assertEquals ( weakETag . value , "abc123" ) ;
36- assert ( weakETag . weak ) ;
37-
38- // Empty ETag
39- const emptyETag = parseETag ( "" ) ;
40- assertEquals ( emptyETag . value , "" ) ;
41- assertEquals ( emptyETag . weak , false ) ;
42- } ) ;
43-
44- Deno . test ( "Conditional Requests - ETag comparison" , ( ) => {
45- const etag1 = '"abc123"' ;
46- const etag2 = '"abc123"' ;
47- const etag3 = '"def456"' ;
48- const weakETag = 'W/"abc123"' ;
49-
50- // Strong comparison - exact match
51- assertEquals ( compareETags ( etag1 , etag2 ) , true ) ;
52- assertEquals ( compareETags ( etag1 , etag3 ) , false ) ;
53-
54- // Strong comparison - weak ETag should not match
55- assertEquals ( compareETags ( etag1 , weakETag , false ) , false ) ;
56-
57- // Weak comparison - should match even with weak ETag
58- assertEquals ( compareETags ( etag1 , weakETag , true ) , true ) ;
59- } ) ;
60-
61- Deno . test ( "Conditional Requests - If-None-Match parsing" , ( ) => {
62- // Single ETag
63- const single = parseIfNoneMatch ( '"abc123"' ) ;
64- assert ( Array . isArray ( single ) ) ;
65- assertEquals ( ( single as string [ ] ) . length , 1 ) ;
66- assertEquals ( ( single as string [ ] ) [ 0 ] , '"abc123"' ) ;
67-
68- // Multiple ETags
69- const multiple = parseIfNoneMatch ( '"abc123", "def456", W/"ghi789"' ) ;
70- assert ( Array . isArray ( multiple ) ) ;
71- assertEquals ( ( multiple as string [ ] ) . length , 3 ) ;
72-
73- // Wildcard
74- const wildcard = parseIfNoneMatch ( "*" ) ;
75- assertEquals ( wildcard , "*" ) ;
76-
77- // Empty
78- const empty = parseIfNoneMatch ( "" ) ;
79- assert ( Array . isArray ( empty ) ) ;
80- assertEquals ( ( empty as string [ ] ) . length , 0 ) ;
81- } ) ;
82-
83- Deno . test ( "Conditional Requests - HTTP date parsing" , ( ) => {
84- const validDate = parseHttpDate ( "Wed, 21 Oct 2015 07:28:00 GMT" ) ;
85- assertExists ( validDate ) ;
86- assert ( validDate instanceof Date ) ;
87-
88- const invalidDate = parseHttpDate ( "invalid date" ) ;
89- assertEquals ( invalidDate , null ) ;
90-
91- const emptyDate = parseHttpDate ( "" ) ;
92- assertEquals ( emptyDate , null ) ;
93- } ) ;
9422
9523Deno . test ( "Conditional Requests - validateConditionalRequest with ETag" , ( ) => {
9624 const request = new Request ( "https://example.com/test" , {
@@ -176,9 +104,8 @@ Deno.test("Conditional Requests - 304 response creation", () => {
176104 assertEquals ( response304 . headers . get ( "x-custom" ) , null ) ;
177105} ) ;
178106
179- // New unified handler integration tests
180107
181- Deno . test ( "Conditional Requests - unified handler If-None-Match " , async ( ) => {
108+ Deno . test ( "Conditional Requests - unified handler returns 304 for matching ETag " , async ( ) => {
182109 await caches . delete ( "conditional-test" ) ;
183110 const cacheName = "conditional-test" ;
184111 const cache = await caches . open ( cacheName ) ;
@@ -202,12 +129,13 @@ Deno.test("Conditional Requests - unified handler If-None-Match", async () => {
202129 { handler : ( ) => Promise . resolve ( new Response ( "fresh" ) ) } ,
203130 ) ;
204131 assertExists ( result ) ;
205- assertEquals ( [ 200 , 304 ] . includes ( result . status ) , true ) ;
206- await result . clone ( ) . text ( ) ;
132+ assertEquals ( result . status , 304 ) ;
133+ assertEquals ( result . body , null ) ;
134+ assertEquals ( result . headers . get ( "etag" ) , '"test-etag-123"' ) ;
207135 await caches . delete ( "conditional-test" ) ;
208136} ) ;
209137
210- Deno . test ( "Conditional Requests - unified handler If -Modified-Since " , async ( ) => {
138+ Deno . test ( "Conditional Requests - unified handler returns 304 for matching Last -Modified" , async ( ) => {
211139 await caches . delete ( "conditional-test-date" ) ;
212140 const cacheName = "conditional-test-date" ;
213141 const cache = await caches . open ( cacheName ) ;
@@ -232,8 +160,10 @@ Deno.test("Conditional Requests - unified handler If-Modified-Since", async () =
232160 { handler : ( ) => Promise . resolve ( new Response ( "fresh" ) ) } ,
233161 ) ;
234162 assertExists ( result ) ;
235- assertEquals ( [ 200 , 304 ] . includes ( result . status ) , true ) ;
236- await result . clone ( ) . text ( ) ;
163+ assertEquals ( result . status , 304 ) ;
164+ assertEquals ( result . body , null ) ;
165+ assertEquals ( result . headers . get ( "last-modified" ) , lastModified ) ;
166+ assertEquals ( result . headers . get ( "content-type" ) , "application/json" ) ;
237167 await caches . delete ( cacheName ) ;
238168} ) ;
239169
@@ -264,13 +194,6 @@ Deno.test("Conditional Requests - unified handler ETag generation", async () =>
264194 await caches . delete ( cacheName ) ;
265195} ) ;
266196
267- Deno . test ( "Conditional Requests - Default configuration" , ( ) => {
268- const config = getDefaultConditionalConfig ( ) ;
269-
270- assert ( config . etag ) ;
271- assert ( config . lastModified ) ;
272- assert ( config . weakValidation ) ;
273- } ) ;
274197
275198Deno . test ( "Conditional Requests - disabled returns full response" , async ( ) => {
276199 await caches . delete ( "conditional-disabled-test" ) ;
0 commit comments