-
Notifications
You must be signed in to change notification settings - Fork 0
/
binder_test.go
138 lines (109 loc) · 2.64 KB
/
binder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package emir
import (
"testing"
"github.com/valyala/fasthttp"
)
func Test_JSONBind(t *testing.T) {
e := New(Config{})
e.POST("/", func(c *Context) error {
v := struct {
Test string `json:"test"`
Test1 string `qs:"Test1"`
}{}
if err := c.Bind(&v); err != nil {
t.Fatal(err)
}
if v.Test != "test" || v.Test1 != "test" {
t.Fatalf("unexpected value. expected: %s, got: %s and %s", "test", v.Test, v.Test1)
}
return nil
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.SetRequestURI("/?Test1=test")
ctx.Request.Header.SetContentType(ContentTypeApplicationJSON)
ctx.Request.SetBodyString(`{"test": "test"}`)
ctx.Request.Header.SetMethod(MethodPost)
e.Handler()(ctx)
}
func Benchmark_JSONBind(b *testing.B) {
e := New(Config{})
e.POST("/bench", func(c *Context) error {
v := struct {
Test string `json:"Test"`
Test1 string `qs:"Test1"`
}{}
if err := c.Bind(&v); err != nil {
b.Fatal(err)
}
return nil
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.SetRequestURI("/bench?Test1=test")
ctx.Request.Header.SetContentType(ContentTypeApplicationJSON)
ctx.Request.SetBodyString(`{"test": "test"}`)
ctx.Request.Header.SetMethod(MethodPost)
handler := e.Handler()
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
for p.Next() {
handler(ctx)
}
})
}
func Benchmark_XMLBind(b *testing.B) {
e := New(Config{})
e.POST("/", func(c *Context) error {
v := struct {
Test1 string `qs:"Test1"`
Test string `xml:"test"`
}{}
if err := c.Bind(&v); err != nil {
b.Fatal(err)
}
return nil
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.SetRequestURI("/?Test1=test")
ctx.Request.Header.SetContentType(ContentTypeApplicationXML)
ctx.Request.SetBodyString(`
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>test</test>
</root>
`)
ctx.Request.Header.SetMethod(MethodPost)
handler := e.Handler()
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
for p.Next() {
handler(ctx)
}
})
}
func Test_XMLBind(t *testing.T) {
e := New(Config{})
e.POST("/", func(c *Context) error {
v := struct {
Test1 string `qs:"Test1"`
Test string `xml:"test"`
}{}
if err := c.Bind(&v); err != nil {
t.Fatal(err)
}
if v.Test != "test" || v.Test1 != "test" {
t.Fatalf("unexpected value. expected: %s, got: %s and %s", "test", v.Test, v.Test1)
}
return nil
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.SetRequestURI("/?Test1=test")
ctx.Request.Header.SetContentType(ContentTypeApplicationXML)
ctx.Request.SetBodyString(`
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>test</test>
</root>
`)
ctx.Request.Header.SetMethod(MethodPost)
e.Handler()(ctx)
}