7 "github.com/google/uuid"
8 middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
9 . "github.com/onsi/ginkgo"
10 . "github.com/onsi/gomega"
14 testRequestHeader = "X-Request-Id"
15 testRequestID = "11111111-2222-4333-8444-555555555555"
16 // mockRand io.Reader below counts bytes from 0-255 in order
17 testRandomUUID = "00010203-0405-4607-8809-0a0b0c0d0e0f"
20var _ = Describe("Scope Suite", func() {
21 Context("NewScope", func() {
22 var request, nextRequest *http.Request
23 var rw http.ResponseWriter
27 request, err = http.NewRequest("", "http://127.0.0.1/", nil)
28 Expect(err).ToNot(HaveOccurred())
30 rw = httptest.NewRecorder()
33 Context("ReverseProxy is false", func() {
35 handler := NewScope(false, testRequestHeader)(
36 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
40 handler.ServeHTTP(rw, request)
43 It("does not add a scope to the original request", func() {
44 Expect(request.Context().Value(middlewareapi.RequestScopeKey)).To(BeNil())
47 It("cannot load a scope from the original request using GetRequestScope", func() {
48 Expect(middlewareapi.GetRequestScope(request)).To(BeNil())
51 It("adds a scope to the request for the next handler", func() {
52 Expect(nextRequest.Context().Value(middlewareapi.RequestScopeKey)).ToNot(BeNil())
55 It("can load a scope from the next handler's request using GetRequestScope", func() {
56 scope := middlewareapi.GetRequestScope(nextRequest)
57 Expect(scope).ToNot(BeNil())
58 Expect(scope.ReverseProxy).To(BeFalse())
62 Context("ReverseProxy is true", func() {
64 handler := NewScope(true, testRequestHeader)(
65 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
69 handler.ServeHTTP(rw, request)
72 It("return a scope where the ReverseProxy field is true", func() {
73 scope := middlewareapi.GetRequestScope(nextRequest)
74 Expect(scope).ToNot(BeNil())
75 Expect(scope.ReverseProxy).To(BeTrue())
79 Context("Request ID header is present", func() {
81 request.Header.Add(testRequestHeader, testRequestID)
82 handler := NewScope(false, testRequestHeader)(
83 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
87 handler.ServeHTTP(rw, request)
90 It("sets the RequestID using the request", func() {
91 scope := middlewareapi.GetRequestScope(nextRequest)
92 Expect(scope.RequestID).To(Equal(testRequestID))
96 Context("Request ID header is missing", func() {
98 uuid.SetRand(mockRand{})
100 handler := NewScope(true, testRequestHeader)(
101 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
105 handler.ServeHTTP(rw, request)
112 It("sets the RequestID using a random UUID", func() {
113 scope := middlewareapi.GetRequestScope(nextRequest)
114 Expect(scope.RequestID).To(Equal(testRandomUUID))
120type mockRand struct{}
122func (mockRand) Read(p []byte) (int, error) {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list