@@ -0,0 +1,131 @@
1+package util_test
2+
3+import (
4+ "fmt"
5+ "net/http"
6+ "net/http/httptest"
7+
8+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
9+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests/util"
10+ . "github.com/onsi/ginkgo"
11+ . "github.com/onsi/gomega"
12+)
13+
14+var _ = Describe("Util Suite", func() {
15+ const (
16+ proto = "http"
17+ host = "www.oauth2proxy.test"
18+ uri = "/test/endpoint"
19+ )
20+ var req *http.Request
21+
22+ BeforeEach(func() {
23+ req = httptest.NewRequest(
24+ http.MethodGet,
25+ fmt.Sprintf("%s://%s%s", proto, host, uri),
26+ nil,
27+ )
28+ })
29+
30+ Context("GetRequestHost", func() {
31+ Context("IsProxied is false", func() {
32+ BeforeEach(func() {
33+ req = middleware.AddRequestScope(req, &middleware.RequestScope{})
34+ })
35+
36+ It("returns the host", func() {
37+ Expect(util.GetRequestHost(req)).To(Equal(host))
38+ })
39+
40+ It("ignores X-Forwarded-Host and returns the host", func() {
41+ req.Header.Add("X-Forwarded-Host", "external.oauth2proxy.text")
42+ Expect(util.GetRequestHost(req)).To(Equal(host))
43+ })
44+ })
45+
46+ Context("IsProxied is true", func() {
47+ BeforeEach(func() {
48+ req = middleware.AddRequestScope(req, &middleware.RequestScope{
49+ ReverseProxy: true,
50+ })
51+ })
52+
53+ It("returns the host if X-Forwarded-Host is not present", func() {
54+ Expect(util.GetRequestHost(req)).To(Equal(host))
55+ })
56+
57+ It("returns the X-Forwarded-Host when present", func() {
58+ req.Header.Add("X-Forwarded-Host", "external.oauth2proxy.text")
59+ Expect(util.GetRequestHost(req)).To(Equal("external.oauth2proxy.text"))
60+ })
61+ })
62+ })
63+
64+ Context("GetRequestProto", func() {
65+ Context("IsProxied is false", func() {
66+ BeforeEach(func() {
67+ req = middleware.AddRequestScope(req, &middleware.RequestScope{})
68+ })
69+
70+ It("returns the scheme", func() {
71+ Expect(util.GetRequestProto(req)).To(Equal(proto))
72+ })
73+
74+ It("ignores X-Forwarded-Proto and returns the scheme", func() {
75+ req.Header.Add("X-Forwarded-Proto", "https")
76+ Expect(util.GetRequestProto(req)).To(Equal(proto))
77+ })
78+ })
79+
80+ Context("IsProxied is true", func() {
81+ BeforeEach(func() {
82+ req = middleware.AddRequestScope(req, &middleware.RequestScope{
83+ ReverseProxy: true,
84+ })
85+ })
86+
87+ It("returns the scheme if X-Forwarded-Proto is not present", func() {
88+ Expect(util.GetRequestProto(req)).To(Equal(proto))
89+ })
90+
91+ It("returns the X-Forwarded-Proto when present", func() {
92+ req.Header.Add("X-Forwarded-Proto", "https")
93+ Expect(util.GetRequestProto(req)).To(Equal("https"))
94+ })
95+ })
96+ })
97+
98+ Context("GetRequestURI", func() {
99+ Context("IsProxied is false", func() {
100+ BeforeEach(func() {
101+ req = middleware.AddRequestScope(req, &middleware.RequestScope{})
102+ })
103+
104+ It("returns the URI", func() {
105+ Expect(util.GetRequestURI(req)).To(Equal(uri))
106+ })
107+
108+ It("ignores X-Forwarded-Uri and returns the URI", func() {
109+ req.Header.Add("X-Forwarded-Uri", "/some/other/path")
110+ Expect(util.GetRequestURI(req)).To(Equal(uri))
111+ })
112+ })
113+
114+ Context("IsProxied is true", func() {
115+ BeforeEach(func() {
116+ req = middleware.AddRequestScope(req, &middleware.RequestScope{
117+ ReverseProxy: true,
118+ })
119+ })
120+
121+ It("returns the URI if X-Forwarded-Uri is not present", func() {
122+ Expect(util.GetRequestURI(req)).To(Equal(uri))
123+ })
124+
125+ It("returns the X-Forwarded-Uri when present", func() {
126+ req.Header.Add("X-Forwarded-Uri", "/some/other/path")
127+ Expect(util.GetRequestURI(req)).To(Equal("/some/other/path"))
128+ })
129+ })
130+ })
131+})