-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcookie_test.go
More file actions
92 lines (82 loc) · 2.01 KB
/
Copy pathcookie_test.go
File metadata and controls
92 lines (82 loc) · 2.01 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
90
91
92
// Copyright © 2023 Brett Vickers.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nts
import (
"bytes"
"testing"
)
func TestCookieJar_Add(t *testing.T) {
jar := cookieJar{}
cookies := make([][]byte, 10)
for i := 0; i < 10; i++ {
cookies[i] = []byte{byte(i)}
jar.Add(cookies[i])
if i < cookieJarSize {
if jar.Count() != i+1 {
t.Errorf("expected count %d, got %d", i+1, jar.Count())
}
} else {
if jar.Count() != cookieJarSize {
t.Errorf("expected count %d, got %d", cookieJarSize, jar.Count())
}
}
}
// Check that the jar contains the last 8 cookies added
for i := 0; i < cookieJarSize; i++ {
actual := jar.Consume()
expected := cookies[i+2]
if !bytes.Equal(expected, actual) {
t.Errorf("cookie mismatch: expected %v, got %v", expected, actual)
}
}
}
func TestCookieJar_Clear(t *testing.T) {
jar := cookieJar{}
for i := 0; i < cookieJarSize; i++ {
jar.Add([]byte{byte(i)})
}
jar.Clear()
if jar.Count() != 0 {
t.Errorf("expected count 0, got %d", jar.Count())
}
if jar.head != 0 || jar.tail != 0 {
t.Errorf("head and tail should be 0 after clear")
}
}
func TestCookieJar_Consume(t *testing.T) {
jar := cookieJar{}
for i := 0; i < cookieJarSize; i++ {
jar.Add([]byte{byte(i)})
}
for i := 0; i < cookieJarSize; i++ {
c := jar.Consume()
if c == nil {
t.Errorf("unexpected nil cookie")
}
if c[0] != byte(i) {
t.Errorf("unexpected cookie value: %d", c[0])
}
}
if jar.Consume() != nil {
t.Errorf("expected nil cookie from empty jar")
}
}
func TestCookieJar_Count(t *testing.T) {
jar := cookieJar{}
if jar.Count() != 0 {
t.Errorf("initial count should be zero")
}
for i := 0; i < cookieJarSize; i++ {
jar.Add([]byte{byte(i)})
if jar.Count() != i+1 {
t.Errorf("expected count %d, got %d", i+1, jar.Count())
}
}
for i := 0; i < cookieJarSize; i++ {
jar.Consume()
if jar.Count() != cookieJarSize-i-1 {
t.Errorf("expected count %d, got %d", cookieJarSize-i-1, jar.Count())
}
}
}