This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 577
/
cookies_test.go
78 lines (55 loc) · 1.61 KB
/
cookies_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
package buffalo
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestCookies_Get(t *testing.T) {
r := require.New(t)
req := httptest.NewRequest("POST", "/", nil)
req.Header.Set("Cookie", "name=Arthur Dent; answer=42")
c := Cookies{req, nil}
v, err := c.Get("name")
r.NoError(err)
r.Equal("Arthur Dent", v)
v, err = c.Get("answer")
r.NoError(err)
r.Equal("42", v)
_, err = c.Get("unknown")
r.EqualError(err, http.ErrNoCookie.Error())
}
func TestCookies_Set(t *testing.T) {
r := require.New(t)
res := httptest.NewRecorder()
c := Cookies{&http.Request{}, res}
c.Set("name", "Rob Pike", time.Hour*24)
h := res.Header().Get("Set-Cookie")
r.Equal("name=\"Rob Pike\"; Max-Age=86400", h)
}
func TestCookies_SetWithPath(t *testing.T) {
r := require.New(t)
res := httptest.NewRecorder()
c := Cookies{&http.Request{}, res}
c.SetWithPath("name", "Rob Pike", "/foo")
h := res.Header().Get("Set-Cookie")
r.Equal("name=\"Rob Pike\"; Path=/foo", h)
}
func TestCookies_SetWithExpirationTime(t *testing.T) {
r := require.New(t)
res := httptest.NewRecorder()
c := Cookies{&http.Request{}, res}
e := time.Date(2017, 7, 29, 19, 28, 45, 0, time.UTC)
c.SetWithExpirationTime("name", "Rob Pike", e)
h := res.Header().Get("Set-Cookie")
r.Equal("name=\"Rob Pike\"; Expires=Sat, 29 Jul 2017 19:28:45 GMT", h)
}
func TestCookies_Delete(t *testing.T) {
r := require.New(t)
res := httptest.NewRecorder()
c := Cookies{&http.Request{}, res}
c.Delete("remove-me")
h := res.Header().Get("Set-Cookie")
r.Equal("remove-me=v; Expires=Thu, 01 Jan 1970 00:00:00 GMT", h)
}