-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger_test.go
55 lines (40 loc) · 1.28 KB
/
swagger_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
package echo_swagger
import (
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"net/http/httptest"
"testing"
)
func performRequest(method, target string, app *echo.Echo) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, target, nil)
rec := httptest.NewRecorder()
app.ServeHTTP(rec,req)
return rec
}
func TestMiddleware_Register(t *testing.T) {
t.Run("Endpoint check", func(t *testing.T) {
app := echo.New()
middleware := NewMiddleware("./docs/swagger_test.json", "/")
middleware.Register(app)
w1 := performRequest("GET", "/docs", app)
assert.Equal(t, 200, w1.Code)
w2 := performRequest("GET", "/swagger.json", app)
assert.Equal(t, 200, w2.Code)
w3 := performRequest("GET", "/notfound", app)
assert.Equal(t, 404, w3.Code)
})
t.Run("Swagger.json file is not exist", func(t *testing.T) {
app := echo.New()
middleware := NewMiddleware("./docs/swagger.json", "/")
assert.Panics(t, func() {
middleware.Register(app)
}, "/swagger.json file is not exist")
})
t.Run("Swagger.json missing file", func(t *testing.T) {
app := echo.New()
middleware := NewMiddleware("./docs/swagger_missing_test.json", "/")
assert.Panics(t, func() {
middleware.Register(app)
}, "invalid character ':' after object key:value pair")
})
}