Skip to content

Commit 44cd700

Browse files
authored
🩹 Fix: behavior of DefaultCtx.Fresh when 'Last-Modified' and 'If-Modified-Since' are equal (#3150)
* fix(ctx): 'if-modified-since' and 'last-modified' are equal, `DefaultCtx.Fresh` now returns true * accurate benchmark measurements
1 parent 0c1f5ff commit 44cd700

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

ctx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ func (c *DefaultCtx) Fresh() bool {
624624
if err != nil {
625625
return false
626626
}
627-
return lastModifiedTime.Before(modifiedSinceTime)
627+
return lastModifiedTime.Compare(modifiedSinceTime) != 1
628628
}
629629
}
630630
}

ctx_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,10 @@ func Test_Ctx_Fresh(t *testing.T) {
13971397
require.False(t, c.Fresh())
13981398

13991399
c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:28:00 GMT")
1400+
require.True(t, c.Fresh())
1401+
1402+
c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:27:59 GMT")
1403+
c.Response().Header.Set(HeaderLastModified, "Wed, 21 Oct 2015 07:28:00 GMT")
14001404
require.False(t, c.Fresh())
14011405
}
14021406

@@ -1412,6 +1416,18 @@ func Benchmark_Ctx_Fresh_WithNoCache(b *testing.B) {
14121416
}
14131417
}
14141418

1419+
// go test -v -run=^$ -bench=Benchmark_Ctx_Fresh_LastModified -benchmem -count=4
1420+
func Benchmark_Ctx_Fresh_LastModified(b *testing.B) {
1421+
app := New()
1422+
c := app.AcquireCtx(&fasthttp.RequestCtx{})
1423+
1424+
c.Response().Header.Set(HeaderLastModified, "Wed, 21 Oct 2015 07:28:00 GMT")
1425+
c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:28:00 GMT")
1426+
for n := 0; n < b.N; n++ {
1427+
c.Fresh()
1428+
}
1429+
}
1430+
14151431
// go test -run Test_Ctx_Binders -v
14161432
func Test_Ctx_Binders(t *testing.T) {
14171433
t.Parallel()

0 commit comments

Comments
 (0)