forked from steinfletcher/apitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.go
144 lines (113 loc) · 3.53 KB
/
cookies.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package apitest
import (
"fmt"
"net/http"
"time"
)
type Cookie struct {
name *string
value *string
path *string
domain *string
expires *time.Time
maxAge *int
secure *bool
httpOnly *bool
}
func NewCookie(name string) *Cookie {
return &Cookie{
name: &name,
}
}
func (cookie *Cookie) Value(value string) *Cookie {
cookie.value = &value
return cookie
}
func (cookie *Cookie) Path(path string) *Cookie {
cookie.path = &path
return cookie
}
func (cookie *Cookie) Domain(domain string) *Cookie {
cookie.domain = &domain
return cookie
}
func (cookie *Cookie) Expires(expires time.Time) *Cookie {
cookie.expires = &expires
return cookie
}
func (cookie *Cookie) MaxAge(maxAge int) *Cookie {
cookie.maxAge = &maxAge
return cookie
}
func (cookie *Cookie) Secure(secure bool) *Cookie {
cookie.secure = &secure
return cookie
}
func (cookie *Cookie) HttpOnly(httpOnly bool) *Cookie {
cookie.httpOnly = &httpOnly
return cookie
}
func (cookie *Cookie) ToHttpCookie() *http.Cookie {
httpCookie := http.Cookie{}
if cookie.name != nil {
httpCookie.Name = *cookie.name
}
if cookie.value != nil {
httpCookie.Value = *cookie.value
}
if cookie.path != nil {
httpCookie.Path = *cookie.path
}
if cookie.domain != nil {
httpCookie.Domain = *cookie.domain
}
if cookie.expires != nil {
httpCookie.Expires = *cookie.expires
}
if cookie.maxAge != nil {
httpCookie.MaxAge = *cookie.maxAge
}
if cookie.secure != nil {
httpCookie.Secure = *cookie.secure
}
if cookie.httpOnly != nil {
httpCookie.HttpOnly = *cookie.httpOnly
}
return &httpCookie
}
// Compares cookies based on only the provided fields from Cookie.
// Supported fields are Name, Value, Domain, Path, Expires, MaxAge, Secure and HttpOnly
func compareCookies(expectedCookie *Cookie, actualCookie *http.Cookie) (bool, []string) {
cookieFound := *expectedCookie.name == actualCookie.Name
compareErrors := []string{}
if cookieFound {
formatError := func(name string, expectedValue, actualValue interface{}) string {
return fmt.Sprintf("Mismatched field %s. Expected %v but received %v",
name,
expectedValue,
actualValue)
}
if expectedCookie.value != nil && *expectedCookie.value != actualCookie.Value {
compareErrors = append(compareErrors, formatError("Value", *expectedCookie.value, actualCookie.Value))
}
if expectedCookie.domain != nil && *expectedCookie.domain != actualCookie.Domain {
compareErrors = append(compareErrors, formatError("Domain", *expectedCookie.domain, actualCookie.Domain))
}
if expectedCookie.path != nil && *expectedCookie.path != actualCookie.Path {
compareErrors = append(compareErrors, formatError("Path", *expectedCookie.path, actualCookie.Path))
}
if expectedCookie.expires != nil && !(*expectedCookie.expires).Equal(actualCookie.Expires) {
compareErrors = append(compareErrors, formatError("Expires", *expectedCookie.expires, actualCookie.Expires))
}
if expectedCookie.maxAge != nil && *expectedCookie.maxAge != actualCookie.MaxAge {
compareErrors = append(compareErrors, formatError("MaxAge", *expectedCookie.maxAge, actualCookie.MaxAge))
}
if expectedCookie.secure != nil && *expectedCookie.secure != actualCookie.Secure {
compareErrors = append(compareErrors, formatError("Secure", *expectedCookie.secure, actualCookie.Secure))
}
if expectedCookie.httpOnly != nil && *expectedCookie.httpOnly != actualCookie.HttpOnly {
compareErrors = append(compareErrors, formatError("HttpOnly", *expectedCookie.httpOnly, actualCookie.HttpOnly))
}
}
return cookieFound, compareErrors
}