Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 7m57s
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
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())
|
|
}
|
|
}
|