2017-06-25 09:23:11 +00:00
|
|
|
package ginSwagger
|
|
|
|
|
|
|
|
|
|
import (
|
2022-04-22 13:50:33 +00:00
|
|
|
"io/ioutil"
|
2022-04-16 07:34:09 +00:00
|
|
|
"net/http"
|
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"
|
|
|
|
|
|
2021-09-25 11:14:03 +00:00
|
|
|
"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"
|
2022-06-11 06:56:01 +00:00
|
|
|
swaggerFiles "github.com/swaggo/files"
|
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
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
assert.Equal(t, http.StatusOK, performRequest("GET", "/index.html", router).Code)
|
2019-03-24 01:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestWrapCustomHandler(t *testing.T) {
|
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
router := gin.New()
|
|
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
router.Any("/*any", CustomWrapHandler(&Config{}, swaggerFiles.Handler))
|
2019-03-24 01:56:13 +00:00
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
w1 := performRequest(http.MethodGet, "/index.html", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, 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
|
|
|
|
2023-03-20 22:16:58 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, performRequest(http.MethodGet, "/doc.json", router).Code)
|
|
|
|
|
|
|
|
|
|
doc := &mockedSwag{}
|
|
|
|
|
swag.Register(swag.Name, doc)
|
2021-08-04 20:19:30 +00:00
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
w2 := performRequest(http.MethodGet, "/doc.json", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, w2.Code)
|
|
|
|
|
assert.Equal(t, w2.Header()["Content-Type"][0], "application/json; charset=utf-8")
|
2017-06-25 09:23:11 +00:00
|
|
|
|
2022-04-22 13:50:33 +00:00
|
|
|
// Perform body rendering validation
|
|
|
|
|
w2Body, err := ioutil.ReadAll(w2.Body)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, doc.ReadDoc(), string(w2Body))
|
|
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
w3 := performRequest(http.MethodGet, "/favicon-16x16.png", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, 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
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
w4 := performRequest(http.MethodGet, "/swagger-ui.css", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, w4.Code)
|
2021-10-12 19:35:19 +00:00
|
|
|
assert.Equal(t, w4.Header()["Content-Type"][0], "text/css; charset=utf-8")
|
|
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
w5 := performRequest(http.MethodGet, "/swagger-ui-bundle.js", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, w5.Code)
|
2021-10-12 19:35:19 +00:00
|
|
|
assert.Equal(t, w5.Header()["Content-Type"][0], "application/javascript")
|
|
|
|
|
|
2023-09-07 07:04:27 +00:00
|
|
|
w6 := performRequest(http.MethodGet, "/index.css", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, w6.Code)
|
|
|
|
|
assert.Equal(t, w6.Header()["Content-Type"][0], "text/css; charset=utf-8")
|
|
|
|
|
|
|
|
|
|
w7 := performRequest(http.MethodGet, "/swagger-initializer.js", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, w7.Code)
|
|
|
|
|
assert.Equal(t, w7.Header()["Content-Type"][0], "application/javascript")
|
|
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
assert.Equal(t, http.StatusNotFound, performRequest(http.MethodGet, "/notfound", router).Code)
|
2021-11-04 20:25:29 +00:00
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPost, "/index.html", router).Code)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPut, "/index.html", router).Code)
|
2018-09-26 02:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDisablingWrapHandler(t *testing.T) {
|
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
|
|
|
|
|
router := gin.New()
|
|
|
|
|
disablingKey := "SWAGGER_DISABLE"
|
|
|
|
|
|
2019-03-23 10:16:08 +00:00
|
|
|
router.GET("/simple/*any", DisablingWrapHandler(swaggerFiles.Handler, disablingKey))
|
2018-09-26 02:28:12 +00:00
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
assert.Equal(t, http.StatusOK, performRequest(http.MethodGet, "/simple/index.html", router).Code)
|
|
|
|
|
assert.Equal(t, http.StatusOK, performRequest(http.MethodGet, "/simple/doc.json", router).Code)
|
2018-09-26 02:28:12 +00:00
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
assert.Equal(t, http.StatusOK, performRequest(http.MethodGet, "/simple/favicon-16x16.png", router).Code)
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, performRequest(http.MethodGet, "/simple/notfound", router).Code)
|
2018-09-26 02:28:12 +00:00
|
|
|
|
2021-09-25 11:14:03 +00:00
|
|
|
_ = os.Setenv(disablingKey, "true")
|
2018-09-26 02:28:12 +00:00
|
|
|
|
2019-03-23 10:16:08 +00:00
|
|
|
router.GET("/disabling/*any", DisablingWrapHandler(swaggerFiles.Handler, disablingKey))
|
2018-09-26 02:28:12 +00:00
|
|
|
|
2022-04-22 13:50:33 +00:00
|
|
|
assert.Equal(t, http.StatusNotFound, performRequest(http.MethodGet, "/disabling/index.html", router).Code)
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, performRequest(http.MethodGet, "/disabling/doc.json", router).Code)
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, performRequest(http.MethodGet, "/disabling/oauth2-redirect.html", router).Code)
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, performRequest(http.MethodGet, "/disabling/notfound", router).Code)
|
2017-06-25 09:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-24 01:56:13 +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))
|
|
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
assert.Equal(t, http.StatusOK, performRequest(http.MethodGet, "/simple/index.html", router).Code)
|
2019-03-24 01:56:13 +00:00
|
|
|
|
2021-09-25 11:14:03 +00:00
|
|
|
_ = os.Setenv(disablingKey, "true")
|
2019-03-24 01:56:13 +00:00
|
|
|
|
|
|
|
|
router.GET("/disabling/*any", DisablingCustomWrapHandler(&Config{}, swaggerFiles.Handler, disablingKey))
|
|
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
assert.Equal(t, http.StatusNotFound, performRequest(http.MethodGet, "/disabling/index.html", router).Code)
|
2019-03-24 01:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-03 02:59:18 +00:00
|
|
|
func TestWithGzipMiddleware(t *testing.T) {
|
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
router := gin.New()
|
|
|
|
|
|
|
|
|
|
router.Use(gzip.Gzip(gzip.BestSpeed))
|
|
|
|
|
|
|
|
|
|
router.GET("/*any", WrapHandler(swaggerFiles.Handler))
|
|
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
w1 := performRequest(http.MethodGet, "/index.html", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, w1.Code)
|
2019-04-03 02:59:18 +00:00
|
|
|
assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8")
|
|
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
w2 := performRequest(http.MethodGet, "/swagger-ui.css", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, w2.Code)
|
2019-04-03 02:59:18 +00:00
|
|
|
assert.Equal(t, w2.Header()["Content-Type"][0], "text/css; charset=utf-8")
|
|
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
w3 := performRequest(http.MethodGet, "/swagger-ui-bundle.js", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, w3.Code)
|
2019-04-03 02:59:18 +00:00
|
|
|
assert.Equal(t, w3.Header()["Content-Type"][0], "application/javascript")
|
|
|
|
|
|
2022-04-16 07:34:09 +00:00
|
|
|
w4 := performRequest(http.MethodGet, "/doc.json", router)
|
|
|
|
|
assert.Equal(t, http.StatusOK, w4.Code)
|
2021-08-11 20:26:37 +00:00
|
|
|
assert.Equal(t, w4.Header()["Content-Type"][0], "application/json; charset=utf-8")
|
2019-04-03 02:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
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{}
|
2021-09-25 11:14:03 +00:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-25 11:14:03 +00:00
|
|
|
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) {
|
2021-09-25 11:14:03 +00:00
|
|
|
var cfg Config
|
|
|
|
|
assert.Equal(t, false, cfg.DeepLinking)
|
|
|
|
|
|
|
|
|
|
configFunc := DeepLinking(true)
|
2021-08-04 20:19:30 +00:00
|
|
|
configFunc(&cfg)
|
2021-09-25 11:14:03 +00:00
|
|
|
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
|
|
|
}
|
2021-09-24 22:45:34 +00:00
|
|
|
|
|
|
|
|
func TestDefaultModelsExpandDepth(t *testing.T) {
|
2021-09-25 11:14:03 +00:00
|
|
|
var cfg Config
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, 0, cfg.DefaultModelsExpandDepth)
|
|
|
|
|
|
2021-09-24 22:45:34 +00:00
|
|
|
expected := -1
|
|
|
|
|
configFunc := DefaultModelsExpandDepth(expected)
|
|
|
|
|
configFunc(&cfg)
|
|
|
|
|
assert.Equal(t, expected, cfg.DefaultModelsExpandDepth)
|
2021-09-25 11:14:03 +00:00
|
|
|
|
|
|
|
|
expected = 1
|
|
|
|
|
configFunc = DefaultModelsExpandDepth(expected)
|
|
|
|
|
configFunc(&cfg)
|
|
|
|
|
assert.Equal(t, expected, cfg.DefaultModelsExpandDepth)
|
|
|
|
|
}
|
2021-11-04 22:49:34 +00:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2022-02-06 11:05:35 +00:00
|
|
|
|
|
|
|
|
func TestPersistAuthorization(t *testing.T) {
|
|
|
|
|
var cfg Config
|
|
|
|
|
assert.Equal(t, false, cfg.PersistAuthorization)
|
|
|
|
|
|
|
|
|
|
configFunc := PersistAuthorization(true)
|
|
|
|
|
configFunc(&cfg)
|
|
|
|
|
assert.Equal(t, true, cfg.PersistAuthorization)
|
|
|
|
|
|
|
|
|
|
configFunc = PersistAuthorization(false)
|
|
|
|
|
configFunc(&cfg)
|
|
|
|
|
assert.Equal(t, false, cfg.PersistAuthorization)
|
|
|
|
|
}
|
2022-05-04 14:13:38 +00:00
|
|
|
|
|
|
|
|
func TestOauth2DefaultClientID(t *testing.T) {
|
|
|
|
|
var cfg Config
|
|
|
|
|
assert.Equal(t, "", cfg.Oauth2DefaultClientID)
|
|
|
|
|
|
|
|
|
|
configFunc := Oauth2DefaultClientID("default_client_id")
|
|
|
|
|
configFunc(&cfg)
|
|
|
|
|
assert.Equal(t, "default_client_id", cfg.Oauth2DefaultClientID)
|
|
|
|
|
|
|
|
|
|
configFunc = Oauth2DefaultClientID("")
|
|
|
|
|
configFunc(&cfg)
|
|
|
|
|
assert.Equal(t, "", cfg.Oauth2DefaultClientID)
|
|
|
|
|
}
|