forked from bogdanfinn/tls-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjar_test.go
More file actions
70 lines (48 loc) · 2.6 KB
/
jar_test.go
File metadata and controls
70 lines (48 loc) · 2.6 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
package httpkit
import (
"net/url"
"testing"
http "github.com/bogdanfinn/fhttp"
"github.com/stretchr/testify/assert"
)
const (
Alive = 1
Expired = -1
)
var urlObject = &url.URL{Scheme: "http", Host: "test.com", Path: "/test"}
func TestCookieJar_GivenEmptyJar_WhenSetCookies_ThenCookiesAreAdded(t *testing.T) {
jar := NewCookieJar()
jar.SetCookies(urlObject, []*http.Cookie{{Name: "1", Value: "first", MaxAge: Alive}})
assert.Equal(t, 1, len(jar.Cookies(urlObject)), "Expected cookie to be added")
assert.Equal(t, "first", jar.Cookie(urlObject, "1").Value, "Expected value to be set")
}
func TestCookieJar_GivenEmptyJar_WhenSetDuplicateCookies_ThenLastCookiesAreAdded(t *testing.T) {
jar := NewCookieJar()
jar.SetCookies(urlObject, []*http.Cookie{{Name: "1", Value: "first", MaxAge: Alive}})
jar.SetCookies(urlObject, []*http.Cookie{{Name: "1", Value: "second", MaxAge: Alive}})
assert.Equal(t, 1, len(jar.Cookies(urlObject)), "Only one cookie should be present after unique filtering")
assert.Equal(t, "second", jar.Cookie(urlObject, "1").Value, "Expected value to be set to the last one")
}
func TestCookieJar_GivenExpiredCookie_WhenSetCookies_ThenExpiredCookieIsExcluded(t *testing.T) {
jar := NewCookieJar()
jar.SetCookies(urlObject, []*http.Cookie{{Name: "1", Value: "first", MaxAge: Expired}})
assert.Equal(t, 0, len(jar.Cookies(urlObject)), "Expected expired cookie to be excluded")
}
func TestCookieJar_GivenSkipExisting_WhenSetExistingCookies_ThenExistingCookiesAreSkipped(t *testing.T) {
jar := NewCookieJar(WithSkipExisting())
jar.SetCookies(urlObject, []*http.Cookie{{Name: "1", Value: "first", MaxAge: Alive}})
jar.SetCookies(urlObject, []*http.Cookie{{Name: "1", Value: "second", MaxAge: Alive}})
assert.Equal(t, 1, len(jar.Cookies(urlObject)), "Only one cookie should be present after skipping existing")
assert.Equal(t, "first", jar.Cookie(urlObject, "1").Value, "Expected existing value to be kept")
}
func TestCookieJar_GivenAllowEmpty_WhenSetEmptyCookies_ThenEmptyCookiesAreAllowed(t *testing.T) {
jar := NewCookieJar(WithAllowEmpty())
jar.SetCookies(urlObject, []*http.Cookie{{Name: "1", Value: "", MaxAge: Alive}})
assert.Equal(t, 1, len(jar.Cookies(urlObject)), "Expected empty cookie to be accepted")
}
func TestCookieJar_GivenAliveCookie_WhenSetExpiredCookies_ThenOldCookiesIsExcluded(t *testing.T) {
jar := NewCookieJar()
jar.SetCookies(urlObject, []*http.Cookie{{Name: "1", Value: "first", MaxAge: Alive}})
jar.SetCookies(urlObject, []*http.Cookie{{Name: "1", Value: "first", MaxAge: Expired}})
assert.Equal(t, 0, len(jar.Cookies(urlObject)), "Expected expired cookie to be excluded")
}