-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractcookie_test.go
220 lines (194 loc) · 5.93 KB
/
extractcookie_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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package extractcookie_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"github.com/v-electrolux/extractcookie"
)
var (
maxCookie = http.Cookie{
Name: "test_cookie_with_auth_token",
Value: "pretend_to_be_auth_token",
Path: "/path/to",
Domain: "google.com",
Expires: time.Now(),
MaxAge: 5,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteNoneMode,
}
minCookie = http.Cookie{
Name: "test_cookie_with_auth_token",
Value: "pretend_to_be_auth_token",
}
minCookieExpired = http.Cookie{
Name: "test_cookie_with_auth_token",
Value: "pretend_to_be_auth_token",
MaxAge: 0,
}
maxCookieGarbage = http.Cookie{
Name: "some_cookie_garbage",
Value: "bla bla foo bar",
Path: "/path/to",
Domain: "google.com",
Expires: time.Now(),
MaxAge: 5,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteNoneMode,
}
minCookieGarbage = http.Cookie{
Name: "some_cookie_garbage",
Value: "bla bla foo bar",
}
minCookieExpiredGarbage = http.Cookie{
Name: "some_cookie_garbage",
Value: "bla bla foo bar",
MaxAge: 0,
}
)
type TestHttpResData struct {
cfgCookieName string
cfgHeaderNameForCookieValue string
cfgCookieValuePrefix string
cookies []http.Cookie
expectedHeaderValue string
}
func TestEmptyCookie(t *testing.T) {
data := TestHttpResData{
cfgCookieName: "test_cookie_with_auth_token",
cfgHeaderNameForCookieValue: "Authorization",
cfgCookieValuePrefix: "Bearer ",
}
t.Run("empty cookie", func(t *testing.T) {
data.cookies = []http.Cookie{}
data.expectedHeaderValue = ""
testHttpRequest(t, data)
})
}
func TestOneCookie(t *testing.T) {
data := TestHttpResData{
cfgCookieName: "test_cookie_with_auth_token",
cfgHeaderNameForCookieValue: "Authorization",
cfgCookieValuePrefix: "Bearer ",
}
t.Run("one maxCookie", func(t *testing.T) {
data.cookies = []http.Cookie{maxCookie}
data.expectedHeaderValue = "Bearer pretend_to_be_auth_token"
testHttpRequest(t, data)
})
t.Run("one minCookie", func(t *testing.T) {
data.cookies = []http.Cookie{minCookie}
data.expectedHeaderValue = "Bearer pretend_to_be_auth_token"
testHttpRequest(t, data)
})
t.Run("one minCookieExpired", func(t *testing.T) {
data.cookies = []http.Cookie{minCookieExpired}
data.expectedHeaderValue = "Bearer pretend_to_be_auth_token"
testHttpRequest(t, data)
})
t.Run("one maxCookieGarbage", func(t *testing.T) {
data.cookies = []http.Cookie{maxCookieGarbage}
data.expectedHeaderValue = ""
testHttpRequest(t, data)
})
t.Run("one minCookieGarbage", func(t *testing.T) {
data.cookies = []http.Cookie{minCookieGarbage}
data.expectedHeaderValue = ""
testHttpRequest(t, data)
})
t.Run("one minCookieExpiredGarbage", func(t *testing.T) {
data.cookies = []http.Cookie{minCookieExpiredGarbage}
data.expectedHeaderValue = ""
testHttpRequest(t, data)
})
}
func TestTwoCookie(t *testing.T) {
data := TestHttpResData{
cfgCookieName: "test_cookie_with_auth_token",
cfgHeaderNameForCookieValue: "Authorization",
cfgCookieValuePrefix: "Bearer ",
}
t.Run("minCookieGarbage and maxCookieGarbage", func(t *testing.T) {
data.cookies = []http.Cookie{minCookieGarbage, maxCookieGarbage}
data.expectedHeaderValue = ""
testHttpRequest(t, data)
})
t.Run("minCookieExpiredGarbage and maxCookieGarbage", func(t *testing.T) {
data.cookies = []http.Cookie{minCookieExpiredGarbage, maxCookieGarbage}
data.expectedHeaderValue = ""
testHttpRequest(t, data)
})
t.Run("minCookie and maxCookieGarbage", func(t *testing.T) {
data.cookies = []http.Cookie{minCookie, maxCookieGarbage}
data.expectedHeaderValue = "Bearer pretend_to_be_auth_token"
testHttpRequest(t, data)
})
t.Run("minCookieGarbage and maxCookie", func(t *testing.T) {
data.cookies = []http.Cookie{minCookieGarbage, maxCookie}
data.expectedHeaderValue = "Bearer pretend_to_be_auth_token"
testHttpRequest(t, data)
})
t.Run("maxCookieGarbage and minCookie", func(t *testing.T) {
data.cookies = []http.Cookie{maxCookieGarbage, minCookie}
data.expectedHeaderValue = "Bearer pretend_to_be_auth_token"
testHttpRequest(t, data)
})
}
func testHttpRequest(t *testing.T, data TestHttpResData) {
t.Helper()
cfg := extractcookie.CreateConfig()
cfg.CookieName = data.cfgCookieName
cfg.HeaderNameForCookieValue = data.cfgHeaderNameForCookieValue
cfg.CookieValuePrefix = data.cfgCookieValuePrefix
cfg.LogLevel = "warn"
backendStatusCode := http.StatusOK
backendBody := []byte("foo bar")
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(backendStatusCode)
rw.Write(backendBody)
})
handler, err := extractcookie.New(ctx, next, cfg, "tlsclientcertforward")
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
for _, cookie := range data.cookies {
req.AddCookie(&cookie)
}
if err != nil {
t.Fatal(err)
}
handler.ServeHTTP(recorder, req)
resp := recorder.Result()
assertStatusCode(t, resp, backendStatusCode)
assertBody(t, resp, backendBody)
assertHeader(t, req, data.cfgHeaderNameForCookieValue, data.expectedHeaderValue)
}
func assertStatusCode(t *testing.T, res *http.Response, expected int) {
t.Helper()
got := res.StatusCode
if got != expected {
t.Errorf("expected status code value: `%d`, got value: `%d`", expected, got)
}
}
func assertBody(t *testing.T, res *http.Response, expected []byte) {
t.Helper()
got, _ := io.ReadAll(res.Body)
if !reflect.DeepEqual(got, expected) {
t.Errorf("expected body value: `%s`, got value: `%s`", expected, got)
}
}
func assertHeader(t *testing.T, req *http.Request, key string, expected string) {
t.Helper()
got := req.Header.Get(key)
if got != expected {
t.Errorf("expected header %s value: `%s`, got value: `%s`", key, expected, got)
}
}