返回 DeepSeek-Reasonix
cookies_test.go
根目录 / desktop / third_party / go-webview2 / pkg / edge / cookies_test.go
1 package edge
2
3 import (
4 "testing"
5 "time"
6 "unsafe"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 "github.com/wailsapp/go-webview2/internal/w32"
11 "golang.org/x/sys/windows"
12 )
13
14 func TestCookieManager(t *testing.T) {
15 // Initialize COM
16 err := windows.CoInitializeEx(0, windows.COINIT_APARTMENTTHREADED)
17 if err != nil {
18 t.Fatalf("Failed to initialize COM: %v", err)
19 }
20 defer windows.CoUninitialize()
21
22 // Create a temporary window for WebView2
23 var hinstance windows.Handle
24 _ = windows.GetModuleHandleEx(0, nil, &hinstance)
25
26 // Load default icon
27 icow, _, _ := w32.User32GetSystemMetrics.Call(w32.SystemMetricsCxIcon)
28 icoh, _, _ := w32.User32GetSystemMetrics.Call(w32.SystemMetricsCyIcon)
29 icon, _, _ := w32.User32LoadImageW.Call(uintptr(hinstance), 32512, icow, icoh, 0)
30
31 className, _ := windows.UTF16PtrFromString("WebView2Test")
32 wc := w32.WndClassExW{
33 CbSize: uint32(unsafe.Sizeof(w32.WndClassExW{})),
34 HInstance: hinstance,
35 LpszClassName: className,
36 HIcon: windows.Handle(icon),
37 HIconSm: windows.Handle(icon),
38 LpfnWndProc: windows.NewCallback(w32.DefWindowProc),
39 }
40 _, _, _ = w32.User32RegisterClassExW.Call(uintptr(unsafe.Pointer(&wc)))
41
42 windowName, _ := windows.UTF16PtrFromString("WebView2 Test Window")
43 hwnd, _, _ := w32.User32CreateWindowExW.Call(
44 0,
45 uintptr(unsafe.Pointer(className)),
46 uintptr(unsafe.Pointer(windowName)),
47 0xCF0000, // WS_OVERLAPPEDWINDOW
48 uintptr(w32.CW_USEDEFAULT),
49 uintptr(w32.CW_USEDEFAULT),
50 640,
51 480,
52 0,
53 0,
54 uintptr(hinstance),
55 0,
56 )
57 if hwnd == 0 {
58 t.Fatal("Failed to create window")
59 }
60 defer w32.DestroyWindow(hwnd)
61
62 _, _, _ = w32.User32ShowWindow.Call(hwnd, w32.SWShow)
63 _, _, _ = w32.User32UpdateWindow.Call(hwnd)
64 _, _, _ = w32.User32SetFocus.Call(hwnd)
65
66 // Create a new Chromium instance
67 chromium := NewChromium()
68 require.NotNil(t, chromium, "Chromium instance should not be nil")
69
70 // Initialize WebView2
71 success := chromium.Embed(uintptr(hwnd))
72 require.True(t, success, "WebView2 initialization should succeed")
73
74 // Get the cookie manager
75 cookieManager, err := chromium.GetCookieManager()
76 require.NoError(t, err, "Should get cookie manager without error")
77 require.NotNil(t, cookieManager, "Cookie manager should not be nil")
78 defer cookieManager.Release()
79
80 // Delete all cookies to start with a clean slate
81 err = cookieManager.DeleteAllCookies()
82 require.NoError(t, err, "Should delete all cookies without error")
83
84 t.Run("Test Cookie Creation and Properties", func(t *testing.T) {
85 // Create a new cookie
86 cookie, err := cookieManager.CreateCookie("testCookie", "testValue", "example.com", "/test")
87 require.NoError(t, err, "Should create cookie without error")
88 require.NotNil(t, cookie, "Cookie should not be nil")
89 defer cookie.Release()
90
91 // Test GetName
92 name, err := cookie.GetName()
93 assert.NoError(t, err, "Should get name without error")
94 assert.Equal(t, "testCookie", name, "Cookie name should match")
95
96 // Test GetValue/PutValue
97 value, err := cookie.GetValue()
98 assert.NoError(t, err, "Should get value without error")
99 assert.Equal(t, "testValue", value, "Cookie value should match")
100
101 err = cookie.PutValue("newValue")
102 assert.NoError(t, err, "Should put value without error")
103 value, err = cookie.GetValue()
104 assert.NoError(t, err, "Should get updated value without error")
105 assert.Equal(t, "newValue", value, "Cookie value should be updated")
106
107 // Test GetDomain
108 domain, err := cookie.GetDomain()
109 assert.NoError(t, err, "Should get domain without error")
110 assert.Equal(t, "example.com", domain, "Cookie domain should match")
111
112 // Test GetPath
113 path, err := cookie.GetPath()
114 assert.NoError(t, err, "Should get path without error")
115 assert.Equal(t, "/test", path, "Cookie path should match")
116
117 // Test Expires
118 now := time.Now().Add(24 * time.Hour) // 24 hours from now
119 comTime := float64(now.Unix())
120 err = cookie.PutExpires(comTime)
121 assert.NoError(t, err, "Should set expiration without error")
122 expires, err := cookie.GetExpires()
123 assert.NoError(t, err, "Should get expiration without error")
124 assert.Equal(t, comTime, expires, "Cookie expiration should match")
125
126 // Test IsHttpOnly
127 err = cookie.PutIsHttpOnly(true)
128 assert.NoError(t, err, "Should set HttpOnly without error")
129 isHttpOnly, err := cookie.GetIsHttpOnly()
130 assert.NoError(t, err, "Should get HttpOnly without error")
131 assert.True(t, isHttpOnly, "Cookie should be HttpOnly")
132
133 // Test IsSecure
134 err = cookie.PutIsSecure(true)
135 assert.NoError(t, err, "Should set Secure without error")
136 isSecure, err := cookie.GetIsSecure()
137 assert.NoError(t, err, "Should get Secure without error")
138 assert.True(t, isSecure, "Cookie should be Secure")
139
140 // Test SameSite
141 err = cookie.PutSameSite(2) // 2 = Lax
142 assert.NoError(t, err, "Should set SameSite without error")
143 sameSite, err := cookie.GetSameSite()
144 assert.NoError(t, err, "Should get SameSite without error")
145 assert.Equal(t, int32(2), sameSite, "Cookie SameSite should be Lax")
146 })
147
148 t.Run("Test Cookie Management", func(t *testing.T) {
149 // Create and add a cookie
150 cookie, err := cookieManager.CreateCookie("managedCookie", "testValue", "example.com", "/test")
151 require.NoError(t, err, "Should create cookie without error")
152 defer cookie.Release()
153
154 err = cookieManager.AddOrUpdateCookie(cookie)
155 assert.NoError(t, err, "Should add cookie without error")
156
157 // Delete the cookie
158 err = cookieManager.DeleteCookie(cookie)
159 assert.NoError(t, err, "Should delete cookie without error")
160
161 // Delete all cookies
162 err = cookieManager.DeleteAllCookies()
163 assert.NoError(t, err, "Should delete all cookies without error")
164 })
165 }
166
166 lines GO