Skip to content

Commit

Permalink
Add unit-test and benchmark for BodyRaw() with Immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Jul 25, 2024
1 parent 57d31cc commit 70b808f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ func Test_Ctx_BodyRaw(t *testing.T) {
require.Equal(t, []byte("john=doe"), c.BodyRaw())
}

// go test -run Test_Ctx_BodyRaw_Immutable
func Test_Ctx_BodyRaw_Immutable(t *testing.T) {
t.Parallel()
app := New(Config{Immutable: true})
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed

c.Request().SetBodyRaw([]byte("john=doe"))
require.Equal(t, []byte("john=doe"), c.BodyRaw())
}

// go test -v -run=^$ -bench=Benchmark_Ctx_Body -benchmem -count=4
func Benchmark_Ctx_Body(b *testing.B) {
const input = "john=doe"
Expand Down Expand Up @@ -400,6 +410,23 @@ func Benchmark_Ctx_BodyRaw(b *testing.B) {
require.Equal(b, []byte(input), c.BodyRaw())
}

// go test -v -run=^$ -bench=Benchmark_Ctx_BodyRaw_Immutable -benchmem -count=4
func Benchmark_Ctx_BodyRaw_Immutable(b *testing.B) {
const input = "john=doe"

app := New(Config{Immutable: true})
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed

c.Request().SetBodyRaw([]byte(input))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.BodyRaw()
}

require.Equal(b, []byte(input), c.BodyRaw())
}

// go test -run Test_Ctx_Body_Immutable
func Test_Ctx_Body_Immutable(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 70b808f

Please sign in to comment.