@@ -51,7 +51,7 @@ func TestCheckNotModifiedIfNoneMatch(t *testing.T) {
5151 ctx .Request .SetRequestURI ("/app.js" )
5252 ctx .Request .Header .Set ("If-None-Match" , `W/"abcdef1234567890"` )
5353
54- if ! headers .CheckNotModified (& ctx , f ) {
54+ if ! headers .CheckNotModified (& ctx , f , true ) {
5555 t .Fatal ("CheckNotModified returned false, want true" )
5656 }
5757 if ctx .Response .StatusCode () != fasthttp .StatusNotModified {
@@ -66,7 +66,7 @@ func TestCheckNotModifiedIfModifiedSince(t *testing.T) {
6666 ctx .Request .SetRequestURI ("/page.html" )
6767 ctx .Request .Header .Set ("If-Modified-Since" , time .Date (2024 , 1 , 16 , 0 , 0 , 0 , 0 , time .UTC ).Format (cache .HTTPTimeFormat ))
6868
69- if ! headers .CheckNotModified (& ctx , f ) {
69+ if ! headers .CheckNotModified (& ctx , f , true ) {
7070 t .Fatal ("CheckNotModified returned false, want true" )
7171 }
7272 if ctx .Response .StatusCode () != fasthttp .StatusNotModified {
@@ -81,14 +81,14 @@ func TestCheckNotModifiedReturnsFalseOnMismatch(t *testing.T) {
8181 ctx .Request .SetRequestURI ("/data.json" )
8282 ctx .Request .Header .Set ("If-None-Match" , `W/"differentetag0000"` )
8383
84- if headers .CheckNotModified (& ctx , f ) {
84+ if headers .CheckNotModified (& ctx , f , true ) {
8585 t .Fatal ("CheckNotModified returned true, want false" )
8686 }
8787}
8888
8989func TestSetCacheHeadersHTML (t * testing.T ) {
9090 f := makeCachedFile ([]byte ("<html>" ), "text/html" )
91- cfg := & config.HeadersConfig {HTMLMaxAge : 0 , StaticMaxAge : 3600 }
91+ cfg := & config.HeadersConfig {HTMLMaxAge : 0 , StaticMaxAge : 3600 , EnableETags : true }
9292 var ctx fasthttp.RequestCtx
9393
9494 headers .SetCacheHeaders (& ctx , "/index.html" , f , cfg )
@@ -106,7 +106,7 @@ func TestSetCacheHeadersHTML(t *testing.T) {
106106
107107func TestSetCacheHeadersStaticImmutable (t * testing.T ) {
108108 f := makeCachedFile ([]byte ("console.log(1)" ), "application/javascript" )
109- cfg := & config.HeadersConfig {StaticMaxAge : 31536000 , ImmutablePattern : "*.js" }
109+ cfg := & config.HeadersConfig {StaticMaxAge : 31536000 , ImmutablePattern : "*.js" , EnableETags : true }
110110 var ctx fasthttp.RequestCtx
111111
112112 headers .SetCacheHeaders (& ctx , "/assets/app.abc123.js" , f , cfg )
@@ -117,6 +117,37 @@ func TestSetCacheHeadersStaticImmutable(t *testing.T) {
117117 }
118118}
119119
120+ func TestCheckNotModifiedETagsDisabled (t * testing.T ) {
121+ f := makeCachedFile ([]byte ("console.log(1)" ), "application/javascript" )
122+ var ctx fasthttp.RequestCtx
123+ ctx .Request .Header .SetMethod ("GET" )
124+ ctx .Request .SetRequestURI ("/app.js" )
125+ ctx .Request .Header .Set ("If-None-Match" , `W/"abcdef1234567890"` )
126+
127+ // When ETags are disabled, CheckNotModified should return false even with matching ETag
128+ if headers .CheckNotModified (& ctx , f , false ) {
129+ t .Fatal ("CheckNotModified returned true, want false when ETags disabled" )
130+ }
131+ if ctx .Response .StatusCode () == fasthttp .StatusNotModified {
132+ t .Fatalf ("status = %d, want not 304 when ETags disabled" , ctx .Response .StatusCode ())
133+ }
134+ }
135+
136+ func TestSetCacheHeadersETagsDisabled (t * testing.T ) {
137+ f := makeCachedFile ([]byte ("<html>" ), "text/html" )
138+ cfg := & config.HeadersConfig {HTMLMaxAge : 0 , StaticMaxAge : 3600 , EnableETags : false }
139+ var ctx fasthttp.RequestCtx
140+
141+ headers .SetCacheHeaders (& ctx , "/index.html" , f , cfg )
142+
143+ if etag := string (ctx .Response .Header .Peek ("ETag" )); etag != "" {
144+ t .Fatalf ("ETag = %q, want empty when disabled" , etag )
145+ }
146+ if cc := string (ctx .Response .Header .Peek ("Cache-Control" )); cc != "no-cache" {
147+ t .Fatalf ("Cache-Control = %q, want no-cache" , cc )
148+ }
149+ }
150+
120151func TestETagMatches (t * testing.T ) {
121152 if ! headers .ETagMatches ("*" , `W/"abc"` ) {
122153 t .Fatal ("ETagMatches wildcard = false, want true" )
0 commit comments