-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler_test.go
142 lines (97 loc) · 3.21 KB
/
handler_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
package openapi
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
type TestHandler struct {
*Handler
}
func (h *Handler) Root(c echo.Context) error {
return h.Validate(c, http.StatusOK, echo.Map{"message": "welcome"})
}
func (h *Handler) Validation(c echo.Context) error {
return h.Validate(c, http.StatusOK, echo.Map{"invalid": "welcome"})
}
func (h *Handler) NoContent(c echo.Context) error {
return h.Validate(c, http.StatusNoContent, nil)
}
func (h *Handler) Text(c echo.Context) error {
return h.ValidateWithContentType(c, http.StatusOK, echo.MIMETextPlain, "ok")
}
func (h *Handler) Bytes(c echo.Context) error {
return h.ValidateWithContentType(c, http.StatusOK, echo.MIMETextPlain, []byte(`ok`))
}
func (h *Handler) InvalidType(c echo.Context) error {
return h.ValidateWithContentType(c, http.StatusOK, echo.MIMETextPlain, []string{})
}
func TestHandler_Validate_Defaults(t *testing.T) {
e := echo.New()
h := TestHandler{NewHandler()}
e.Add(http.MethodGet, "/", h.Root)
e.Use(OpenAPI("./fixtures/openapi.yaml"))
req := httptest.NewRequest(http.MethodGet, "/", nil)
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
}
func TestHandler_ValidateWithConfig_Defaults(t *testing.T) {
e := echo.New()
h := TestHandler{NewHandlerWithConfig(HandlerConfig{})}
e.Add(http.MethodGet, "/", h.Root)
e.Use(OpenAPI("./fixtures/openapi.yaml"))
req := httptest.NewRequest(http.MethodGet, "/", nil)
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
}
func TestHandler_Validate_Error(t *testing.T) {
e := echo.New()
h := TestHandler{NewHandler()}
e.Add(http.MethodGet, "/", h.Validation)
e.Use(OpenAPI("./fixtures/openapi.yaml"))
req := httptest.NewRequest(http.MethodGet, "/", nil)
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(t, http.StatusInternalServerError, resp.Code)
}
func TestHandler_Validate_No_Content(t *testing.T) {
e := echo.New()
h := TestHandler{NewHandler()}
e.Add(http.MethodPost, "/no-content", h.NoContent)
e.Use(OpenAPI("./fixtures/openapi.yaml"))
req := httptest.NewRequest(http.MethodPost, "/no-content", nil)
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(t, http.StatusNoContent, resp.Code)
}
func TestHandler_ValidateWithContentType_Text_Plain(t *testing.T) {
h := TestHandler{NewHandler()}
testCases := []struct {
name string
handler echo.HandlerFunc
statusCode int
}{
{"string", h.Text, http.StatusOK},
{"bytes", h.Bytes, http.StatusOK},
{"invalid type", h.InvalidType, http.StatusInternalServerError},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
e := echo.New()
e.Add(http.MethodGet, "/text", tc.handler)
e.Use(OpenAPI("./fixtures/openapi.yaml"))
req := httptest.NewRequest(http.MethodGet, "/text", nil)
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
if tc.statusCode == http.StatusOK {
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, echo.MIMETextPlain, resp.Header().Get("Content-Type"))
} else {
assert.Equal(t, http.StatusInternalServerError, resp.Code)
}
})
}
}