@@ -1,7 +1,10 @@
11package cookie
22
33import (
4+ "fmt"
5+ mathrand "math/rand"
46 "net/http"
7+ "strings"
58 "testing"
69 "time"
710
@@ -49,3 +52,115 @@ func Test_copyCookie(t *testing.T) {
4952 got := copyCookie(c)
5053 assert.Equal(t, c, got)
5154}
55+
56+func Test_splitCookie(t *testing.T) {
57+ testCases := map[string]struct {
58+ Cookie *http.Cookie
59+ ExpectedSizes []int
60+ }{
61+ "Short cookie name": {
62+ Cookie: &http.Cookie{
63+ Name: "short",
64+ Value: strings.Repeat("v", 10000),
65+ },
66+ ExpectedSizes: []int{4089, 4089, 1822},
67+ },
68+ "Long cookie name": {
69+ Cookie: &http.Cookie{
70+ Name: strings.Repeat("n", 251),
71+ Value: strings.Repeat("a", 10000),
72+ },
73+ ExpectedSizes: []int{3843, 3843, 2314},
74+ },
75+ "Max cookie name": {
76+ Cookie: &http.Cookie{
77+ Name: strings.Repeat("n", 256),
78+ Value: strings.Repeat("a", 10000),
79+ },
80+ ExpectedSizes: []int{3840, 3840, 2320},
81+ },
82+ "Suffix overflow cookie name": {
83+ Cookie: &http.Cookie{
84+ Name: strings.Repeat("n", 255),
85+ Value: strings.Repeat("a", 10000),
86+ },
87+ ExpectedSizes: []int{3840, 3840, 2320},
88+ },
89+ "Double digit suffix cookie name results in different sizes": {
90+ Cookie: &http.Cookie{
91+ Name: strings.Repeat("n", 253),
92+ Value: strings.Repeat("a", 50000),
93+ },
94+ ExpectedSizes: []int{3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841,
95+ 3840, 3840, 3840, 70},
96+ },
97+ "Double digit suffix overflow cookie name results in same sizes": {
98+ Cookie: &http.Cookie{
99+ Name: strings.Repeat("n", 254),
100+ Value: strings.Repeat("a", 50000),
101+ },
102+ ExpectedSizes: []int{3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840,
103+ 3840, 3840, 3840, 80},
104+ },
105+ }
106+ for testName, tc := range testCases {
107+ t.Run(testName, func(t *testing.T) {
108+ for i, cookie := range splitCookie(tc.Cookie) {
109+ assert.Equal(t, tc.ExpectedSizes[i], len(cookie.Value))
110+ }
111+ })
112+ }
113+}
114+
115+func Test_splitCookieName(t *testing.T) {
116+ testCases := map[string]struct {
117+ Name string
118+ Count int
119+ Output string
120+ }{
121+ "Standard length": {
122+ Name: "IAmSoNormal",
123+ Count: 2,
124+ Output: "IAmSoNormal_2",
125+ },
126+ "Max length": {
127+ Name: strings.Repeat("n", 256),
128+ Count: 1,
129+ Output: fmt.Sprintf("%s_%d", strings.Repeat("n", 254), 1),
130+ },
131+ "Large count overflow": {
132+ Name: strings.Repeat("n", 253),
133+ Count: 1000,
134+ Output: fmt.Sprintf("%s_%d", strings.Repeat("n", 251), 1000),
135+ },
136+ }
137+ for testName, tc := range testCases {
138+ t.Run(testName, func(t *testing.T) {
139+ splitName := splitCookieName(tc.Name, tc.Count)
140+ assert.Equal(t, tc.Output, splitName)
141+ })
142+ }
143+}
144+
145+func Test_splitCookie_joinCookies(t *testing.T) {
146+ const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
147+
148+ v := make([]byte, 251)
149+ for i := range v {
150+ v[i] = charset[mathrand.Intn(len(charset))]
151+ }
152+ value := strings.Repeat(string(v), 1000)
153+
154+ for _, nameSize := range []int{1, 10, 50, 100, 200, 254} {
155+ t.Run(fmt.Sprintf("%d length cookie name", nameSize), func(t *testing.T) {
156+ cookie := &http.Cookie{
157+ Name: strings.Repeat("n", nameSize),
158+ Value: value,
159+ }
160+ splitCookies := splitCookie(cookie)
161+ joinedCookie, err := joinCookies(splitCookies)
162+ assert.NoError(t, err)
163+ assert.Equal(t, *cookie, *joinedCookie)
164+ })
165+ }
166+}