-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_cookie_test.go
More file actions
96 lines (81 loc) · 2.71 KB
/
Copy pathrequest_cookie_test.go
File metadata and controls
96 lines (81 loc) · 2.71 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
93
94
95
96
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package httpserver
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"testing"
)
// ============ cookie tag tests ============
type cookieSingleStruct struct {
Session string `cookie:"session"`
}
func TestCookieTag_SingleField(t *testing.T) {
captured, rec := doRequest[cookieSingleStruct](t, captureHandler[cookieSingleStruct],
http.MethodGet, "/", withCookie("session", "abc123"))
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "abc123", captured.request.Session, "Session")
}
func TestCookieTag_MissingCookie_ZeroValue(t *testing.T) {
captured, rec := doRequest[cookieSingleStruct](t, captureHandler[cookieSingleStruct], http.MethodGet, "/")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "", captured.request.Session, "Session")
}
type cookieAllStruct struct {
Cookies KeyValues `cookie:""`
}
func TestCookieTag_EmptyTag_BindsAllCookies(t *testing.T) {
captured, rec := doRequest[cookieAllStruct](t, captureHandler[cookieAllStruct],
http.MethodGet, "/",
withCookie("session", "abc"),
withCookie("csrf", "xyz"),
)
assert.Equal(t, http.StatusOK, rec.Code)
if captured.request.Cookies == nil {
t.Fatal("Cookies field is nil")
}
assert.Equal(t, []string{"abc"}, captured.request.Cookies["session"], "Cookies[session]")
assert.Equal(t, []string{"xyz"}, captured.request.Cookies["csrf"], "Cookies[csrf]")
}
type cookieAllWrongTypeStruct struct {
Cookies string `cookie:""`
}
func TestCookieTag_EmptyTag_WrongType_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[cookieAllWrongTypeStruct])
})
}
type cookieMultipleEmptyStruct struct {
C1 KeyValues `cookie:""`
C2 KeyValues `cookie:""`
}
func TestCookieTag_MultipleEmptyTags_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[cookieMultipleEmptyStruct])
})
}
type cookieNonEmptyAfterEmptyStruct struct {
All KeyValues `cookie:""`
Single string `cookie:"session"`
}
func TestCookieTag_NonEmptyAfterEmpty_Panics(t *testing.T) {
require.Panics(t, func() {
_ = RequestParser(captureHandler[cookieNonEmptyAfterEmptyStruct])
})
}
func TestCookieTag_MultipleCookiesSameName_BindsAllValues(t *testing.T) {
type Req struct {
Values []string `cookie:"pref"`
}
captured, rec := doRequest[Req](t, captureHandler[Req],
http.MethodGet, "/",
withCookie("pref", "dark"),
withCookie("pref", "compact"),
)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, []string{"dark", "compact"}, captured.request.Values, "Values")
}