git.iko.soy/typeable/oauth2-proxy
on ea261ca help sign in
127 lines 3.5 kB history raw
1package middleware
3import (
4 "net/http"
5 "net/http/httptest"
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"
13const (
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
25 BeforeEach(func() {
26 var err error
27 request, err = http.NewRequest("", "http://127.0.0.1/", nil)
28 Expect(err).ToNot(HaveOccurred())
30 rw = httptest.NewRecorder()
31 })
33 Context("ReverseProxy is false", func() {
34 BeforeEach(func() {
35 handler := NewScope(false, testRequestHeader)(
36 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
37 nextRequest = r
38 w.WriteHeader(200)
39 }))
40 handler.ServeHTTP(rw, request)
41 })
43 It("does not add a scope to the original request", func() {
44 Expect(request.Context().Value(middlewareapi.RequestScopeKey)).To(BeNil())
45 })
47 It("cannot load a scope from the original request using GetRequestScope", func() {
48 Expect(middlewareapi.GetRequestScope(request)).To(BeNil())
49 })
51 It("adds a scope to the request for the next handler", func() {
52 Expect(nextRequest.Context().Value(middlewareapi.RequestScopeKey)).ToNot(BeNil())
53 })
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())
59 })
60 })
62 Context("ReverseProxy is true", func() {
63 BeforeEach(func() {
64 handler := NewScope(true, testRequestHeader)(
65 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
66 nextRequest = r
67 w.WriteHeader(200)
68 }))
69 handler.ServeHTTP(rw, request)
70 })
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())
76 })
77 })
79 Context("Request ID header is present", func() {
80 BeforeEach(func() {
81 request.Header.Add(testRequestHeader, testRequestID)
82 handler := NewScope(false, testRequestHeader)(
83 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
84 nextRequest = r
85 w.WriteHeader(200)
86 }))
87 handler.ServeHTTP(rw, request)
88 })
90 It("sets the RequestID using the request", func() {
91 scope := middlewareapi.GetRequestScope(nextRequest)
92 Expect(scope.RequestID).To(Equal(testRequestID))
93 })
94 })
96 Context("Request ID header is missing", func() {
97 BeforeEach(func() {
98 uuid.SetRand(mockRand{})
100 handler := NewScope(true, testRequestHeader)(
101 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
102 nextRequest = r
103 w.WriteHeader(200)
104 }))
105 handler.ServeHTTP(rw, request)
106 })
108 AfterEach(func() {
109 uuid.SetRand(nil)
110 })
112 It("sets the RequestID using a random UUID", func() {
113 scope := middlewareapi.GetRequestScope(nextRequest)
114 Expect(scope.RequestID).To(Equal(testRandomUUID))
115 })
116 })
117 })
118})
120type mockRand struct{}
122func (mockRand) Read(p []byte) (int, error) {
123 for i := range p {
124 p[i] = byte(i % 256)
125 }
126 return len(p), nil