-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
991 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.