Skip to content

Commit

Permalink
Fix redirecting on gin router with prefixed path
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
fabiante committed Jun 15, 2024
1 parent d4b16f4 commit 5adeab0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
16 changes: 12 additions & 4 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ func NewHandler(specYML []byte, opts ...Option) (*Handler, error) {
func (handler *Handler) Register(router gin.IRoutes) {
router.GET("/openapi.yml", handler.GetSpec)
router.StaticFS("/swagger-ui", handler.FS)
router.GET("/swaggerui", func(ctx *gin.Context) {
ctx.Redirect(http.StatusPermanentRedirect, getRedirectPath(ctx))
})
router.GET("/swaggerui", handler.Redirect)
}

// Option objects used to construct Handler objects.
Expand Down Expand Up @@ -145,6 +143,16 @@ func (handler *Handler) GetSpec(ctx *gin.Context) {
ctx.YAML(200, spec)
}

func getRedirectPath(_ *gin.Context) string {
func (handler *Handler) Redirect(ctx *gin.Context) {
ctx.Redirect(http.StatusPermanentRedirect, getRedirectPath(ctx))
}

func getRedirectPath(ctx *gin.Context) string {
if ctx != nil && ctx.Request != nil && ctx.Request.URL != nil {
p := ctx.Request.URL.Path
p = strings.TrimSuffix(p, "/swaggerui")
return p + "/swagger-ui"
}

return "/swagger-ui"
}
55 changes: 54 additions & 1 deletion handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,58 @@ func TestHandler(t *testing.T) {
}

func TestGetRedirectPath(t *testing.T) {
require.Equal(t, "/swagger-ui", getRedirectPath(nil))
gin.SetMode(gin.TestMode)

t.Run("returns proper path when using no prefix", func(t *testing.T) {
engine := gin.New()
testServer := httptest.NewServer(engine)
defer testServer.Close()

handler := &Handler{}

// Register redirect handler on prefixed path
engine.Any("/swaggerui", handler.Redirect)

client := testServer.Client()

redirectCalled := false

client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
require.Equal(t, "/swagger-ui", req.URL.Path)
redirectCalled = true
return nil
}

_, err := client.Get(testServer.URL + "/swaggerui")
assert.NoError(t, err)

require.True(t, redirectCalled, "handler did not respond with redirect")
})

t.Run("returns proper path when using prefix", func(t *testing.T) {
engine := gin.New()
testServer := httptest.NewServer(engine)
defer testServer.Close()

handler := &Handler{}

// Register redirect handler on prefixed path
group := engine.Group("/prefix")
group.Any("/swaggerui", handler.Redirect)

client := testServer.Client()

redirectCalled := false

client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
require.Equal(t, "/prefix/swagger-ui", req.URL.Path)
redirectCalled = true
return nil
}

_, err := client.Get(testServer.URL + "/prefix/swaggerui")
assert.NoError(t, err)

require.True(t, redirectCalled, "handler did not respond with redirect")
})
}

0 comments on commit 5adeab0

Please sign in to comment.