@@ -0,0 +1,94 @@
1+package middleware
2+
3+import (
4+ "context"
5+ "net/http"
6+ "net/http/httptest"
7+
8+ middlewareapi "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/middleware"
9+ . "github.com/onsi/ginkgo"
10+ . "github.com/onsi/gomega"
11+)
12+
13+var _ = Describe("Scope Suite", func() {
14+ Context("NewScope", func() {
15+ var request, nextRequest *http.Request
16+ var rw http.ResponseWriter
17+
18+ BeforeEach(func() {
19+ var err error
20+ request, err = http.NewRequest("", "http://127.0.0.1/", nil)
21+ Expect(err).ToNot(HaveOccurred())
22+
23+ rw = httptest.NewRecorder()
24+
25+ handler := NewScope()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26+ nextRequest = r
27+ w.WriteHeader(200)
28+ }))
29+ handler.ServeHTTP(rw, request)
30+ })
31+
32+ It("does not add a scope to the original request", func() {
33+ Expect(request.Context().Value(requestScopeKey)).To(BeNil())
34+ })
35+
36+ It("cannot load a scope from the original request using GetRequestScope", func() {
37+ Expect(GetRequestScope(request)).To(BeNil())
38+ })
39+
40+ It("adds a scope to the request for the next handler", func() {
41+ Expect(nextRequest.Context().Value(requestScopeKey)).ToNot(BeNil())
42+ })
43+
44+ It("can load a scope from the next handler's request using GetRequestScope", func() {
45+ Expect(GetRequestScope(nextRequest)).ToNot(BeNil())
46+ })
47+ })
48+
49+ Context("GetRequestScope", func() {
50+ var request *http.Request
51+
52+ BeforeEach(func() {
53+ var err error
54+ request, err = http.NewRequest("", "http://127.0.0.1/", nil)
55+ Expect(err).ToNot(HaveOccurred())
56+ })
57+
58+ Context("with a scope", func() {
59+ var scope *middlewareapi.RequestScope
60+
61+ BeforeEach(func() {
62+ scope = &middlewareapi.RequestScope{}
63+ contextWithScope := context.WithValue(request.Context(), requestScopeKey, scope)
64+ request = request.WithContext(contextWithScope)
65+ })
66+
67+ It("returns the scope", func() {
68+ s := GetRequestScope(request)
69+ Expect(s).ToNot(BeNil())
70+ Expect(s).To(Equal(scope))
71+ })
72+
73+ Context("if the scope is then modified", func() {
74+ BeforeEach(func() {
75+ Expect(scope.SaveSession).To(BeFalse())
76+ scope.SaveSession = true
77+ })
78+
79+ It("returns the updated session", func() {
80+ s := GetRequestScope(request)
81+ Expect(s).ToNot(BeNil())
82+ Expect(s).To(Equal(scope))
83+ Expect(s.SaveSession).To(BeTrue())
84+ })
85+ })
86+ })
87+
88+ Context("without a scope", func() {
89+ It("returns nil", func() {
90+ Expect(GetRequestScope(request)).To(BeNil())
91+ })
92+ })
93+ })
94+})