-
Notifications
You must be signed in to change notification settings - Fork 2
/
fresh_test.go
169 lines (153 loc) · 3.55 KB
/
fresh_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
package fresh
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type testRoute struct {
path string
urlParams []string
body dataResponse
code int
}
type dataResponse struct {
Field int `json:"field"`
Fields []string `json:"fields"`
}
var req = []testRoute{
{
path: "/first/",
urlParams: []string{},
body: dataResponse{Field: 1, Fields: []string{"white", "black"}},
code: http.StatusOK,
},
{
path: "/first/:/",
urlParams: []string{"1"},
body: dataResponse{Field: 1, Fields: []string{"brown", "charcoal"}},
code: http.StatusCreated,
},
{
path: "/first/:/second",
urlParams: []string{"1"},
body: dataResponse{Field: 1, Fields: []string{"yellow"}},
code: http.StatusOK,
},
{
path: "/first/:/second/:/",
urlParams: []string{"1", "2"},
body: dataResponse{Field: 1, Fields: []string{"blue"}},
code: http.StatusForbidden,
},
{
path: "/first/:/second/:/third",
urlParams: []string{"1", "2"},
body: dataResponse{Field: 1, Fields: []string{"red"}},
code: http.StatusOK,
},
{
path: "/first/:/second/:/third/:/",
urlParams: []string{"1", "2", "3"},
body: dataResponse{Field: 1, Fields: []string{"blue", "violet"}},
code: http.StatusOK,
},
}
func setup() fresh {
f := fresh{
config: &Config{},
server: new(http.Server),
}
f.config.fresh = &f
f.config.init(&f)
f.router = &router{&f, &route{}, make(map[string]string)}
return f
}
func ctrl(r testRoute) HandlerFunc {
return func(context Context) error {
return context.Response().JSON(r.code, r.body)
}
}
func requests(method string, f *fresh) {
for _, elm := range req {
// set url params
for _, v := range elm.urlParams {
elm.path = strings.Replace(elm.path, ":/", ":"+v+"/", 1)
}
switch method {
case "GET":
f.GET(elm.path, ctrl(elm))
case "POST":
f.POST(elm.path, ctrl(elm))
case "PUT":
f.PUT(elm.path, ctrl(elm))
case "TRACE":
f.TRACE(elm.path, ctrl(elm))
case "PATCH":
f.PATCH(elm.path, ctrl(elm))
case "DELETE":
f.DELETE(elm.path, ctrl(elm))
case "OPTIONS":
f.OPTIONS(elm.path, ctrl(elm))
}
}
}
func records(method string, body io.Reader, f fresh, t *testing.T) {
for _, elm := range req {
rec := httptest.NewRecorder()
req, err := http.NewRequest(method, elm.path, body)
if err != nil {
t.Fatal("Creating", method, elm, "request failed!")
}
f.router.ServeHTTP(rec, req)
// status code
if rec.Code != elm.code {
t.Fatal("Server error: Returned ", rec.Code, " instead of ", elm.code)
}
// body
expected, _ := json.Marshal(elm.body)
result, _ := ioutil.ReadAll(rec.Body)
if string(result) != string(expected) {
t.Fatal("Expected", string(expected), "instead", string(result))
}
}
}
func TestFresh_Run(t *testing.T) {}
func TestFresh_GET(t *testing.T) {
f := setup()
requests("GET", &f)
records("GET", nil, f, t)
}
func TestFresh_PUT(t *testing.T) {
f := setup()
requests("PUT", &f)
records("PUT", nil, f, t)
}
func TestFresh_POST(t *testing.T) {
f := setup()
requests("POST", &f)
records("POST", nil, f, t)
}
func TestFresh_TRACE(t *testing.T) {
f := setup()
requests("TRACE", &f)
records("TRACE", nil, f, t)
}
func TestFresh_PATCH(t *testing.T) {
f := setup()
requests("PATCH", &f)
records("PATCH", nil, f, t)
}
func TestFresh_DELETE(t *testing.T) {
f := setup()
requests("DELETE", &f)
records("DELETE", nil, f, t)
}
func TestFresh_OPTIONS(t *testing.T) {
f := setup()
requests("OPTIONS", &f)
records("OPTIONS", nil, f, t)
}