diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f0af436..25b8eb7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: steps: - uses: actions/checkout@master - name: Set up Go - uses: actions/setup-go@v1 + uses: actions/setup-go@v4 with: go-version: ${{ matrix.go }} - name: test diff --git a/swagger.go b/swagger.go index 7e0e414..3888ede 100644 --- a/swagger.go +++ b/swagger.go @@ -159,6 +159,12 @@ func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc var matcher = regexp.MustCompile(`(.*)(index\.html|index\.css|swagger-initializer\.js|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(ctx *gin.Context) { + // Return index.html content for /swagger or /swagger/ + if ctx.Request.Method == http.MethodGet && (ctx.Request.RequestURI == "/swagger" || ctx.Request.RequestURI == "/swagger/") { + ctx.Header("Content-Type", "text/html; charset=utf-8") + _ = index.Execute(ctx.Writer, config.toSwaggerConfig()) + return + } if ctx.Request.Method != http.MethodGet { ctx.AbortWithStatus(http.StatusMethodNotAllowed) diff --git a/swagger_test.go b/swagger_test.go index 85c5cb7..8e6afba 100644 --- a/swagger_test.go +++ b/swagger_test.go @@ -80,6 +80,16 @@ func TestWrapCustomHandler(t *testing.T) { assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPost, "/index.html", router).Code) assert.Equal(t, http.StatusMethodNotAllowed, performRequest(http.MethodPut, "/index.html", router).Code) + + // Test: /swagger should redirect to /swagger/index.html (not implemented yet, should fail) + w := performRequest(http.MethodGet, "/swagger", router) + assert.Equal(t, http.StatusOK, w.Code) + assert.Contains(t, w.Body.String(), "Swagger UI") + + // Test: /swagger/ should also return index.html + wSlash := performRequest(http.MethodGet, "/swagger/", router) + assert.Equal(t, http.StatusOK, wSlash.Code) + assert.Contains(t, wSlash.Body.String(), "Swagger UI") } func TestDisablingWrapHandler(t *testing.T) {