forked from ivpusic/httpcheck
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtester_cookie_test.go
106 lines (94 loc) · 2.28 KB
/
tester_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package httpcheck
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTester_WithCookie(t *testing.T) {
checker := newTestChecker()
checker.Test(t, http.MethodGet, "/some").
WithCookie("key", "value")
cookie, err := checker.request.Cookie("key")
require.NoError(t, err)
assert.Equal(t, cookie.Value, "value")
_, err = checker.request.Cookie("unknown")
assert.Error(t, err)
}
func TestTester_Cookies(t *testing.T) {
mockT := new(testing.T)
checker := newTestChecker()
checker.PersistCookie("some")
checker.Test(mockT, "GET", "/cookies").
Check().
HasCookie("some", "cookie").
HasCookie("other", "secondcookie")
assert.False(t, mockT.Failed())
result := checker.Test(mockT, "GET", "/nothing").
Check().
HasCookie("some", "cookie")
assert.False(t, mockT.Failed())
result.UnpersistCookie("some")
result = checker.Test(mockT, "GET", "/nothing").
Check().
HasCookie("some", "cookie")
assert.True(t, mockT.Failed())
result.HasCookie("other", "secondcookie")
assert.True(t, mockT.Failed())
}
func TestTester_HasCookie(t *testing.T) {
type pair struct {
key string
value string
}
testdata := []struct {
name string
expected pair
want bool
}{
{
name: "OK: response has some cookie.",
expected: pair{
key: "some",
value: "cookie",
},
want: false,
},
{
name: "NG: response does not have the specified cookie value.",
expected: pair{
key: "some",
value: "unknown",
},
want: true,
},
{
name: "NG: response does not have the specified cookie key.",
expected: pair{
key: "unknown",
value: "cookie",
},
want: true,
},
}
for _, tt := range testdata {
t.Run(tt.name, func(t *testing.T) {
mockT := new(testing.T)
checker := newTestChecker()
checker.Test(mockT, "GET", "/some").
Check().
HasCookie(tt.expected.key, tt.expected.value)
assert.Equal(t, tt.want, mockT.Failed())
})
}
}
func TestTester_MustHasCookie(t *testing.T) {
t.Skip("skip this test because it expects failure.")
checker := newTestChecker()
checker.Test(t, "GET", "/some").
Check().
MustHasCookie("some", "unknown").
Cb(func(response *http.Response) {
t.Fatal("it is expected that this assertion will not be executed.")
})
}