-
Notifications
You must be signed in to change notification settings - Fork 1
/
extractor_test.go
185 lines (148 loc) · 4.07 KB
/
extractor_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package valueextractor
import (
"net/http"
"reflect"
"strconv"
"testing"
)
type Bench1 struct {
Name string `query:"name"`
Age uint64 `query:"age"`
}
func TestOptionalKeys(t *testing.T) {
req, _ := http.NewRequest("GET", "http://localhost:8080?name=John", nil)
var name string
var age uint64
ex := Using(QueryExtractor{Query: req.URL.Query()}, WithOptionalKeys("age"))
ex.With("name", AsString(&name))
ex.With("age", AsUint64(&age))
err := ex.Errors()
switch {
case err != nil:
t.Fatal(err)
case ex.optionalKeys == nil:
t.Fatal("Optional keys not set")
}
}
func TestResultGeneric(t *testing.T) {
req, _ := http.NewRequest("GET", "http://localhost:8080?name=John&age=30", nil)
ex := Using(QueryExtractor{Query: req.URL.Query()})
name := Result(ex, "name", AsString)
age := Result(ex, "age", AsUint64)
err := ex.Errors()
switch {
case err != nil:
t.Fatal(err)
case name != "John":
t.Fatal("Name not parsed correctly")
case age != 30:
t.Fatal("Age not parsed correctly")
}
}
func BenchmarkParamsParser(b *testing.B) {
// construct a request with sample query data
req, _ := http.NewRequest("GET", "http://localhost:8080?name=John&age=30", nil)
for i := 0; i < b.N; i++ {
res := Bench1{}
ex := Using(QueryExtractor{Query: req.URL.Query()})
ex.With("name", AsString(&res.Name))
ex.With("age", AsUint64(&res.Age))
err := ex.Errors()
switch {
case err != nil:
b.Fatal(err)
case res.Name != "John":
b.Fatal("Name not parsed correctly")
case res.Age != 30:
b.Fatal("Age not parsed correctly")
}
}
}
func BenchmarkTypeReturn(b *testing.B) {
req, _ := http.NewRequest("GET", "http://localhost:8080?name=John&age=30", nil)
for i := 0; i < b.N; i++ {
ex := Using(QueryExtractor{Query: req.URL.Query()})
if *ReturnString(ex, "name") != "John" {
b.Fatal("Name not parsed correctly")
}
if *ReturnUint64(ex, "age") != 30 {
b.Fatal("Age not parsed correctly")
}
if err := ex.Errors(); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParamsParserResultsValue(b *testing.B) {
// construct a request with sample query data
req, _ := http.NewRequest("GET", "http://localhost:8080?name=John&age=30", nil)
for i := 0; i < b.N; i++ {
ex := Using(QueryExtractor{Query: req.URL.Query()})
if Result(ex, "name", AsString) != "John" {
b.Fatal("Name not parsed correctly")
}
if Result(ex, "age", AsUint64) != 30 {
b.Fatal("Age not parsed correctly")
}
if err := ex.Errors(); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParamsParserResultsPtr(b *testing.B) {
// construct a request with sample query data
req, _ := http.NewRequest("GET", "http://localhost:8080?name=John&age=30", nil)
for i := 0; i < b.N; i++ {
ex := Using(QueryExtractor{Query: req.URL.Query()})
if *ResultPtr(ex, "name", AsString) != "John" {
b.Fatal("Name not parsed correctly")
}
if *ResultPtr(ex, "age", AsUint64) != 30 {
b.Fatal("Age not parsed correctly")
}
if err := ex.Errors(); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParamsParserNoStruct(b *testing.B) {
req, _ := http.NewRequest("GET", "http://localhost:8080?name=John&age=30", nil)
for i := 0; i < b.N; i++ {
name := ""
age := uint64(0)
ex := Using(QueryExtractor{Query: req.URL.Query()})
ex.With("name", AsString(&name))
ex.With("age", AsUint64(&age))
err := ex.Errors()
switch {
case err != nil:
b.Fatal(err)
case name != "John":
b.Fatal("Name not parsed correctly")
case age != 30:
b.Fatal("Age not parsed correctly")
}
}
}
func BenchmarkWithReflection(b *testing.B) {
// construct a request with sample query data
req, _ := http.NewRequest("GET", "http://localhost:8080?name=John&age=30", nil)
for i := 0; i < b.N; i++ {
bench1 := Bench1{}
v := reflect.ValueOf(&bench1).Elem()
t := v.Type()
query := req.URL.Query()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
tag := t.Field(i).Tag.Get("query")
value := query.Get(tag)
switch field.Kind() {
case reflect.String:
field.SetString(value)
case reflect.Int:
intVal, _ := strconv.Atoi(value)
field.SetUint(uint64(intVal))
}
}
}
}