-
Notifications
You must be signed in to change notification settings - Fork 19
/
complex_test.go
95 lines (80 loc) · 1.76 KB
/
complex_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
package gongular
import (
"fmt"
"net/http"
"reflect"
"testing"
"net/url"
"github.com/stretchr/testify/assert"
)
type allFields struct {
U1 uint8
U2 uint16
U3 uint32
U4 uint64
I1 int8
I2 int16
I3 int32
I4 int64
F1 float32
F2 float64
B1 bool
B2 bool
Str1 string
Str2 string
Str3 string
}
var set = allFields{
U1: 4,
U2: 6312,
U3: 34314,
U4: 1023123132,
I1: 123,
I2: 1325,
I3: 12359,
I4: 495435034,
F1: 34013.43103024,
F2: -1320213995243.44353243103024,
B1: true,
B2: false,
Str1: "selambro",
Str2: "wassup",
Str3: "bye",
}
type complexHandler1 struct {
Param allFields
Body allFields
Query allFields
}
func (s *complexHandler1) Handle(c *Context) error {
b1 := reflect.DeepEqual(s.Param, set)
b2 := reflect.DeepEqual(s.Body, set)
b3 := reflect.DeepEqual(s.Query, set)
c.SetBody(fmt.Sprintf("%t:%t:%t", b1, b2, b3))
return nil
}
func TestComplex1Handler(t *testing.T) {
e := newEngineTest()
e.GetRouter().POST("/complex/:U1/:U2/:U3/:U4/:I1/:I2/:I3/:I4/:F1/:F2/:B1/:B2/:Str1/:Str2/:Str3",
&complexHandler1{})
// Construct the path params
path := fmt.Sprintf(
"/complex/%d/%d/%d/%d/%d/%d/%d/%d/%f/%f/%t/%t/%s/%s/%s",
set.U1, set.U2, set.U3, set.U4, set.I1, set.I2, set.I3, set.I4, set.F1, set.F2,
set.B1, set.B2, set.Str1, set.Str2, set.Str3,
)
u, err := url.Parse(path)
assert.Nil(t, err)
// Construct the query params
q := u.Query()
tip := reflect.TypeOf(set)
val := reflect.ValueOf(set)
for i := 0; i < tip.NumField(); i++ {
q.Set(tip.Field(i).Name, fmt.Sprint(val.Field(i).Interface()))
}
u.RawQuery = q.Encode()
// Add body and post
resp, content := post(t, e, u.String(), set)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `"true:true:true"`, content)
}