add tests
This commit is contained in:
+3
-8
@@ -2,7 +2,6 @@ package ginSwagger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/labstack/gommon/log"
|
|
||||||
"github.com/swag-gonic/swag/swagger"
|
"github.com/swag-gonic/swag/swagger"
|
||||||
"golang.org/x/net/webdav"
|
"golang.org/x/net/webdav"
|
||||||
"html/template"
|
"html/template"
|
||||||
@@ -19,7 +18,7 @@ func WrapHandler(h *webdav.Handler) gin.HandlerFunc {
|
|||||||
Host string
|
Host string
|
||||||
}
|
}
|
||||||
|
|
||||||
var re = regexp.MustCompile(`(.*)(/|index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[\?|.]*`)
|
var re = regexp.MustCompile(`(.*)(index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[\?|.]*`)
|
||||||
|
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
var matches []string
|
var matches []string
|
||||||
@@ -33,15 +32,11 @@ func WrapHandler(h *webdav.Handler) gin.HandlerFunc {
|
|||||||
h.Prefix = prefix
|
h.Prefix = prefix
|
||||||
|
|
||||||
switch path {
|
switch path {
|
||||||
case "/", "index.html":
|
case "index.html":
|
||||||
s := &pro{
|
s := &pro{
|
||||||
Host: "doc.json", //TODO: provide to customs?
|
Host: "doc.json", //TODO: provide to customs?
|
||||||
}
|
}
|
||||||
|
index.Execute(c.Writer, s)
|
||||||
if err := index.Execute(c.Writer, s); err != nil {
|
|
||||||
log.Fatal("Execute:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case "doc.json":
|
case "doc.json":
|
||||||
c.Writer.Write([]byte(swagger.ReadDoc()))
|
c.Writer.Write([]byte(swagger.ReadDoc()))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package ginSwagger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/swag-gonic/gin-swagger/swaggerFiles"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
_ "github.com/swag-gonic/gin-swagger/example/docs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWrapHandler(t *testing.T) {
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
router := gin.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 performRequest(method, target string, router *gin.Engine) *httptest.ResponseRecorder {
|
||||||
|
r := httptest.NewRequest(method, target, nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
router.ServeHTTP(w, r)
|
||||||
|
return w
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user