add tests

This commit is contained in:
Eason Lin
2017-06-25 17:23:11 +08:00
parent 1e97bf3e19
commit ac24661f04
2 changed files with 41 additions and 8 deletions
+3 -8
View File
@@ -2,7 +2,6 @@ package ginSwagger
import (
"github.com/gin-gonic/gin"
"github.com/labstack/gommon/log"
"github.com/swag-gonic/swag/swagger"
"golang.org/x/net/webdav"
"html/template"
@@ -19,7 +18,7 @@ func WrapHandler(h *webdav.Handler) gin.HandlerFunc {
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) {
var matches []string
@@ -33,15 +32,11 @@ func WrapHandler(h *webdav.Handler) gin.HandlerFunc {
h.Prefix = prefix
switch path {
case "/", "index.html":
case "index.html":
s := &pro{
Host: "doc.json", //TODO: provide to customs?
}
if err := index.Execute(c.Writer, s); err != nil {
log.Fatal("Execute:", err)
return
}
index.Execute(c.Writer, s)
case "doc.json":
c.Writer.Write([]byte(swagger.ReadDoc()))
return
+38
View File
@@ -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
}