Merge pull request #328 from illionillion/fix/swagger-index-html-direct-response

fix: return index.html for /swagger and /swagger/ instead of 404
This commit is contained in:
sdghchj 2025-12-20 16:15:05 +08:00 committed by GitHub
commit 1b6744c941
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 1 deletions

View File

@ -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

View File

@ -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)

View File

@ -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(), "<title>Swagger UI</title>")
// 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(), "<title>Swagger UI</title>")
}
func TestDisablingWrapHandler(t *testing.T) {