-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathziptie_test.go
148 lines (119 loc) · 3.29 KB
/
ziptie_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
package ziptie
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
)
type PostsCtrl struct {
DB bool
Namespace string
Index interface{} `path:"" method:"GET"`
Show interface{} `path:"/:id" method:"GET"`
}
func (ctrl *PostsCtrl) IndexFunc(ctx echo.Context) error {
return ctx.String(http.StatusOK, "Index")
}
func (ctrl *PostsCtrl) ShowFunc(ctx echo.Context) error {
res := fmt.Sprintf("Showing post %s", ctx.Param("id"))
foo := ctx.QueryParam("foo")
if foo != "" {
return ctx.String(http.StatusOK, "Query Param found!")
}
return ctx.String(http.StatusOK, res)
}
func TestFastenWithOneMethod(t *testing.T) {
e := echo.New()
Fasten(&PostsCtrl{Namespace: "/posts"}, e, "")
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/posts", nil)
e.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Error("Status Code is not 200")
}
}
func TestFastenWithASecondMethod(t *testing.T) {
e := echo.New()
Fasten(&PostsCtrl{Namespace: "/posts"}, e, "")
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/posts/1", nil)
e.ServeHTTP(rec, req)
byt := rec.Body.Bytes()
if rec.Code != 200 {
t.Error("Status Code is not 200")
}
if string(byt) != "Showing post 1" {
t.Error("Unexpected response body")
}
}
type MixedCtrl struct {
Namespace string
Config map[string]interface{}
Index interface{} `path:"" method:"GET"`
}
func (mc *MixedCtrl) IndexFunc(ctx echo.Context) error {
return ctx.String(http.StatusOK, "Index")
}
func TestHandlingOfNonMethodFieldsInStruct(t *testing.T) {
e := echo.New()
Fasten(&MixedCtrl{Namespace: "/mixed"}, e, "")
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/mixed", nil)
e.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Error("Status Code is not 200")
}
}
type MissingHandlerFuncCtrl struct {
Index interface{} `path:"" method:"GET"`
}
func TestMissingHandlerFunc(t *testing.T) {
t.Skip("TODO")
}
type NamespacedCtrl struct {
Namespace string
Foo interface{} `path:"/" method:"GET"`
}
func (nc *NamespacedCtrl) FooFunc(ctx echo.Context) error {
return ctx.String(http.StatusOK, "foo")
}
func TestRootCtrl(t *testing.T) {
e := echo.New()
ctrl := &NamespacedCtrl{Namespace: ""}
Fasten(ctrl, e, "")
rec := httptest.NewRecorder()
req := httptest.NewRequest(echo.GET, "/", nil)
e.ServeHTTP(rec, req)
byt := rec.Body.Bytes()
if rec.Code != 200 {
t.Error(fmt.Sprintf("Expecting %d, got %d", 200, rec.Code))
}
if string(byt) != "foo" {
t.Error("Unexpected response body")
}
}
func TestQueryParameterHandling(t *testing.T) {
expected := "Query Param found!"
e := echo.New()
Fasten(&PostsCtrl{Namespace: "/posts"}, e, "")
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/posts/123?foo=bar", nil)
e.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Error("Status Code is not 200")
}
if rec.Body.String() != expected {
t.Error(fmt.Sprintf("Expected body to be %s, got %s", expected, rec.Body.String()))
}
}
func Test_supportsANamespaceAtFasten(t *testing.T) {
e := echo.New()
Fasten(&PostsCtrl{Namespace: "/posts"}, e, "/api/v1")
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/posts/123", nil)
e.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Error("Status Code is not 200")
}
}