Refactor session_store_test.go

This commit is contained in:
Joel Speed 2019-05-07 13:32:54 +01:00
parent b965f25c10
commit 1c2ee715b3
No known key found for this signature in database
GPG Key ID: 6E80578D6751DEFB

View File

@ -27,6 +27,7 @@ var _ = Describe("NewSessionStore", func() {
var request *http.Request var request *http.Request
var response *httptest.ResponseRecorder var response *httptest.ResponseRecorder
var session *sessionsapi.SessionState var session *sessionsapi.SessionState
var ss sessionsapi.SessionStore
CheckCookieOptions := func() { CheckCookieOptions := func() {
Context("the cookies returned", func() { Context("the cookies returned", func() {
@ -72,9 +73,46 @@ var _ = Describe("NewSessionStore", func() {
}) })
} }
RunCookieTests := func() { SessionStoreInterfaceTests := func() {
var ss sessionsapi.SessionStore Context("when SaveSession is called", func() {
BeforeEach(func() {
err := ss.SaveSession(response, request, session)
Expect(err).ToNot(HaveOccurred())
})
It("sets a `set-cookie` header in the response", func() {
Expect(response.Header().Get("set-cookie")).ToNot(BeEmpty())
})
CheckCookieOptions()
})
Context("when ClearSession is called", func() {
BeforeEach(func() {
cookie := cookies.MakeCookie(request,
cookieOpts.CookieName,
"foo",
cookieOpts.CookiePath,
cookieOpts.CookieDomain,
cookieOpts.CookieHTTPOnly,
cookieOpts.CookieSecure,
cookieOpts.CookieExpire,
time.Now(),
)
request.AddCookie(cookie)
err := ss.ClearSession(response, request)
Expect(err).ToNot(HaveOccurred())
})
It("sets a `set-cookie` header in the response", func() {
Expect(response.Header().Get("Set-Cookie")).ToNot(BeEmpty())
})
CheckCookieOptions()
})
}
RunCookieTests := func() {
Context("with default options", func() { Context("with default options", func() {
BeforeEach(func() { BeforeEach(func() {
var err error var err error
@ -82,42 +120,7 @@ var _ = Describe("NewSessionStore", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
Context("when SaveSession is called", func() { SessionStoreInterfaceTests()
BeforeEach(func() {
err := ss.SaveSession(response, request, session)
Expect(err).ToNot(HaveOccurred())
})
It("sets a `set-cookie` header in the response", func() {
Expect(response.Header().Get("set-cookie")).ToNot(BeEmpty())
})
CheckCookieOptions()
})
Context("when ClearSession is called", func() {
BeforeEach(func() {
cookie := cookies.MakeCookie(request,
cookieOpts.CookieName,
"foo",
cookieOpts.CookiePath,
cookieOpts.CookieDomain,
cookieOpts.CookieHTTPOnly,
cookieOpts.CookieSecure,
cookieOpts.CookieExpire,
time.Now(),
)
request.AddCookie(cookie)
err := ss.ClearSession(response, request)
Expect(err).ToNot(HaveOccurred())
})
It("sets a `set-cookie` header in the response", func() {
Expect(response.Header().Get("set-cookie")).ToNot(BeEmpty())
})
CheckCookieOptions()
})
}) })
Context("with non-default options", func() { Context("with non-default options", func() {
@ -137,46 +140,13 @@ var _ = Describe("NewSessionStore", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
Context("when SaveSession is called", func() { SessionStoreInterfaceTests()
BeforeEach(func() {
err := ss.SaveSession(response, request, session)
Expect(err).ToNot(HaveOccurred())
})
It("sets a `set-cookie` header in the response", func() {
Expect(response.Header().Get("set-cookie")).ToNot(BeEmpty())
})
CheckCookieOptions()
})
Context("when ClearSession is called", func() {
BeforeEach(func() {
cookie := cookies.MakeCookie(request,
cookieOpts.CookieName,
"foo",
cookieOpts.CookiePath,
cookieOpts.CookieDomain,
cookieOpts.CookieHTTPOnly,
cookieOpts.CookieSecure,
cookieOpts.CookieExpire,
time.Now(),
)
request.AddCookie(cookie)
err := ss.ClearSession(response, request)
Expect(err).ToNot(HaveOccurred())
})
It("sets a `set-cookie` header in the response", func() {
Expect(response.Header().Get("Set-Cookie")).ToNot(BeEmpty())
})
CheckCookieOptions()
})
}) })
} }
BeforeEach(func() { BeforeEach(func() {
ss = nil
opts = &options.SessionOptions{} opts = &options.SessionOptions{}
// Set default options in CookieOptions // Set default options in CookieOptions