Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
easonlin404 committed Jun 25, 2017
1 parent 1e97bf3 commit ac24661
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
11 changes: 3 additions & 8 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down
38 changes: 38 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ac24661

Please sign in to comment.