MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestCookies

Function TestCookies

pkg/webmiddleware/cookie_test.go:28–78  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

26)
27
28func TestCookies(t *testing.T) {
29 a := assertions.New(t)
30
31 m := Cookies(
32 []byte("1234123412341234123412341234123412341234123412341234123412341234"),
33 []byte("12341234123412341234123412341234"),
34 )
35
36 t.Run("Set and Get Cookie", func(t *testing.T) {
37 r := httptest.NewRequest(http.MethodPut, "/", nil)
38 rec := httptest.NewRecorder()
39
40 var sc *securecookie.SecureCookie
41
42 m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
43 sc, _ = GetSecureCookie(r.Context())
44 value := map[string]string{
45 "foo": "bar",
46 }
47 if encoded, err := sc.Encode("cookie-name", value); err == nil {
48 cookie := &http.Cookie{
49 Name: "cookie-name",
50 Value: encoded,
51 Path: "/",
52 Secure: true,
53 HttpOnly: true,
54 }
55 http.SetCookie(rec, cookie)
56 }
57 })).ServeHTTP(rec, r)
58 res := rec.Result()
59
60 a.So(res.StatusCode, should.Equal, http.StatusOK)
61 a.So(res.Header.Get("Set-Cookie"), should.ContainSubstring, "cookie-name")
62
63 cookies := res.Cookies()
64
65 a.So(cookies, should.HaveLength, 1)
66 a.So(cookies[0].Name, should.Equal, "cookie-name")
67 a.So(cookies[0].Value, should.NotBeEmpty)
68
69 for _, cookie := range res.Cookies() {
70 if cookie.Name == "cookie-name" {
71 value := make(map[string]string)
72 err := sc.Decode("cookie-name", cookie.Value, &value)
73 a.So(err, should.BeNil)
74 a.So(value["foo"], should.Equal, "bar")
75 }
76 }
77 })
78}

Callers

nothing calls this directly

Calls 10

CookiesFunction · 0.85
GetSecureCookieFunction · 0.85
NewMethod · 0.65
RunMethod · 0.65
ContextMethod · 0.65
EncodeMethod · 0.65
ResultMethod · 0.65
GetMethod · 0.65
DecodeMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected