-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
166 lines (150 loc) · 4.83 KB
/
main_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
package echowbt
import (
"fmt"
"net/http"
"reflect"
"strings"
"testing"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
)
type User struct {
ID int `json:"id"`
Firstname string `json:"firstname" form:"firstname"`
Lastname string `json:"lastname" form:"lastname"`
Age int `json:"age" form:"age"`
}
// Handlers
func GenericHandler() echo.HandlerFunc {
return func(c echo.Context) error {
status, u := http.StatusOK, User{}
log.Info("HEADERS: ", c.Request().Header)
switch c.Request().Method {
case "POST":
log.Info("POST /")
status = http.StatusCreated
contentType := c.Request().Header.Get("Content-Type")
if strings.HasPrefix(contentType, "application/x-www-form-urlencoded") {
c.Bind(&u)
fname := c.FormValue("firstname")
return c.String(status, fmt.Sprintf("Hello %s", fname))
}
if strings.HasPrefix(contentType, "multipart/form") {
fname := c.FormValue("firstname")
form, err := c.MultipartForm()
if err != nil {
log.Error(err)
return err
}
filename := form.File["bio"][0].Filename
return c.String(status, fmt.Sprintf("Hello %s ! Your file %s is up.", fname, filename))
}
case "PUT":
log.Info("PUT /:id")
if err := c.Bind(&u); err != nil {
log.Error(err)
return err
}
log.Info("USER: ", u)
status = http.StatusNoContent
case "PATCH":
log.Info("PATCH /:id")
if err := c.Bind(&u); err != nil {
log.Error(err)
return err
}
log.Info("USER: ", u)
status = http.StatusNoContent
case "DELETE":
log.Info("DELETE /:id")
status = http.StatusAccepted
default:
log.Info("GET /")
log.Info("QueryParams", c.QueryParams())
log.Info("Params", c.ParamNames())
log.Info("Values", c.ParamValues())
if id := c.Param("id"); id != "" {
u = User{1, "Yosuke", "Loking", 30}
}
}
return c.JSON(status, u)
}
}
type Application struct {
E *echo.Echo
}
func App() (app Application) {
app = Application{E: echo.New()}
app.E.Logger.SetLevel(log.DEBUG)
app.E.Use(middleware.Logger())
app.E.GET("/", GenericHandler())
app.E.GET("/:id", GenericHandler())
app.E.POST("/", GenericHandler())
app.E.PUT("/:id", GenericHandler())
app.E.PATCH("/:id", GenericHandler())
app.E.DELETE("/:id", GenericHandler())
return
}
func assert(t *testing.T, x, y any) {
if !reflect.DeepEqual(x, y) {
t.Fatalf("AssertionError: %v != %v", x, y)
}
}
func NewTestClient() Client {
client := New()
client.SetHeaders(DictString{"Authorization": "Token <mytoken>"})
return client
}
var testclient Client = NewTestClient()
func TestGet(t *testing.T) {
url := NewURL("/", nil, nil)
rec := testclient.Get(url, GenericHandler(), nil, DictString{})
assert(t, http.StatusOK, rec.Code)
url = NewURL("/", nil, DictString{"lastname": "kouka", "firstname": "kim"})
rec = testclient.Get(url, GenericHandler(), nil, DictString{})
assert(t, http.StatusOK, rec.Code)
url = NewURL("/:id", DictString{"id": "1"}, nil)
rec = testclient.Get(url, GenericHandler(), nil, DictString{})
assert(t, http.StatusOK, rec.Code)
data := JSONDecode(rec.Body)
assert(t, "Loking", data["lastname"])
}
func TestPost(t *testing.T) {
url := URL{Path: "/"}
u := User{Firstname: "Josué", Lastname: "Kouka", Age: 30}
rec := testclient.Post(url, GenericHandler(), JSONEncode(u), DictString{})
assert(t, http.StatusCreated, rec.Code)
// post form
headers := DictString{"Content-Type": "application/x-www-form-urlencoded"}
rec = testclient.Post(url, GenericHandler(), []byte("firstname=Josué&lastname=Kouka"), headers)
assert(t, "Hello Josué", rec.Body.String())
}
func TestPostMultipartForm(t *testing.T) {
url := URL{Path: "/"}
form, _ := FormData(DictString{"firstname": "Josué", "lastname": "Kouka"}, DictString{"bio": "testdata/bio.txt"})
headers := DictString{"Content-Type": form.ContentType}
rec := testclient.Post(url, GenericHandler(), form.Data, headers)
assert(t, http.StatusCreated, rec.Code)
expected := "Hello Josué ! Your file bio.txt is up."
assert(t, expected, rec.Body.String())
}
func TestPut(t *testing.T) {
url := NewURL("/:id", DictString{"id": "1"}, nil)
u := User{Firstname: "Josué", Lastname: "Kouka", Age: 30}
headers := DictString{"Authorization": "Bearer <mytoken>"}
rec := testclient.Put(url, GenericHandler(), JSONEncode(u), headers)
assert(t, http.StatusNoContent, rec.Code)
}
func TestPatch(t *testing.T) {
url := NewURL("/:id", DictString{"id": "1"}, nil)
u := User{Firstname: "Josué", Lastname: "Kouka", Age: 30}
headers := DictString{"Authorization": "Bearer <mytoken>"}
rec := testclient.Patch(url, GenericHandler(), JSONEncode(u), headers)
assert(t, http.StatusNoContent, rec.Code)
}
func TestDelete(t *testing.T) {
url := NewURL("/:id", DictString{"id": "1"}, nil)
rec := testclient.Delete(url, GenericHandler(), nil, DictString{})
assert(t, http.StatusAccepted, rec.Code)
}