fix gitea workflow path and runner label
Build docker and publish / build (20.15.1) (push) Failing after 7m57s
Build docker and publish / build (20.15.1) (push) Failing after 7m57s
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/perfect-panel/server/pkg/constant"
|
||||
)
|
||||
|
||||
func ApiVersionSwitchHandler(legacyHandler gin.HandlerFunc, latestHandler gin.HandlerFunc) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
useLatest, _ := c.Request.Context().Value(constant.CtxKeyAPIVersionUseLatest).(bool)
|
||||
if useLatest && latestHandler != nil {
|
||||
latestHandler(c)
|
||||
return
|
||||
}
|
||||
if legacyHandler != nil {
|
||||
legacyHandler(c)
|
||||
return
|
||||
}
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/perfect-panel/server/pkg/constant"
|
||||
)
|
||||
|
||||
func TestApiVersionSwitchHandlerUsesLegacyByDefault(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.GET("/test", ApiVersionSwitchHandler(
|
||||
func(c *gin.Context) { c.String(http.StatusOK, "legacy") },
|
||||
func(c *gin.Context) { c.String(http.StatusOK, "latest") },
|
||||
))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||
resp := httptest.NewRecorder()
|
||||
r.ServeHTTP(resp, req)
|
||||
|
||||
if resp.Code != http.StatusOK || resp.Body.String() != "legacy" {
|
||||
t.Fatalf("expected legacy handler, code=%d body=%s", resp.Code, resp.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiVersionSwitchHandlerUsesLatestWhenFlagSet(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(func(c *gin.Context) {
|
||||
ctx := context.WithValue(c.Request.Context(), constant.CtxKeyAPIVersionUseLatest, true)
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Next()
|
||||
})
|
||||
r.GET("/test", ApiVersionSwitchHandler(
|
||||
func(c *gin.Context) { c.String(http.StatusOK, "legacy") },
|
||||
func(c *gin.Context) { c.String(http.StatusOK, "latest") },
|
||||
))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||
resp := httptest.NewRecorder()
|
||||
r.ServeHTTP(resp, req)
|
||||
|
||||
if resp.Code != http.StatusOK || resp.Body.String() != "latest" {
|
||||
t.Fatalf("expected latest handler, code=%d body=%s", resp.Code, resp.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user