-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathparams_test.go
61 lines (58 loc) · 1.12 KB
/
params_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
package params
import (
"fmt"
"net/http"
"net/url"
"reflect"
"strings"
"testing"
)
func lt100(v interface{}) error {
i, ok := v.(int)
if !ok {
return fmt.Errorf("not an int: %v", v)
}
if i >= 100 {
return fmt.Errorf("%d >= 100", i)
}
return nil
}
func testChecks(t *testing.T) {
type unpacked struct {
post int `http:p,check:"lt100"`
}
tests := []struct {
req *http.Request
want unpacked
errSubstr string
}{
{
&http.Request{Form: url.Values{"p": []string{"120"}}},
unpacked{},
">= 100",
},
{
&http.Request{Form: url.Values{"p": []string{"80"}}},
unpacked{80},
"",
},
}
checks := map[string]Check{
"lt100": lt100,
}
for _, test := range tests {
var got unpacked
err := Unpack(test.req, &got, checks)
if test.errSubstr != "" && !strings.Contains(err.Error(), test.errSubstr) {
t.Errorf("Unpack(%v), error %q doesn't contain %q",
test.req, err, test.errSubstr)
continue
}
if err != nil {
t.Errorf("Unpack(%v): %s", test.req, err)
}
if reflect.DeepEqual(test.want, got) {
t.Errorf("Unpack(%v), got %v, want %v", test.req, got, test.want)
}
}
}