git.iko.soy/typeable/oauth2-proxy
on 8cbf921 help sign in
94 lines 2.4 kB history raw
1package middleware
3import (
4 "context"
5 "net/http"
6 "net/http/httptest"
8 middlewareapi "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/middleware"
9 . "github.com/onsi/ginkgo"
10 . "github.com/onsi/gomega"
13var _ = Describe("Scope Suite", func() {
14 Context("NewScope", func() {
15 var request, nextRequest *http.Request
16 var rw http.ResponseWriter
18 BeforeEach(func() {
19 var err error
20 request, err = http.NewRequest("", "http://127.0.0.1/", nil)
21 Expect(err).ToNot(HaveOccurred())
23 rw = httptest.NewRecorder()
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 })
32 It("does not add a scope to the original request", func() {
33 Expect(request.Context().Value(requestScopeKey)).To(BeNil())
34 })
36 It("cannot load a scope from the original request using GetRequestScope", func() {
37 Expect(GetRequestScope(request)).To(BeNil())
38 })
40 It("adds a scope to the request for the next handler", func() {
41 Expect(nextRequest.Context().Value(requestScopeKey)).ToNot(BeNil())
42 })
44 It("can load a scope from the next handler's request using GetRequestScope", func() {
45 Expect(GetRequestScope(nextRequest)).ToNot(BeNil())
46 })
47 })
49 Context("GetRequestScope", func() {
50 var request *http.Request
52 BeforeEach(func() {
53 var err error
54 request, err = http.NewRequest("", "http://127.0.0.1/", nil)
55 Expect(err).ToNot(HaveOccurred())
56 })
58 Context("with a scope", func() {
59 var scope *middlewareapi.RequestScope
61 BeforeEach(func() {
62 scope = &middlewareapi.RequestScope{}
63 contextWithScope := context.WithValue(request.Context(), requestScopeKey, scope)
64 request = request.WithContext(contextWithScope)
65 })
67 It("returns the scope", func() {
68 s := GetRequestScope(request)
69 Expect(s).ToNot(BeNil())
70 Expect(s).To(Equal(scope))
71 })
73 Context("if the scope is then modified", func() {
74 BeforeEach(func() {
75 Expect(scope.SaveSession).To(BeFalse())
76 scope.SaveSession = true
77 })
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 })
88 Context("without a scope", func() {
89 It("returns nil", func() {
90 Expect(GetRequestScope(request)).To(BeNil())
91 })
92 })
93 })
94})