forked from webview/webview_go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie_internal_test.go
More file actions
89 lines (82 loc) · 2.72 KB
/
cookie_internal_test.go
File metadata and controls
89 lines (82 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package webview
import (
"reflect"
"testing"
"time"
)
func TestDefaultCookiePath(t *testing.T) {
if got := defaultCookiePath(""); got != "/" {
t.Fatalf("defaultCookiePath(\"\") = %q, want /", got)
}
if got := defaultCookiePath("/app"); got != "/app" {
t.Fatalf("defaultCookiePath(\"/app\") = %q, want /app", got)
}
}
func TestPrepareCookieForNative(t *testing.T) {
expires := time.Unix(1710000000, 0)
prepared, err := prepareCookieForNative(Cookie{
Name: "sid",
Value: "abc",
Domain: ".example.com",
Secure: true,
HTTPOnly: true,
Expires: expires,
})
if err != nil {
t.Fatalf("prepareCookieForNative returned error: %v", err)
}
if prepared.Path != "/" {
t.Fatalf("prepared.Path = %q, want /", prepared.Path)
}
if !prepared.HasExpires {
t.Fatal("prepared.HasExpires = false, want true")
}
if prepared.ExpiresUnix != float64(expires.Unix()) {
t.Fatalf("prepared.ExpiresUnix = %v, want %v", prepared.ExpiresUnix, float64(expires.Unix()))
}
}
func TestPrepareCookieForNativeValidation(t *testing.T) {
if _, err := prepareCookieForNative(Cookie{Domain: ".example.com"}); err == nil {
t.Fatal("expected missing-name error")
}
if _, err := prepareCookieForNative(Cookie{Name: "sid"}); err == nil {
t.Fatal("expected missing-domain error")
}
}
func TestPrepareCookieIdentity(t *testing.T) {
identity, err := prepareCookieIdentity("sid", ".example.com", "")
if err != nil {
t.Fatalf("prepareCookieIdentity returned error: %v", err)
}
want := cookieIdentity{Name: "sid", Domain: ".example.com", Path: "/"}
if !reflect.DeepEqual(identity, want) {
t.Fatalf("prepareCookieIdentity = %+v, want %+v", identity, want)
}
}
func TestPrepareCookieIdentityValidation(t *testing.T) {
if _, err := prepareCookieIdentity("", ".example.com", "/"); err == nil {
t.Fatal("expected missing-name error")
}
if _, err := prepareCookieIdentity("sid", "", "/"); err == nil {
t.Fatal("expected missing-domain error")
}
}
func TestDecodeCookiesJSON(t *testing.T) {
raw := `[{"name":"sid","value":"abc","domain":".example.com","path":"/","expires_unix":1710000000,"secure":true,"http_only":true},{"name":"theme","value":"dark","domain":"example.com","path":"/prefs","secure":false,"http_only":false}]`
got, err := decodeCookiesJSON(raw)
if err != nil {
t.Fatalf("decodeCookiesJSON returned error: %v", err)
}
if len(got) != 2 {
t.Fatalf("len(got) = %d, want 2", len(got))
}
if got[0].Name != "sid" || !got[0].Secure || !got[0].HTTPOnly {
t.Fatalf("unexpected first cookie: %+v", got[0])
}
if got[0].Expires.Unix() != 1710000000 {
t.Fatalf("got[0].Expires = %v, want unix 1710000000", got[0].Expires.Unix())
}
if !got[1].Expires.IsZero() {
t.Fatalf("got[1].Expires = %v, want zero time", got[1].Expires)
}
}