Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"

"github.com/labstack/echo/v4"
swaggerFiles "github.com/swaggo/files/v2"
Expand Down Expand Up @@ -142,7 +143,12 @@ func EchoWrapHandler(options ...func(*Config)) echo.HandlerFunc {
return c.String(http.StatusMethodNotAllowed, http.StatusText(http.StatusMethodNotAllowed))
}

matches := re.FindStringSubmatch(c.Request().RequestURI)
requestURI := c.Request().RequestURI
if idx := strings.Index(requestURI, "?"); idx != -1 {
requestURI = requestURI[:idx]
}

matches := re.FindStringSubmatch(requestURI)
path := matches[2]

switch filepath.Ext(path) {
Expand Down Expand Up @@ -216,7 +222,12 @@ func EchoWrapHandlerV3(options ...func(*Config)) echo.HandlerFunc {
return echo.NewHTTPError(http.StatusMethodNotAllowed, http.StatusText(http.StatusMethodNotAllowed))
}

matches := re.FindStringSubmatch(c.Request().RequestURI)
requestURI := c.Request().RequestURI
if idx := strings.Index(requestURI, "?"); idx != -1 {
requestURI = requestURI[:idx]
}

matches := re.FindStringSubmatch(requestURI)
path := matches[2]

switch filepath.Ext(path) {
Expand Down Expand Up @@ -372,7 +383,14 @@ window.onload = function() {
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
layout: "StandaloneLayout",
requestInterceptor: (req) => {
if (req.headers) {
delete req.headers["X-Requested-With"]
delete req.headers["x-requested-with"]
}
return req
}
})

{{if .OAuth}}
Expand Down
26 changes: 26 additions & 0 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ func TestWrapHandler(t *testing.T) {
w1 := performRequest(http.MethodGet, "/index.html", router)
assert.Equal(t, http.StatusOK, w1.Code)
assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8")
body, err := io.ReadAll(w1.Body)
assert.NoError(t, err)
assert.Contains(t, string(body), "requestInterceptor")

assert.Equal(t, http.StatusInternalServerError, performRequest(http.MethodGet, "/doc.json", router).Code)

Expand Down Expand Up @@ -279,6 +282,16 @@ func TestWrapHandler(t *testing.T) {

}

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

router.Any("/*", EchoWrapHandler())

w := performRequest(http.MethodGet, "/oauth2-redirect.html?code=abc123&state=xyz", router)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
}

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

Expand All @@ -287,6 +300,9 @@ func TestWrapHandlerV3(t *testing.T) {
w1 := performRequest(http.MethodGet, "/index.html", router)
assert.Equal(t, http.StatusOK, w1.Code)
assert.Equal(t, w1.Header()["Content-Type"][0], "text/html; charset=utf-8")
body, err := io.ReadAll(w1.Body)
assert.NoError(t, err)
assert.Contains(t, string(body), "requestInterceptor")

assert.Equal(t, http.StatusInternalServerError, performRequest(http.MethodGet, "/doc.json", router).Code)

Expand Down Expand Up @@ -323,6 +339,16 @@ func TestWrapHandlerV3(t *testing.T) {

}

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

router.Any("/*", EchoWrapHandlerV3())

w := performRequest(http.MethodGet, "/oauth2-redirect.html?code=abc123&state=xyz", router)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
}

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

Expand Down