Skip to content

Commit

Permalink
improve unit test (swaggo#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan authored Aug 4, 2021
1 parent f5258e9 commit 71be254
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
6 changes: 4 additions & 2 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ginSwagger

import (
"html/template"
"net/http"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -96,10 +97,11 @@ func CustomWrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc {
case "doc.json":
doc, err := swag.ReadDoc()
if err != nil {
panic(err)
c.AbortWithStatus(http.StatusInternalServerError)

return
}
c.Writer.Write([]byte(doc))
return
default:
locker.RLock()
h.ServeHTTP(c.Writer, c.Request)
Expand Down
34 changes: 31 additions & 3 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@ package ginSwagger

import (
"github.com/gin-contrib/gzip"
"github.com/swaggo/swag"

"net/http/httptest"
"os"
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"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) {
gin.SetMode(gin.TestMode)
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)
assert.Equal(t, 200, w1.Code)
Expand All @@ -33,6 +40,11 @@ func TestWrapCustomHandler(t *testing.T) {
assert.Equal(t, 200, w1.Code)

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)

w3 := performRequest("GET", "/favicon-16x16.png", router)
Expand Down Expand Up @@ -129,3 +141,19 @@ func performRequest(method, target string, router *gin.Engine) *httptest.Respons
router.ServeHTTP(w, r)
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)
}

0 comments on commit 71be254

Please sign in to comment.