git.iko.soy/typeable/oauth2-proxy
on master help sign in
Add ProxyRawPath tests

Refactor proxy_test to set mux/upstream options for each test individually and add tests for encoded urls with ProxyRawPath set and unset.

commit662fa72e8c7bd52bad5c1200f9e7a9b07ee3b853 authorFabian Stelzer <fs@gigacodes.de> dateMon 9 Aug 2021, 14:58 (5 years ago) parentd515565
1 file changed+117−92
pkg/upstream/proxy_test.go+117−92
pkg/upstream/proxy_test.go+117−92
@@ -16,96 +16,94 @@ import (
1616)
1717
1818var _ = Describe("Proxy Suite", func() {
19− var upstreamServer http.Handler
19+ type proxyTableInput struct {
20+ target string
21+ response testHTTPResponse
22+ upstream string
23+ upstreams options.Upstreams
24+ }
2025
2126 Context("multiUpstreamProxy", func() {
22− BeforeEach(func() {
23− sigData := &options.SignatureData{Hash: crypto.SHA256, Key: "secret"}
24−
25− writer := &pagewriter.WriterFuncs{
26− ProxyErrorFunc: func(rw http.ResponseWriter, _ *http.Request, _ error) {
27− rw.WriteHeader(502)
28− rw.Write([]byte("Proxy Error"))
29− },
30− }
31−
32− ok := http.StatusOK
33− accepted := http.StatusAccepted
27+ DescribeTable("Proxy ServeHTTP",
28+ func(in *proxyTableInput) {
29+ sigData := &options.SignatureData{Hash: crypto.SHA256, Key: "secret"}
3430
35− upstreams := options.Upstreams{
36− Configs: []options.Upstream{
37− {
38− ID: "http-backend",
39− Path: "/http/",
40− URI: serverAddr,
41− },
42− {
43− ID: "file-backend",
44− Path: "/files/",
45− URI: fmt.Sprintf("file:///%s", filesDir),
46− },
47− {
48− ID: "static-backend",
49− Path: "/static/",
50− Static: true,
51− StaticCode: &ok,
52− },
53− {
54− ID: "static-backend-no-trailing-slash",
55− Path: "/static",
56− Static: true,
57− StaticCode: &accepted,
58− },
59− {
60− ID: "static-backend-long",
61− Path: "/static/long",
62− Static: true,
63− StaticCode: &accepted,
64− },
65− {
66− ID: "bad-http-backend",
67− Path: "/bad-http/",
68− URI: "http://::1",
69− },
70− {
71− ID: "single-path-backend",
72− Path: "/single-path",
73− Static: true,
74− StaticCode: &ok,
75− },
76− {
77− ID: "backend-with-rewrite-prefix",
78− Path: "^/rewrite-prefix/(.*)",
79− RewriteTarget: "/different/backend/path/$1",
80− URI: serverAddr,
31+ writer := &pagewriter.WriterFuncs{
32+ ProxyErrorFunc: func(rw http.ResponseWriter, _ *http.Request, _ error) {
33+ rw.WriteHeader(502)
34+ rw.Write([]byte("Proxy Error"))
8135 },
82− {
83− ID: "double-match-plain",
84− Path: "/double-match/",
85− URI: serverAddr,
86− },
87− {
88− ID: "double-match-rewrite",
89− Path: "^/double-match/(.*)",
90− RewriteTarget: "/double-match/rewrite/$1",
91− URI: serverAddr,
92− },
93− },
94− }
36+ }
9537
96− var err error
97− upstreamServer, err = NewProxy(upstreams, sigData, writer)
98− Expect(err).ToNot(HaveOccurred())
99− })
38+ ok := http.StatusOK
39+ accepted := http.StatusAccepted
10040
101− type proxyTableInput struct {
102− target string
103− response testHTTPResponse
104− upstream string
105− }
41+ // Allows for specifying settings and even individual upstreams for specific tests and uses the default upstreams/configs otherwise
42+ upstreams := in.upstreams
43+ if len(in.upstreams.Configs) == 0 {
44+ upstreams.Configs = []options.Upstream{
45+ {
46+ ID: "http-backend",
47+ Path: "/http/",
48+ URI: serverAddr,
49+ },
50+ {
51+ ID: "file-backend",
52+ Path: "/files/",
53+ URI: fmt.Sprintf("file:///%s", filesDir),
54+ },
55+ {
56+ ID: "static-backend",
57+ Path: "/static/",
58+ Static: true,
59+ StaticCode: &ok,
60+ },
61+ {
62+ ID: "static-backend-no-trailing-slash",
63+ Path: "/static",
64+ Static: true,
65+ StaticCode: &accepted,
66+ },
67+ {
68+ ID: "static-backend-long",
69+ Path: "/static/long",
70+ Static: true,
71+ StaticCode: &accepted,
72+ },
73+ {
74+ ID: "bad-http-backend",
75+ Path: "/bad-http/",
76+ URI: "http://::1",
77+ },
78+ {
79+ ID: "single-path-backend",
80+ Path: "/single-path",
81+ Static: true,
82+ StaticCode: &ok,
83+ },
84+ {
85+ ID: "backend-with-rewrite-prefix",
86+ Path: "^/rewrite-prefix/(.*)",
87+ RewriteTarget: "/different/backend/path/$1",
88+ URI: serverAddr,
89+ },
90+ {
91+ ID: "double-match-plain",
92+ Path: "/double-match/",
93+ URI: serverAddr,
94+ },
95+ {
96+ ID: "double-match-rewrite",
97+ Path: "^/double-match/(.*)",
98+ RewriteTarget: "/double-match/rewrite/$1",
99+ URI: serverAddr,
100+ },
101+ }
102+ }
103+
104+ upstreamServer, err := NewProxy(upstreams, sigData, writer)
105+ Expect(err).ToNot(HaveOccurred())
106106
107− DescribeTable("Proxy ServeHTTP",
108− func(in *proxyTableInput) {
109107 req := middlewareapi.AddRequestScope(
110108 httptest.NewRequest("", in.target, nil),
111109 &middlewareapi.RequestScope{},
@@ -133,10 +131,12 @@ var _ = Describe("Proxy Suite", func() {
133131 }
134132
135133 // Compare the reflected request to the upstream
136− request := testHTTPRequest{}
137− Expect(json.Unmarshal(body, &request)).To(Succeed())
138− testSanitizeRequestHeader(request.Header)
139− Expect(request).To(Equal(in.response.request))
134+ if body != nil {
135+ request := testHTTPRequest{}
136+ Expect(json.Unmarshal(body, &request)).To(Succeed())
137+ testSanitizeRequestHeader(request.Header)
138+ Expect(request).To(Equal(in.response.request))
139+ }
140140 },
141141 Entry("with a request to the HTTP service", &proxyTableInput{
142142 target: "http://example.localhost/http/1234",
@@ -312,6 +312,31 @@ var _ = Describe("Proxy Suite", func() {
312312 },
313313 upstream: "double-match-rewrite",
314314 }),
315+ Entry("containing an escaped '/' without ProxyRawPath", &proxyTableInput{
316+ target: "http://example.localhost/%2F/test1/%2F/test2",
317+ response: testHTTPResponse{
318+ code: 301,
319+ header: map[string][]string{
320+ "Location": {
321+ "http://example.localhost/test1/test2",
322+ },
323+ },
324+ },
325+ upstream: "",
326+ }),
327+ Entry("containing an escaped '/' with ProxyRawPath", &proxyTableInput{
328+ upstreams: options.Upstreams{ProxyRawPath: true},
329+ target: "http://example.localhost/%2F/test1/%2F/test2",
330+ response: testHTTPResponse{
331+ code: 404,
332+ header: map[string][]string{
333+ "X-Content-Type-Options": {"nosniff"},
334+ contentType: {textPlainUTF8},
335+ },
336+ raw: "404 page not found\n",
337+ },
338+ upstream: "",
339+ }),
315340 )
316341 })
317342
@@ -321,24 +346,24 @@ var _ = Describe("Proxy Suite", func() {
321346 expectedOutput []options.Upstream
322347 }
323348
324− var httpPath = options.Upstream{
349+ httpPath := options.Upstream{
325350 Path: "/http/",
326351 }
327352
328− var httpSubPath = options.Upstream{
353+ httpSubPath := options.Upstream{
329354 Path: "/http/subpath/",
330355 }
331356
332− var longerPath = options.Upstream{
357+ longerPath := options.Upstream{
333358 Path: "/longer-than-http",
334359 }
335360
336− var shortPathWithRewrite = options.Upstream{
361+ shortPathWithRewrite := options.Upstream{
337362 Path: "^/h/(.*)",
338363 RewriteTarget: "/$1",
339364 }
340365
341− var shortSubPathWithRewrite = options.Upstream{
366+ shortSubPathWithRewrite := options.Upstream{
342367 Path: "^/h/bar/(.*)",
343368 RewriteTarget: "/$1",
344369 }