forked from whentp/go-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_test.go
161 lines (135 loc) · 4.4 KB
/
rest_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
package rest
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
type FakeNode struct {
formatter pathFormatter
lastInstance reflect.Value
lastCtx *context
}
func (n *FakeNode) init(formatter pathFormatter, instance reflect.Type, name string, tag reflect.StructTag) ([]handler, []pathFormatter, error) {
n.formatter = formatter
return []handler{&FakeHandler{name, n}}, []pathFormatter{formatter}, nil
}
type FakeHandler struct {
name_ string
node *FakeNode
}
func (h *FakeHandler) name() string {
return h.name_
}
func (h *FakeHandler) handle(instance reflect.Value, ctx *context) {
h.node.lastInstance = instance
h.node.lastCtx = ctx
}
type TestDefault struct {
Service `prefix:"/prefix" mime:"mime" charset:"charset"`
NoMethod FakeNode `path:"/default" method:"METHOD" other:"other"`
}
type TestFunc struct {
NoMethod FakeNode `path:"/func" method:"METHOD" func:"FuncHandler"`
Service `prefix:"/prefix" mime:"mime" charset:"charset"`
}
type TestNoMethod struct {
Service `prefix:"/prefix" mime:"mime" charset:"charset"`
NoMethod FakeNode `path:"/no/method"`
}
type TestNoPath struct {
Service `prefix:"/prefix" mime:"mime" charset:"charset"`
NoMethod FakeNode `method:"METHOD"`
}
type TestSamePath struct {
Service `prefix:"/prefix" mime:"mime" charset:"charset"`
NoMethod1 FakeNode `method:"METHOD"`
NoMethod2 FakeNode `method:"METHOD"`
}
type TestNoService struct{}
func TestNewRest(t *testing.T) {
type Test struct {
instance interface{}
ok bool
serviceIndex int
prefix string
mime string
charset string
formatter pathFormatter
tag reflect.StructTag
}
var tests = []Test{
{new(TestDefault), true, 0, "/prefix", "mime", "charset", "/prefix/default", `path:"/default" method:"METHOD" other:"other"`},
{new(TestFunc), true, 1, "/prefix", "mime", "charset", "/prefix/func", `path:"/func" method:"METHOD" func:"FuncHandler"`},
{new(TestNoPath), true, 0, "/prefix", "mime", "charset", "/prefix", `method:"METHOD"`},
{new(TestNoService), false, 0, "", "", "", "", ""},
{new(TestNoMethod), false, 0, "", "", "", "", ""},
{new(TestSamePath), false, 0, "", "", "", "", ""},
}
for i, test := range tests {
r, err := New(test.instance)
equal(t, err == nil, test.ok, "test %d error: %s", i, err)
if err != nil || !test.ok {
continue
}
equal(t, r.Prefix(), test.prefix, "test %d", i)
equal(t, r.defaultMime, test.mime, "test %d", i)
equal(t, r.defaultCharset, test.charset, "test %d", i)
handler, ok := r.router.Routes[0].Dest.(*FakeHandler)
if !ok {
fmt.Errorf("handler not *FakeHandler")
continue
}
equal(t, handler.node.formatter, test.formatter, "test %d", i)
}
}
type TestPost struct {
Service `prefix:"/prefix"`
Node FakeNode `method:"POST" path:"/node"`
NodeId FakeNode `method:"GET" path:"/node/:id" func:"HandleNode"`
}
func (r TestPost) HandleNode() {}
func TestRestServeHTTP(t *testing.T) {
type Test struct {
method string
url string
code int
name string
node *FakeNode
formatter pathFormatter
vars map[string]string
}
instance := new(TestPost)
rest, err := New(instance)
if err != nil {
t.Fatalf("new rest service failed: %s", err)
}
var tests = []Test{
{"GET", "http://domain/prefix/node/123", http.StatusOK, "NodeId", &instance.NodeId, "/prefix/node/:id", map[string]string{"id": "123"}},
{"GET", "http://domain/prefix/node/", http.StatusNotFound, "", nil, "", nil},
{"POST", "http://domain/prefix/node", http.StatusOK, "Node", &instance.Node, "/prefix/node", nil},
{"POST", "http://domain/prefix/no/exist", http.StatusNotFound, "", nil, "", nil},
{"GET", "http://domain/prefix/node", http.StatusNotFound, "", nil, "", nil},
}
for i, test := range tests {
buf := bytes.NewBuffer(nil)
req, err := http.NewRequest(test.method, test.url, buf)
if err != nil {
t.Fatalf("test %d create request failed", i, err)
}
w := httptest.NewRecorder()
w.Code = http.StatusOK
rest.ServeHTTP(w, req)
equal(t, w.Code, test.code, "test %d code: %s", i, w.Code)
if w.Code != http.StatusOK {
continue
}
equal(t, test.node.formatter, test.formatter, "test %d", i)
equal(t, equalMap(test.node.lastCtx.vars, test.vars), true, "test %d", i)
equal(t, test.node.lastCtx.name, test.name, "test %d", i)
service := test.node.lastInstance.Field(0).Interface().(Service)
equal(t, equalMap(service.Vars(), test.vars), true, "test %d", i)
}
}