2020-07-04 18:41:58 +01:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
|
2020-09-30 01:44:42 +09:00
|
|
|
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
|
2020-07-04 18:41:58 +01:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Scope Suite", func() {
|
|
|
|
Context("NewScope", func() {
|
|
|
|
var request, nextRequest *http.Request
|
|
|
|
var rw http.ResponseWriter
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
var err error
|
|
|
|
request, err = http.NewRequest("", "http://127.0.0.1/", nil)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
rw = httptest.NewRecorder()
|
|
|
|
})
|
|
|
|
|
2021-01-02 13:16:01 -08:00
|
|
|
Context("ReverseProxy is false", func() {
|
2020-07-04 18:41:58 +01:00
|
|
|
BeforeEach(func() {
|
2021-01-02 13:16:01 -08:00
|
|
|
handler := NewScope(false)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
nextRequest = r
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}))
|
|
|
|
handler.ServeHTTP(rw, request)
|
2020-07-04 18:41:58 +01:00
|
|
|
})
|
|
|
|
|
2021-01-02 13:16:01 -08:00
|
|
|
It("does not add a scope to the original request", func() {
|
|
|
|
Expect(request.Context().Value(middlewareapi.RequestScopeKey)).To(BeNil())
|
2020-07-04 18:41:58 +01:00
|
|
|
})
|
|
|
|
|
2021-01-02 13:16:01 -08:00
|
|
|
It("cannot load a scope from the original request using GetRequestScope", func() {
|
|
|
|
Expect(middlewareapi.GetRequestScope(request)).To(BeNil())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("adds a scope to the request for the next handler", func() {
|
|
|
|
Expect(nextRequest.Context().Value(middlewareapi.RequestScopeKey)).ToNot(BeNil())
|
|
|
|
})
|
2020-07-04 18:41:58 +01:00
|
|
|
|
2021-01-02 13:16:01 -08:00
|
|
|
It("can load a scope from the next handler's request using GetRequestScope", func() {
|
|
|
|
scope := middlewareapi.GetRequestScope(nextRequest)
|
|
|
|
Expect(scope).ToNot(BeNil())
|
|
|
|
Expect(scope.ReverseProxy).To(BeFalse())
|
2020-07-04 18:41:58 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-01-02 13:16:01 -08:00
|
|
|
Context("ReverseProxy is true", func() {
|
|
|
|
BeforeEach(func() {
|
|
|
|
handler := NewScope(true)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
nextRequest = r
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}))
|
|
|
|
handler.ServeHTTP(rw, request)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("return a scope where the ReverseProxy field is true", func() {
|
|
|
|
scope := middlewareapi.GetRequestScope(nextRequest)
|
|
|
|
Expect(scope).ToNot(BeNil())
|
|
|
|
Expect(scope.ReverseProxy).To(BeTrue())
|
2020-07-04 18:41:58 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|