From 44cd700ad578f9c60c9c5d2b4752fe22ef32a4e1 Mon Sep 17 00:00:00 2001 From: miyamo2 <79917704+miyamo2@users.noreply.github.com> Date: Thu, 26 Sep 2024 15:42:35 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20Fix:=20behavior=20of=20`DefaultC?= =?UTF-8?q?tx.Fresh`=20when=20'Last-Modified'=20and=20'If-Modified-Since'?= =?UTF-8?q?=20are=20equal=20(#3150)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(ctx): 'if-modified-since' and 'last-modified' are equal, `DefaultCtx.Fresh` now returns true * accurate benchmark measurements --- ctx.go | 2 +- ctx_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ctx.go b/ctx.go index 2a4341b2..607a678f 100644 --- a/ctx.go +++ b/ctx.go @@ -624,7 +624,7 @@ func (c *DefaultCtx) Fresh() bool { if err != nil { return false } - return lastModifiedTime.Before(modifiedSinceTime) + return lastModifiedTime.Compare(modifiedSinceTime) != 1 } } } diff --git a/ctx_test.go b/ctx_test.go index 2060e2a9..a94e4cb4 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -1397,6 +1397,10 @@ func Test_Ctx_Fresh(t *testing.T) { require.False(t, c.Fresh()) c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:28:00 GMT") + require.True(t, c.Fresh()) + + c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:27:59 GMT") + c.Response().Header.Set(HeaderLastModified, "Wed, 21 Oct 2015 07:28:00 GMT") require.False(t, c.Fresh()) } @@ -1412,6 +1416,18 @@ func Benchmark_Ctx_Fresh_WithNoCache(b *testing.B) { } } +// go test -v -run=^$ -bench=Benchmark_Ctx_Fresh_LastModified -benchmem -count=4 +func Benchmark_Ctx_Fresh_LastModified(b *testing.B) { + app := New() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + + c.Response().Header.Set(HeaderLastModified, "Wed, 21 Oct 2015 07:28:00 GMT") + c.Request().Header.Set(HeaderIfModifiedSince, "Wed, 21 Oct 2015 07:28:00 GMT") + for n := 0; n < b.N; n++ { + c.Fresh() + } +} + // go test -run Test_Ctx_Binders -v func Test_Ctx_Binders(t *testing.T) { t.Parallel()