-
Notifications
You must be signed in to change notification settings - Fork 0
/
swagger_test.go
76 lines (51 loc) · 1.94 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package swagger
import (
"net/http/httptest"
"os"
"testing"
"github.com/kataras/iris"
"github.com/stretchr/testify/assert"
"github.com/huntsman-li/iris-swagger/swaggerFiles"
_ "github.com/huntsman-li/iris-swagger/example/docs"
)
func TestWrapHandler(t *testing.T) {
router := iris.New()
router.Get("/*any", WrapHandler(swaggerFiles.Handler))
w1 := performRequest("GET", "/index.html", router)
assert.Equal(t, 200, w1.Code)
w2 := performRequest("GET", "/doc.json", router)
assert.Equal(t, 200, w2.Code)
w3 := performRequest("GET", "/favicon-16x16.png", router)
assert.Equal(t, 200, w3.Code)
w4 := performRequest("GET", "/notfound", router)
assert.Equal(t, 404, w4.Code)
}
func TestDisablingWrapHandler(t *testing.T) {
router := iris.New()
disablingKey := "SWAGGER_DISABLE"
router.Get("/simple/*any", DisablingWrapHandler(swaggerFiles.Handler, disablingKey))
w1 := performRequest("GET", "/simple/index.html", router)
assert.Equal(t, 200, w1.Code)
w2 := performRequest("GET", "/simple/doc.json", router)
assert.Equal(t, 200, w2.Code)
w3 := performRequest("GET", "/simple/favicon-16x16.png", router)
assert.Equal(t, 200, w3.Code)
w4 := performRequest("GET", "/simple/notfound", router)
assert.Equal(t, 404, w4.Code)
os.Setenv(disablingKey, "true")
router.Get("/disabling/*any", DisablingWrapHandler(swaggerFiles.Handler, disablingKey))
w11 := performRequest("GET", "/disabling/index.html", router)
assert.Equal(t, 404, w11.Code)
w22 := performRequest("GET", "/disabling/doc.json", router)
assert.Equal(t, 404, w22.Code)
w33 := performRequest("GET", "/disabling/favicon-16x16.png", router)
assert.Equal(t, 404, w33.Code)
w44 := performRequest("GET", "/disabling/notfound", router)
assert.Equal(t, 404, w44.Code)
}
func performRequest(method, target string, router *iris.Application) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, target, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
return w
}