-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver_mw_test.go
More file actions
60 lines (46 loc) · 1.13 KB
/
server_mw_test.go
File metadata and controls
60 lines (46 loc) · 1.13 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
package gserv
import (
"bytes"
"net/http"
"net/http/cookiejar"
"testing"
"time"
"github.com/gorilla/securecookie"
)
func TestSecureCookie(t *testing.T) {
srv := newServerAndWait(t, "")
defer srv.Shutdown(0)
srv.Use(SecureCookie(bytes.Repeat([]byte("1"), 32), securecookie.GenerateRandomKey(32)))
JSONGet(srv, "/", func(ctx *Context) (any, error) {
ctx.SetCookie("cooookie", M{"stuff": "and things"}, "", false, time.Hour)
return nil, nil
}, true)
JSONGet(srv, "/cookie", func(ctx *Context) (M, error) {
var m M
ctx.GetCookieValue("cooookie", &m)
return m, nil
}, true)
addr := srv.Addrs()[0]
var cli http.Client
cli.Jar, _ = cookiejar.New(nil)
resp, err := cli.Get("http://" + addr + "/")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
cs := resp.Cookies()
if len(cs) != 1 {
t.Fatal("couldn't find the cookie :(")
}
resp, err = cli.Get("http://" + addr + "/cookie")
if err != nil {
t.Fatal(err)
}
var respValue M
if _, err = ReadJSONResponse(resp.Body, &respValue); err != nil {
t.Fatal(err)
}
if respValue["stuff"] != "and things" {
t.Fatalf("unexpected response: %#+v", respValue)
}
}