Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Dec 20, 2024
1 parent a56943d commit b5cc3a6
Show file tree
Hide file tree
Showing 15 changed files with 991 additions and 20 deletions.
28 changes: 28 additions & 0 deletions binder/binder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package binder

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_GetAndPutToThePool(t *testing.T) {
t.Parallel()

// Panics in case we get from another pool
require.Panics(t, func() {
_ = GetFromThePool[*HeaderBinding](&CookieBinderPool)
})

// We get from the pool
binder := GetFromThePool[*HeaderBinding](&HeaderBinderPool)
PutToThePool(&HeaderBinderPool, binder)

_ = GetFromThePool[*RespHeaderBinding](&RespHeaderBinderPool)
_ = GetFromThePool[*QueryBinding](&QueryBinderPool)
_ = GetFromThePool[*FormBinding](&FormBinderPool)
_ = GetFromThePool[*URIBinding](&URIBinderPool)
_ = GetFromThePool[*XMLBinding](&XMLBinderPool)
_ = GetFromThePool[*JSONBinding](&JSONBinderPool)
_ = GetFromThePool[*CBORBinding](&CBORBinderPool)
}
90 changes: 90 additions & 0 deletions binder/cbor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package binder

import (
"testing"

"github.com/fxamacker/cbor/v2"
"github.com/stretchr/testify/require"
)

func Test_CBORBinder_Bind(t *testing.T) {
t.Parallel()

b := &CBORBinding{
CBORDecoder: cbor.Unmarshal,
}
require.Equal(t, "cbor", b.Name())

type Post struct {
Title string `cbor:"title"`
}

type User struct {
Name string `cbor:"name"`
Names []string `cbor:"names"`
Age int `cbor:"age"`

Posts []Post `cbor:"posts"`
}
var user User

wantedUser := User{
Name: "john",
Names: []string{
"john",
"doe",
},
Age: 42,
Posts: []Post{
{Title: "post1"},
{Title: "post2"},
{Title: "post3"},
},
}

body, err := cbor.Marshal(wantedUser)
require.NoError(t, err)

err = b.Bind(body, &user)

require.NoError(t, err)
require.Equal(t, "john", user.Name)
require.Equal(t, 42, user.Age)
require.Len(t, user.Posts, 3)
require.Equal(t, "post1", user.Posts[0].Title)
require.Equal(t, "post2", user.Posts[1].Title)
require.Equal(t, "post3", user.Posts[2].Title)
require.Contains(t, user.Names, "john")
require.Contains(t, user.Names, "doe")
}

func Benchmark_CBORBinder_Bind(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

binder := &CBORBinding{
CBORDecoder: cbor.Unmarshal,
}

type User struct {
Name string `cbor:"name"`
Age int `cbor:"age"`
}

var user User
wantedUser := User{
Name: "john",
Age: 42,
}

body, err := cbor.Marshal(wantedUser)
require.NoError(b, err)

for i := 0; i < b.N; i++ {
err = binder.Bind(body, &user)
}

require.NoError(b, err)
require.Equal(b, "john", user.Name)
require.Equal(b, 42, user.Age)
}
4 changes: 2 additions & 2 deletions binder/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func (*CookieBinding) Name() string {
}

// Bind parses the request cookie and returns the result.
func (b *CookieBinding) Bind(reqCtx *fasthttp.RequestCtx, out any) error {
func (b *CookieBinding) Bind(req *fasthttp.Request, out any) error {
data := make(map[string][]string)
var err error

reqCtx.Request.Header.VisitAllCookie(func(key, val []byte) {
req.Header.VisitAllCookie(func(key, val []byte) {
if err != nil {
return
}
Expand Down
84 changes: 84 additions & 0 deletions binder/cookie_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package binder

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/valyala/fasthttp"
)

func Test_CookieBinder_Bind(t *testing.T) {
t.Parallel()

b := &CookieBinding{
EnableSplitting: true,
}
require.Equal(t, "cookie", b.Name())

type Post struct {
Title string `form:"title"`
}

type User struct {
Name string `form:"name"`
Names []string `form:"names"`
Age int `form:"age"`

Posts []Post `form:"posts"`
}
var user User

req := fasthttp.AcquireRequest()

req.Header.SetCookie("name", "john")
req.Header.SetCookie("names", "john,doe")
req.Header.SetCookie("age", "42")

t.Cleanup(func() {
fasthttp.ReleaseRequest(req)
})

err := b.Bind(req, &user)

require.NoError(t, err)
require.Equal(t, "john", user.Name)
require.Equal(t, 42, user.Age)
require.Contains(t, user.Names, "john")
require.Contains(t, user.Names, "doe")
}

func Benchmark_CookieBinder_Bind(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

binder := &CookieBinding{
EnableSplitting: true,
}

type User struct {
Name string `query:"name"`
Age int `query:"age"`

Posts []string `query:"posts"`
}
var user User

req := fasthttp.AcquireRequest()

req.Header.SetCookie("name", "john")
req.Header.SetCookie("age", "42")
req.Header.SetCookie("posts", "post1,post2,post3")

b.ResetTimer()

for i := 0; i < b.N; i++ {
_ = binder.Bind(req, &user)
}

require.Equal(b, "john", user.Name)
require.Equal(b, 42, user.Age)
require.Len(b, user.Posts, 3)
require.Contains(b, user.Posts, "post1")
require.Contains(b, user.Posts, "post2")
require.Contains(b, user.Posts, "post3")
}
8 changes: 4 additions & 4 deletions binder/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func (*FormBinding) Name() string {
}

// Bind parses the request body and returns the result.
func (b *FormBinding) Bind(reqCtx *fasthttp.RequestCtx, out any) error {
func (b *FormBinding) Bind(req *fasthttp.Request, out any) error {
data := make(map[string][]string)
var err error

reqCtx.PostArgs().VisitAll(func(key, val []byte) {
req.PostArgs().VisitAll(func(key, val []byte) {
if err != nil {
return
}
Expand Down Expand Up @@ -53,8 +53,8 @@ func (b *FormBinding) Bind(reqCtx *fasthttp.RequestCtx, out any) error {
}

// BindMultipart parses the request body and returns the result.
func (b *FormBinding) BindMultipart(reqCtx *fasthttp.RequestCtx, out any) error {
data, err := reqCtx.MultipartForm()
func (b *FormBinding) BindMultipart(req *fasthttp.Request, out any) error {
data, err := req.MultipartForm()
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit b5cc3a6

Please sign in to comment.