package tests import ( "fmt" "shudao-chat-go/utils" "testing" "github.com/beego/beego/v2/server/web/context" ) // TestContextDataStorage 测试context数据存储和获取 func TestContextDataStorage(t *testing.T) { // 创建一个模拟的 BeegoInput ctx := &context.Context{ Input: context.NewInput(), } // 创建测试用户信息 testUserInfo := &utils.TokenUserInfo{ AccountID: "test123", ID: 12345, Name: "测试用户", UserCode: "T001", ContactNumber: "13800138000", TokenType: "access", Exp: 1234567890, Iat: 1234567800, } fmt.Println("\n========== Context存储测试 ==========") fmt.Printf("原始用户信息: %+v\n", testUserInfo) fmt.Printf("原始指针地址: %p\n\n", testUserInfo) // 存储到context ctx.Input.SetData("userInfo", testUserInfo) fmt.Println("✅ 已存储到context") // 立即从context获取 storedData := ctx.Input.GetData("userInfo") fmt.Printf("\n从context获取的数据:\n") fmt.Printf(" 类型: %T\n", storedData) fmt.Printf(" 值: %+v\n\n", storedData) // 使用 GetUserInfoFromContext 函数解析 userInfo, err := utils.GetUserInfoFromContext(storedData) if err != nil { t.Errorf("❌ GetUserInfoFromContext 失败: %v", err) return } fmt.Println("\n✅ 测试通过!成功解析用户信息:") fmt.Printf(" - AccountID: %s\n", userInfo.AccountID) fmt.Printf(" - ID: %d\n", userInfo.ID) fmt.Printf(" - Name: %s\n", userInfo.Name) fmt.Println("=====================================") }