初始化
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
// 管理端路由测试,验证通用资源接口仅放行明确支持的资源类型。
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// TestAllowAdminResource 验证模型资源可进入处理器,未知资源仍返回接口不存在。
|
||||
func TestAllowAdminResource(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
tests := []struct {
|
||||
name string
|
||||
resource string
|
||||
statusCode int
|
||||
}{
|
||||
{name: "允许模型资源", resource: "models", statusCode: http.StatusNoContent},
|
||||
{name: "拒绝未知资源", resource: "unknown", statusCode: http.StatusNotFound},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
router := gin.New()
|
||||
route := router.Group("/resources/:resource")
|
||||
route.Use(allowAdminResource("styles", "channels", "models"))
|
||||
route.PATCH("/:id/enabled", func(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
})
|
||||
|
||||
response := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodPatch, "/resources/"+test.resource+"/model-id/enabled", nil)
|
||||
router.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != test.statusCode {
|
||||
t.Fatalf("unexpected status: got %d, want %d", response.Code, test.statusCode)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user