-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclient_builder_cookie_test.go
85 lines (79 loc) · 2.11 KB
/
client_builder_cookie_test.go
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
package fastshot
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestClientCookieBuilder(t *testing.T) {
tests := []struct {
name string
cookie *http.Cookie
assertFunc func(*testing.T, *ClientBuilder)
}{
{
name: "Add simple cookie",
cookie: &http.Cookie{
Name: "session",
Value: "abc123",
},
assertFunc: func(t *testing.T, cb *ClientBuilder) {
assert.Equal(t, 1, cb.client.Cookies().Count())
cookie := cb.client.Cookies().Get(0)
assert.Equal(t, "session", cookie.Name)
assert.Equal(t, "abc123", cookie.Value)
},
},
{
name: "Add complex cookie",
cookie: &http.Cookie{
Name: "complex",
Value: "value",
Path: "/",
Domain: "example.com",
Expires: time.Now().Add(24 * time.Hour),
MaxAge: 86400,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
},
assertFunc: func(t *testing.T, cb *ClientBuilder) {
assert.Equal(t, 1, cb.client.Cookies().Count())
cookie := cb.client.Cookies().Get(0)
assert.Equal(t, "complex", cookie.Name)
assert.Equal(t, "value", cookie.Value)
assert.Equal(t, "/", cookie.Path)
assert.Equal(t, "example.com", cookie.Domain)
assert.True(t, cookie.Expires.After(time.Now()))
assert.Equal(t, 86400, cookie.MaxAge)
assert.True(t, cookie.Secure)
assert.True(t, cookie.HttpOnly)
assert.Equal(t, http.SameSiteStrictMode, cookie.SameSite)
},
},
{
name: "Add multiple cookies",
cookie: &http.Cookie{
Name: "first",
Value: "value1",
},
assertFunc: func(t *testing.T, cb *ClientBuilder) {
cb.Cookie().Add(&http.Cookie{Name: "second", Value: "value2"})
assert.Equal(t, 2, cb.client.Cookies().Count())
assert.Equal(t, "first", cb.client.Cookies().Get(0).Name)
assert.Equal(t, "second", cb.client.Cookies().Get(1).Name)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Arrange
cb := NewClient("https://example.com")
// Act
result := cb.Cookie().Add(tt.cookie)
// Assert
assert.Equal(t, cb, result)
tt.assertFunc(t, cb)
})
}
}