(t *testing.T)
| 68 | } |
| 69 | |
| 70 | func TestCookie(t *testing.T) { |
| 71 | root := mux.NewRouter() |
| 72 | |
| 73 | a := assertions.New(t) |
| 74 | blockKey := random.Bytes(32) |
| 75 | hashKey := random.Bytes(64) |
| 76 | |
| 77 | root.Use(mux.MiddlewareFunc(webmiddleware.Cookies(hashKey, blockKey))) |
| 78 | |
| 79 | root.Path("/set").HandlerFunc(testSetCookie(t, "test_value")).Methods(http.MethodGet) |
| 80 | root.Path("/get").HandlerFunc(testGetCookie(t, "test_value", true)).Methods(http.MethodGet) |
| 81 | root.Path("/del").HandlerFunc(testDeleteCookie()).Methods(http.MethodGet) |
| 82 | root.Path("/no_cookie").HandlerFunc(testGetCookie(t, "", false)).Methods(http.MethodGet) |
| 83 | |
| 84 | jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) |
| 85 | if err != nil { |
| 86 | panic(err) |
| 87 | } |
| 88 | |
| 89 | doGET := func(path string) *http.Response { |
| 90 | req := httptest.NewRequest(http.MethodGet, path, nil) |
| 91 | req.URL.Scheme, req.URL.Host = "http", req.Host |
| 92 | for _, c := range jar.Cookies(req.URL) { |
| 93 | req.AddCookie(c) |
| 94 | } |
| 95 | |
| 96 | rec := httptest.NewRecorder() |
| 97 | |
| 98 | root.ServeHTTP(rec, req) |
| 99 | |
| 100 | resp := rec.Result() |
| 101 | resp.Request = req |
| 102 | |
| 103 | if cookies := resp.Cookies(); len(cookies) > 0 { |
| 104 | jar.SetCookies(req.URL, cookies) |
| 105 | } |
| 106 | |
| 107 | return resp |
| 108 | } |
| 109 | |
| 110 | resp := doGET("/no_cookie") |
| 111 | |
| 112 | cookies := jar.Cookies(resp.Request.URL) |
| 113 | a.So(cookies, should.BeEmpty) |
| 114 | |
| 115 | resp = doGET("/del") |
| 116 | |
| 117 | cookies = jar.Cookies(resp.Request.URL) |
| 118 | a.So(cookies, should.BeEmpty) |
| 119 | |
| 120 | resp = doGET("/set") |
| 121 | |
| 122 | cookies = jar.Cookies(resp.Request.URL) |
| 123 | if a.So(cookies, should.HaveLength, 1) { |
| 124 | cookie := cookies[0] |
| 125 | a.So(cookie.Name, should.Equal, "test_cookie") |
| 126 | } |
| 127 |
nothing calls this directly
no test coverage detected