@@ -140,7 +140,7 @@ var _ = Describe("CSRF Cookie Tests", func() {
140140 Host: cookieDomain,
141141 Path: cookiePath,
142142 },
143− }
143+ Header: make(http.Header)}
144144 })
145145
146146 AfterEach(func() {
@@ -168,6 +168,67 @@ var _ = Describe("CSRF Cookie Tests", func() {
168168 })
169169 })
170170
171+ Context("LoadCSRFCookie", func() {
172+ BeforeEach(func() {
173+ // we need to reset the time to ensure the cookie is valid
174+ privateCSRF.time.Reset()
175+ })
176+
177+ It("should return error when no cookie is set", func() {
178+ csrf, err := LoadCSRFCookie(req, cookieOpts)
179+ Expect(err).To(HaveOccurred())
180+ Expect(csrf).To(BeNil())
181+ })
182+
183+ It("should find one valid cookie", func() {
184+ privateCSRF.OAuthState = []byte(csrfState)
185+ privateCSRF.OIDCNonce = []byte(csrfNonce)
186+ encoded, err := privateCSRF.encodeCookie()
187+ Expect(err).ToNot(HaveOccurred())
188+
189+ req.AddCookie(&http.Cookie{
190+ Name: privateCSRF.cookieName(),
191+ Value: encoded,
192+ })
193+
194+ csrf, err := LoadCSRFCookie(req, cookieOpts)
195+ Expect(err).ToNot(HaveOccurred())
196+ Expect(csrf).ToNot(BeNil())
197+ })
198+
199+ It("should return error when one invalid cookie is set", func() {
200+ req.AddCookie(&http.Cookie{
201+ Name: privateCSRF.cookieName(),
202+ Value: "invalid",
203+ })
204+
205+ csrf, err := LoadCSRFCookie(req, cookieOpts)
206+ Expect(err).To(HaveOccurred())
207+ Expect(csrf).To(BeNil())
208+ })
209+
210+ It("should be able to handle two cookie with one invalid", func() {
211+ privateCSRF.OAuthState = []byte(csrfState)
212+ privateCSRF.OIDCNonce = []byte(csrfNonce)
213+ encoded, err := privateCSRF.encodeCookie()
214+ Expect(err).ToNot(HaveOccurred())
215+
216+ req.AddCookie(&http.Cookie{
217+ Name: privateCSRF.cookieName(),
218+ Value: "invalid",
219+ })
220+
221+ req.AddCookie(&http.Cookie{
222+ Name: privateCSRF.cookieName(),
223+ Value: encoded,
224+ })
225+
226+ csrf, err := LoadCSRFCookie(req, cookieOpts)
227+ Expect(err).ToNot(HaveOccurred())
228+ Expect(csrf).ToNot(BeNil())
229+ })
230+ })
231+
171232 Context("ClearCookie", func() {
172233 It("sets a cookie with an empty value in the past", func() {
173234 rw := httptest.NewRecorder()