git.iko.soy/typeable/oauth2-proxy
on request-rules help sign in
182 lines 4.9 kB history raw
1package upstream
3import (
4 "crypto"
5 "encoding/json"
6 "fmt"
7 "html/template"
8 "net/http"
9 "net/http/httptest"
11 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
12 . "github.com/onsi/ginkgo"
13 . "github.com/onsi/ginkgo/extensions/table"
14 . "github.com/onsi/gomega"
17var _ = Describe("Proxy Suite", func() {
18 var upstreamServer http.Handler
20 BeforeEach(func() {
21 sigData := &options.SignatureData{Hash: crypto.SHA256, Key: "secret"}
23 tmpl, err := template.New("").Parse("{{ .Title }}\n{{ .Message }}\n{{ .ProxyPrefix }}")
24 Expect(err).ToNot(HaveOccurred())
25 errorHandler := NewProxyErrorHandler(tmpl, "prefix")
27 ok := http.StatusOK
29 upstreams := options.Upstreams{
30 {
31 ID: "http-backend",
32 Path: "/http/",
33 URI: serverAddr,
34 },
35 {
36 ID: "file-backend",
37 Path: "/files/",
38 URI: fmt.Sprintf("file:///%s", filesDir),
39 },
40 {
41 ID: "static-backend",
42 Path: "/static/",
43 Static: true,
44 StaticCode: &ok,
45 },
46 {
47 ID: "bad-http-backend",
48 Path: "/bad-http/",
49 URI: "http://::1",
50 },
51 {
52 ID: "single-path-backend",
53 Path: "/single-path",
54 Static: true,
55 StaticCode: &ok,
56 },
57 }
59 upstreamServer, err = NewProxy(upstreams, sigData, errorHandler)
60 Expect(err).ToNot(HaveOccurred())
61 })
63 type proxyTableInput struct {
64 target string
65 response testHTTPResponse
66 }
68 DescribeTable("Proxy ServerHTTP",
69 func(in *proxyTableInput) {
70 req := httptest.NewRequest("", in.target, nil)
71 rw := httptest.NewRecorder()
72 // Don't mock the remote Address
73 req.RemoteAddr = ""
75 upstreamServer.ServeHTTP(rw, req)
77 Expect(rw.Code).To(Equal(in.response.code))
79 // Delete extra headers that aren't relevant to tests
80 testSanitizeResponseHeader(rw.Header())
81 Expect(rw.Header()).To(Equal(in.response.header))
83 body := rw.Body.Bytes()
84 // If the raw body is set, check that, else check the Request object
85 if in.response.raw != "" {
86 Expect(string(body)).To(Equal(in.response.raw))
87 return
88 }
90 // Compare the reflected request to the upstream
91 request := testHTTPRequest{}
92 Expect(json.Unmarshal(body, &request)).To(Succeed())
93 testSanitizeRequestHeader(request.Header)
94 Expect(request).To(Equal(in.response.request))
95 },
96 Entry("with a request to the HTTP service", &proxyTableInput{
97 target: "http://example.localhost/http/1234",
98 response: testHTTPResponse{
99 code: 200,
100 header: map[string][]string{
101 gapUpstream: {"http-backend"},
102 contentType: {applicationJSON},
103 },
104 request: testHTTPRequest{
105 Method: "GET",
106 URL: "http://example.localhost/http/1234",
107 Header: map[string][]string{
108 "Gap-Auth": {""},
109 "Gap-Signature": {"sha256 ofB1u6+FhEUbFLc3/uGbJVkl7GaN4egFqVvyO3+2I1w="},
110 },
111 Body: []byte{},
112 Host: "example.localhost",
113 RequestURI: "http://example.localhost/http/1234",
114 },
115 },
116 }),
117 Entry("with a request to the File backend", &proxyTableInput{
118 target: "http://example.localhost/files/foo",
119 response: testHTTPResponse{
120 code: 200,
121 header: map[string][]string{
122 contentType: {textPlainUTF8},
123 gapUpstream: {"file-backend"},
124 },
125 raw: "foo",
126 },
127 }),
128 Entry("with a request to the Static backend", &proxyTableInput{
129 target: "http://example.localhost/static/bar",
130 response: testHTTPResponse{
131 code: 200,
132 header: map[string][]string{
133 gapUpstream: {"static-backend"},
134 },
135 raw: "Authenticated",
136 },
137 }),
138 Entry("with a request to the bad HTTP backend", &proxyTableInput{
139 target: "http://example.localhost/bad-http/bad",
140 response: testHTTPResponse{
141 code: 502,
142 header: map[string][]string{
143 gapUpstream: {"bad-http-backend"},
144 },
145 // This tests the error handler
146 raw: "Bad Gateway\nError proxying to upstream server\nprefix",
147 },
148 }),
149 Entry("with a request to the to an unregistered path", &proxyTableInput{
150 target: "http://example.localhost/unregistered",
151 response: testHTTPResponse{
152 code: 404,
153 header: map[string][]string{
154 "X-Content-Type-Options": {"nosniff"},
155 contentType: {textPlainUTF8},
156 },
157 raw: "404 page not found\n",
158 },
159 }),
160 Entry("with a request to the to backend registered to a single path", &proxyTableInput{
161 target: "http://example.localhost/single-path",
162 response: testHTTPResponse{
163 code: 200,
164 header: map[string][]string{
165 gapUpstream: {"single-path-backend"},
166 },
167 raw: "Authenticated",
168 },
169 }),
170 Entry("with a request to the to a subpath of a backend registered to a single path", &proxyTableInput{
171 target: "http://example.localhost/single-path/unregistered",
172 response: testHTTPResponse{
173 code: 404,
174 header: map[string][]string{
175 "X-Content-Type-Options": {"nosniff"},
176 contentType: {textPlainUTF8},
177 },
178 raw: "404 page not found\n",
179 },
180 }),
181 )
182})