-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathendpoint_test.go
More file actions
128 lines (99 loc) · 2.78 KB
/
endpoint_test.go
File metadata and controls
128 lines (99 loc) · 2.78 KB
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
package draft_test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/gothing/draft"
"github.com/gothing/draft/types"
"github.com/stretchr/testify/assert"
)
type UserEndpoint struct {
draft.Endpoint
}
type UserEndpointBody struct {
ID types.UserID
Flags UserFlags
}
type UserFlags struct {
IsAdmin bool
Deleted bool
}
func (ue *UserEndpoint) InitEndpointScheme(s *draft.Scheme) {
s.URL("/api/v1/user")
s.Case(draft.Status.OK, "Wow!", func() {
s.Body(UserEndpointBody{
ID: 20976,
})
})
}
type UserEndpoint2 struct {
draft.Endpoint
}
func (ue *UserEndpoint2) InitEndpointScheme(s *draft.Scheme) {
s.URL("/api/v1/user2")
}
type UserEndpoint3 struct {
draft.Endpoint
}
func (ue *UserEndpoint3) InitEndpointScheme(s *draft.Scheme) {
s.URL("/api/v1/user3")
}
func TestEndpoint(t *testing.T) {
ue := &UserEndpoint{}
api := draft.Create(draft.Config{DevMode: true})
group := draft.Compose("test", ue)
api.Add(group, nil)
r := httptest.NewRequest("GET", "http://gothing/api/v1/user", nil)
w := httptest.NewRecorder()
assert.Equal(t, []string{"/api/v1/user"}, api.URLs(), "url")
assert.Equal(t, ue.GetScheme().GetCaseByStatus(draft.Status.OK).Name, "Wow!")
api.ServeHTTP(w, r)
resp := w.Result()
body, err := ioutil.ReadAll(resp.Body)
assert.Equal(t, nil, err, "error")
json := `{"status":"ok","body":{"id":20976,"flags":{"is_admin":false,"deleted":false}}}`
assert.Equal(t, json, string(body), "Mock")
}
func TestEndpointWithHandler(t *testing.T) {
ue := &UserEndpoint2{
Endpoint: draft.Endpoint{
Handler: func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK:" + r.URL.Path))
},
},
}
api := draft.Create(draft.Config{DevMode: true})
group := draft.Compose("test", ue)
api.Add(group, nil)
r := httptest.NewRequest("GET", "http://gothing/api/v1/user2", nil)
w := httptest.NewRecorder()
api.ServeHTTP(w, r)
resp := w.Result()
body, err := ioutil.ReadAll(resp.Body)
assert.Equal(t, nil, err, "error")
result := `OK:/api/v1/user2`
assert.Equal(t, result, string(body), "result")
}
type testGroupHandler struct {
}
func (tgh *testGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("WOW:" + r.URL.Path))
}
func (tgh *testGroupHandler) Routes() []string {
return []string{"/api/v1/user3"}
}
// func TestEndpointWithGroupHandler(t *testing.T) {
// ue := &UserEndpoint3{}
// api := draft.Create()
// group := draft.Compose("test", ue)
// api.Add(group, &testGroupHandler{})
// r := httptest.NewRequest("GET", "http://gothing/api/v1/user3", nil)
// w := httptest.NewRecorder()
// api.ServeHTTP(w, r)
// resp := w.Result()
// body, err := ioutil.ReadAll(resp.Body)
// assert.Equal(t, nil, err, "error")
// result := `OK:/api/v1/user3`
// assert.Equal(t, result, string(body), "result")
// }