forked from ivpusic/httpcheck
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tester_cookie.go
45 lines (39 loc) · 1.18 KB
/
tester_cookie.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
package httpcheck
import (
"net/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// cookies ///////////////////////////////////////////////////////
// WithCookie puts cookie on the request.
func (tt *Tester) WithCookie(key, value string) *Tester {
tt.request.AddCookie(&http.Cookie{
Name: key,
Value: value,
})
return tt
}
// HasCookie checks if the response contains cookie with provided key and value.
func (tt *Tester) HasCookie(key, expectedValue string) *Tester {
found := false
for _, cookie := range tt.client.Jar.Cookies(tt.request.URL) {
if cookie.Name == key && cookie.Value == expectedValue {
found = true
break
}
}
assert.True(tt.t, found, "not found, expected key:"+key+", value:"+expectedValue)
return tt
}
// MustHasCookie checks if the response contains cookie with provided key and value.
func (tt *Tester) MustHasCookie(key, expectedValue string) *Tester {
found := false
for _, cookie := range tt.client.Jar.Cookies(tt.request.URL) {
if cookie.Name == key && cookie.Value == expectedValue {
found = true
break
}
}
require.True(tt.t, found, "not found, expected key:"+key+", value:"+expectedValue)
return tt
}