gin-swagger/swagger_test.go

229 lines
5.9 KiB
Go
Raw Normal View History

2017-06-25 09:23:11 +00:00
package ginSwagger
import (
2017-08-04 03:22:19 +00:00
"net/http/httptest"
2018-09-26 02:28:12 +00:00
"os"
2017-08-04 03:22:19 +00:00
"testing"
"github.com/gin-contrib/gzip"
"github.com/swaggo/swag"
2017-06-25 09:23:11 +00:00
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
2017-07-06 16:57:54 +00:00
"github.com/swaggo/gin-swagger/swaggerFiles"
2017-06-25 09:23:11 +00:00
)
2021-08-04 20:19:30 +00:00
type mockedSwag struct{}
func (s *mockedSwag) ReadDoc() string {
return `{
}`
}
2017-06-25 09:23:11 +00:00
func TestWrapHandler(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
2021-08-04 20:19:30 +00:00
router.GET("/*any", WrapHandler(swaggerFiles.Handler, URL("https://github.com/swaggo/gin-swagger")))
2017-06-25 09:23:11 +00:00
w1 := performRequest("GET", "/index.html", router)
assert.Equal(t, 200, w1.Code)
}
func TestWrapCustomHandler(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
router.GET("/*any", CustomWrapHandler(&Config{}, swaggerFiles.Handler))
w1 := performRequest("GET", "/index.html", router)
assert.Equal(t, 200, w1.Code)
2021-10-12 19:35:19 +00:00
assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8")
2017-06-25 09:23:11 +00:00
w2 := performRequest("GET", "/doc.json", router)
2021-08-04 20:19:30 +00:00
assert.Equal(t, 500, w2.Code)
swag.Register(swag.Name, &mockedSwag{})
w2 = performRequest("GET", "/doc.json", router)
2017-06-25 09:23:11 +00:00
assert.Equal(t, 200, w2.Code)
w3 := performRequest("GET", "/favicon-16x16.png", router)
assert.Equal(t, 200, w3.Code)
2021-10-12 19:35:19 +00:00
assert.Equal(t, w3.Header()["Content-Type"][0], "image/png")
2017-06-25 09:23:11 +00:00
2021-10-12 19:35:19 +00:00
w4 := performRequest("GET", "/swagger-ui.css", router)
assert.Equal(t, 200, w4.Code)
assert.Equal(t, w4.Header()["Content-Type"][0], "text/css; charset=utf-8")
w5 := performRequest("GET", "/swagger-ui-bundle.js", router)
assert.Equal(t, 200, w5.Code)
assert.Equal(t, w5.Header()["Content-Type"][0], "application/javascript")
w6 := performRequest("GET", "/notfound", router)
assert.Equal(t, 404, w6.Code)
2018-09-26 02:28:12 +00:00
}
func TestDisablingWrapHandler(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
disablingKey := "SWAGGER_DISABLE"
router.GET("/simple/*any", DisablingWrapHandler(swaggerFiles.Handler, disablingKey))
2018-09-26 02:28:12 +00:00
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")
2018-09-26 02:28:12 +00:00
router.GET("/disabling/*any", DisablingWrapHandler(swaggerFiles.Handler, disablingKey))
2018-09-26 02:28:12 +00:00
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)
w44 := performRequest("GET", "/disabling/oauth2-redirect.html", router)
2018-09-26 02:28:12 +00:00
assert.Equal(t, 404, w44.Code)
w55 := performRequest("GET", "/disabling/notfound", router)
assert.Equal(t, 404, w55.Code)
2017-06-25 09:23:11 +00:00
}
func TestDisablingCustomWrapHandler(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
disablingKey := "SWAGGER_DISABLE2"
router.GET("/simple/*any", DisablingCustomWrapHandler(&Config{}, swaggerFiles.Handler, disablingKey))
w1 := performRequest("GET", "/simple/index.html", router)
assert.Equal(t, 200, w1.Code)
_ = os.Setenv(disablingKey, "true")
router.GET("/disabling/*any", DisablingCustomWrapHandler(&Config{}, swaggerFiles.Handler, disablingKey))
w11 := performRequest("GET", "/disabling/index.html", router)
assert.Equal(t, 404, w11.Code)
}
func TestWithGzipMiddleware(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
router.Use(gzip.Gzip(gzip.BestSpeed))
router.GET("/*any", WrapHandler(swaggerFiles.Handler))
w1 := performRequest("GET", "/index.html", router)
assert.Equal(t, 200, w1.Code)
assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8")
w2 := performRequest("GET", "/swagger-ui.css", router)
assert.Equal(t, 200, w2.Code)
assert.Equal(t, w2.Header()["Content-Type"][0], "text/css; charset=utf-8")
w3 := performRequest("GET", "/swagger-ui-bundle.js", router)
assert.Equal(t, 200, w3.Code)
assert.Equal(t, w3.Header()["Content-Type"][0], "application/javascript")
w4 := performRequest("GET", "/doc.json", router)
assert.Equal(t, 200, w4.Code)
assert.Equal(t, w4.Header()["Content-Type"][0], "application/json; charset=utf-8")
}
2017-06-25 09:23:11 +00:00
func performRequest(method, target string, router *gin.Engine) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, target, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
return w
}
2021-08-04 20:19:30 +00:00
func TestURL(t *testing.T) {
cfg := Config{}
expected := "https://github.com/swaggo/http-swagger"
2021-08-04 20:19:30 +00:00
configFunc := URL(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.URL)
}
func TestDocExpansion(t *testing.T) {
var cfg Config
expected := "list"
configFunc := DocExpansion(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.DocExpansion)
expected = "full"
configFunc = DocExpansion(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.DocExpansion)
expected = "none"
configFunc = DocExpansion(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.DocExpansion)
}
2021-08-04 20:19:30 +00:00
func TestDeepLinking(t *testing.T) {
var cfg Config
assert.Equal(t, false, cfg.DeepLinking)
configFunc := DeepLinking(true)
2021-08-04 20:19:30 +00:00
configFunc(&cfg)
assert.Equal(t, true, cfg.DeepLinking)
configFunc = DeepLinking(false)
configFunc(&cfg)
assert.Equal(t, false, cfg.DeepLinking)
2021-08-04 20:19:30 +00:00
}
func TestDefaultModelsExpandDepth(t *testing.T) {
var cfg Config
assert.Equal(t, 0, cfg.DefaultModelsExpandDepth)
expected := -1
configFunc := DefaultModelsExpandDepth(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.DefaultModelsExpandDepth)
expected = 1
configFunc = DefaultModelsExpandDepth(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.DefaultModelsExpandDepth)
}
func TestInstanceName(t *testing.T) {
var cfg Config
assert.Equal(t, "", cfg.InstanceName)
expected := swag.Name
configFunc := InstanceName(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.InstanceName)
expected = "custom_name"
configFunc = InstanceName(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.InstanceName)
}