1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-03-21 21:47:11 +02:00

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.
This commit is contained in:
Fabian Stelzer 2021-08-09 14:58:48 +00:00
parent d51556515e
commit 662fa72e8c
No known key found for this signature in database

View File

@ -16,10 +16,16 @@ import (
) )
var _ = Describe("Proxy Suite", func() { var _ = Describe("Proxy Suite", func() {
var upstreamServer http.Handler type proxyTableInput struct {
target string
response testHTTPResponse
upstream string
upstreams options.Upstreams
}
Context("multiUpstreamProxy", func() { Context("multiUpstreamProxy", func() {
BeforeEach(func() { DescribeTable("Proxy ServeHTTP",
func(in *proxyTableInput) {
sigData := &options.SignatureData{Hash: crypto.SHA256, Key: "secret"} sigData := &options.SignatureData{Hash: crypto.SHA256, Key: "secret"}
writer := &pagewriter.WriterFuncs{ writer := &pagewriter.WriterFuncs{
@ -32,8 +38,10 @@ var _ = Describe("Proxy Suite", func() {
ok := http.StatusOK ok := http.StatusOK
accepted := http.StatusAccepted accepted := http.StatusAccepted
upstreams := options.Upstreams{ // Allows for specifying settings and even individual upstreams for specific tests and uses the default upstreams/configs otherwise
Configs: []options.Upstream{ upstreams := in.upstreams
if len(in.upstreams.Configs) == 0 {
upstreams.Configs = []options.Upstream{
{ {
ID: "http-backend", ID: "http-backend",
Path: "/http/", Path: "/http/",
@ -90,22 +98,12 @@ var _ = Describe("Proxy Suite", func() {
RewriteTarget: "/double-match/rewrite/$1", RewriteTarget: "/double-match/rewrite/$1",
URI: serverAddr, URI: serverAddr,
}, },
}, }
} }
var err error upstreamServer, err := NewProxy(upstreams, sigData, writer)
upstreamServer, err = NewProxy(upstreams, sigData, writer)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
})
type proxyTableInput struct {
target string
response testHTTPResponse
upstream string
}
DescribeTable("Proxy ServeHTTP",
func(in *proxyTableInput) {
req := middlewareapi.AddRequestScope( req := middlewareapi.AddRequestScope(
httptest.NewRequest("", in.target, nil), httptest.NewRequest("", in.target, nil),
&middlewareapi.RequestScope{}, &middlewareapi.RequestScope{},
@ -133,10 +131,12 @@ var _ = Describe("Proxy Suite", func() {
} }
// Compare the reflected request to the upstream // Compare the reflected request to the upstream
if body != nil {
request := testHTTPRequest{} request := testHTTPRequest{}
Expect(json.Unmarshal(body, &request)).To(Succeed()) Expect(json.Unmarshal(body, &request)).To(Succeed())
testSanitizeRequestHeader(request.Header) testSanitizeRequestHeader(request.Header)
Expect(request).To(Equal(in.response.request)) Expect(request).To(Equal(in.response.request))
}
}, },
Entry("with a request to the HTTP service", &proxyTableInput{ Entry("with a request to the HTTP service", &proxyTableInput{
target: "http://example.localhost/http/1234", target: "http://example.localhost/http/1234",
@ -312,6 +312,31 @@ var _ = Describe("Proxy Suite", func() {
}, },
upstream: "double-match-rewrite", upstream: "double-match-rewrite",
}), }),
Entry("containing an escaped '/' without ProxyRawPath", &proxyTableInput{
target: "http://example.localhost/%2F/test1/%2F/test2",
response: testHTTPResponse{
code: 301,
header: map[string][]string{
"Location": {
"http://example.localhost/test1/test2",
},
},
},
upstream: "",
}),
Entry("containing an escaped '/' with ProxyRawPath", &proxyTableInput{
upstreams: options.Upstreams{ProxyRawPath: true},
target: "http://example.localhost/%2F/test1/%2F/test2",
response: testHTTPResponse{
code: 404,
header: map[string][]string{
"X-Content-Type-Options": {"nosniff"},
contentType: {textPlainUTF8},
},
raw: "404 page not found\n",
},
upstream: "",
}),
) )
}) })
@ -321,24 +346,24 @@ var _ = Describe("Proxy Suite", func() {
expectedOutput []options.Upstream expectedOutput []options.Upstream
} }
var httpPath = options.Upstream{ httpPath := options.Upstream{
Path: "/http/", Path: "/http/",
} }
var httpSubPath = options.Upstream{ httpSubPath := options.Upstream{
Path: "/http/subpath/", Path: "/http/subpath/",
} }
var longerPath = options.Upstream{ longerPath := options.Upstream{
Path: "/longer-than-http", Path: "/longer-than-http",
} }
var shortPathWithRewrite = options.Upstream{ shortPathWithRewrite := options.Upstream{
Path: "^/h/(.*)", Path: "^/h/(.*)",
RewriteTarget: "/$1", RewriteTarget: "/$1",
} }
var shortSubPathWithRewrite = options.Upstream{ shortSubPathWithRewrite := options.Upstream{
Path: "^/h/bar/(.*)", Path: "^/h/bar/(.*)",
RewriteTarget: "/$1", RewriteTarget: "/$1",
} }