improve unit test (#152)

This commit is contained in:
Bogdan U
2021-08-04 23:19:30 +03:00
committed by GitHub
parent f5258e93ab
commit 71be2548eb
2 changed files with 35 additions and 5 deletions
+4 -2
View File
@@ -2,6 +2,7 @@ package ginSwagger
import ( import (
"html/template" "html/template"
"net/http"
"os" "os"
"regexp" "regexp"
"strings" "strings"
@@ -96,10 +97,11 @@ func CustomWrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc {
case "doc.json": case "doc.json":
doc, err := swag.ReadDoc() doc, err := swag.ReadDoc()
if err != nil { if err != nil {
panic(err) c.AbortWithStatus(http.StatusInternalServerError)
return
} }
c.Writer.Write([]byte(doc)) c.Writer.Write([]byte(doc))
return
default: default:
locker.RLock() locker.RLock()
h.ServeHTTP(c.Writer, c.Request) h.ServeHTTP(c.Writer, c.Request)
+31 -3
View File
@@ -2,6 +2,8 @@ package ginSwagger
import ( import (
"github.com/gin-contrib/gzip" "github.com/gin-contrib/gzip"
"github.com/swaggo/swag"
"net/http/httptest" "net/http/httptest"
"os" "os"
"testing" "testing"
@@ -9,15 +11,20 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/swaggo/gin-swagger/swaggerFiles" "github.com/swaggo/gin-swagger/swaggerFiles"
_ "github.com/swaggo/gin-swagger/example/basic/docs"
) )
type mockedSwag struct{}
func (s *mockedSwag) ReadDoc() string {
return `{
}`
}
func TestWrapHandler(t *testing.T) { func TestWrapHandler(t *testing.T) {
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
router := gin.New() router := gin.New()
router.GET("/*any", WrapHandler(swaggerFiles.Handler)) router.GET("/*any", WrapHandler(swaggerFiles.Handler, URL("https://github.com/swaggo/gin-swagger")))
w1 := performRequest("GET", "/index.html", router) w1 := performRequest("GET", "/index.html", router)
assert.Equal(t, 200, w1.Code) assert.Equal(t, 200, w1.Code)
@@ -33,6 +40,11 @@ func TestWrapCustomHandler(t *testing.T) {
assert.Equal(t, 200, w1.Code) assert.Equal(t, 200, w1.Code)
w2 := performRequest("GET", "/doc.json", router) w2 := performRequest("GET", "/doc.json", router)
assert.Equal(t, 500, w2.Code)
swag.Register(swag.Name, &mockedSwag{})
w2 = performRequest("GET", "/doc.json", router)
assert.Equal(t, 200, w2.Code) assert.Equal(t, 200, w2.Code)
w3 := performRequest("GET", "/favicon-16x16.png", router) w3 := performRequest("GET", "/favicon-16x16.png", router)
@@ -129,3 +141,19 @@ func performRequest(method, target string, router *gin.Engine) *httptest.Respons
router.ServeHTTP(w, r) router.ServeHTTP(w, r)
return w return w
} }
func TestURL(t *testing.T) {
expected := "https://github.com/swaggo/http-swagger"
cfg := Config{}
configFunc := URL(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.URL)
}
func TestDeepLinking(t *testing.T) {
expected := true
cfg := Config{}
configFunc := DeepLinking(expected)
configFunc(&cfg)
assert.Equal(t, expected, cfg.DeepLinking)
}