Skip to content

Commit

Permalink
fix: handler reuse on different paths (#63)
Browse files Browse the repository at this point in the history
* fix handler reuse on 2 different paths
  • Loading branch information
ubogdan authored Oct 27, 2021
1 parent f298ba3 commit f33cf31
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/labstack/echo/v4"
swaggerFiles "github.com/swaggo/files"
"github.com/swaggo/swag"
"golang.org/x/net/webdav"
)

// Config stores echoSwagger configuration variables.
Expand Down Expand Up @@ -56,8 +57,6 @@ var WrapHandler = EchoWrapHandler()
func EchoWrapHandler(configFns ...func(c *Config)) echo.HandlerFunc {
var once sync.Once

h := swaggerFiles.Handler

config := &Config{
URL: "doc.json",
DeepLinking: true,
Expand All @@ -75,6 +74,11 @@ func EchoWrapHandler(configFns ...func(c *Config)) echo.HandlerFunc {

var re = regexp.MustCompile(`^(.*/)([^?].*)?[?|.]*$`)

h := webdav.Handler{
FileSystem: swaggerFiles.FS,
LockSystem: webdav.NewMemLS(),
}

return func(c echo.Context) error {
matches := re.FindStringSubmatch(c.Request().RequestURI)
path := matches[2]
Expand Down
23 changes: 23 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,29 @@ func TestWrapHandler(t *testing.T) {
assert.Equal(t, 301, w7.Code)
}

func TestHandlerReuse(t *testing.T) {
router := echo.New()

router.GET("/swagger/*", EchoWrapHandler())
router.GET("/admin/swagger/*", EchoWrapHandler())

w1 := performRequest("GET", "/swagger/index.html", router)
assert.Equal(t, 200, w1.Code)
assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8")

w2 := performRequest("GET", "/admin/swagger/index.html", router)
assert.Equal(t, 200, w2.Code)
assert.Equal(t, w2.Header()["Content-Type"][0], "text/html; charset=utf-8")

w3 := performRequest("GET", "/swagger/index.html", router)
assert.Equal(t, 200, w3.Code)
assert.Equal(t, w3.Header()["Content-Type"][0], "text/html; charset=utf-8")

w4 := performRequest("GET", "/admin/swagger/index.html", router)
assert.Equal(t, 200, w4.Code)
assert.Equal(t, w4.Header()["Content-Type"][0], "text/html; charset=utf-8")
}

func performRequest(method, target string, e *echo.Echo) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, target, nil)
w := httptest.NewRecorder()
Expand Down

0 comments on commit f33cf31

Please sign in to comment.