mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2026-10-18 13:18:26 +02:00
Move Logging to Middleware Package (#1070)
* Use a specialized ResponseWriter in middleware * Track User & Upstream in RequestScope * Wrap responses in our custom ResponseWriter * Add tests for logging middleware * Inject upstream metadata into request scope * Use custom ResponseWriter only in logging middleware * Assume RequestScope is never nil
This commit is contained in:
+23
-18
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/ginkgo/extensions/table"
|
||||
@@ -64,17 +65,24 @@ var _ = Describe("Proxy Suite", func() {
|
||||
type proxyTableInput struct {
|
||||
target string
|
||||
response testHTTPResponse
|
||||
upstream string
|
||||
}
|
||||
|
||||
DescribeTable("Proxy ServerHTTP",
|
||||
DescribeTable("Proxy ServeHTTP",
|
||||
func(in *proxyTableInput) {
|
||||
req := httptest.NewRequest("", in.target, nil)
|
||||
req := middlewareapi.AddRequestScope(
|
||||
httptest.NewRequest("", in.target, nil),
|
||||
&middlewareapi.RequestScope{},
|
||||
)
|
||||
rw := httptest.NewRecorder()
|
||||
// Don't mock the remote Address
|
||||
req.RemoteAddr = ""
|
||||
|
||||
upstreamServer.ServeHTTP(rw, req)
|
||||
|
||||
scope := middlewareapi.GetRequestScope(req)
|
||||
Expect(scope.Upstream).To(Equal(in.upstream))
|
||||
|
||||
Expect(rw.Code).To(Equal(in.response.code))
|
||||
|
||||
// Delete extra headers that aren't relevant to tests
|
||||
@@ -99,7 +107,6 @@ var _ = Describe("Proxy Suite", func() {
|
||||
response: testHTTPResponse{
|
||||
code: 200,
|
||||
header: map[string][]string{
|
||||
gapUpstream: {"http-backend"},
|
||||
contentType: {applicationJSON},
|
||||
},
|
||||
request: testHTTPRequest{
|
||||
@@ -114,6 +121,7 @@ var _ = Describe("Proxy Suite", func() {
|
||||
RequestURI: "http://example.localhost/http/1234",
|
||||
},
|
||||
},
|
||||
upstream: "http-backend",
|
||||
}),
|
||||
Entry("with a request to the File backend", &proxyTableInput{
|
||||
target: "http://example.localhost/files/foo",
|
||||
@@ -121,31 +129,29 @@ var _ = Describe("Proxy Suite", func() {
|
||||
code: 200,
|
||||
header: map[string][]string{
|
||||
contentType: {textPlainUTF8},
|
||||
gapUpstream: {"file-backend"},
|
||||
},
|
||||
raw: "foo",
|
||||
},
|
||||
upstream: "file-backend",
|
||||
}),
|
||||
Entry("with a request to the Static backend", &proxyTableInput{
|
||||
target: "http://example.localhost/static/bar",
|
||||
response: testHTTPResponse{
|
||||
code: 200,
|
||||
header: map[string][]string{
|
||||
gapUpstream: {"static-backend"},
|
||||
},
|
||||
raw: "Authenticated",
|
||||
code: 200,
|
||||
header: map[string][]string{},
|
||||
raw: "Authenticated",
|
||||
},
|
||||
upstream: "static-backend",
|
||||
}),
|
||||
Entry("with a request to the bad HTTP backend", &proxyTableInput{
|
||||
target: "http://example.localhost/bad-http/bad",
|
||||
response: testHTTPResponse{
|
||||
code: 502,
|
||||
header: map[string][]string{
|
||||
gapUpstream: {"bad-http-backend"},
|
||||
},
|
||||
code: 502,
|
||||
header: map[string][]string{},
|
||||
// This tests the error handler
|
||||
raw: "Proxy Error",
|
||||
},
|
||||
upstream: "bad-http-backend",
|
||||
}),
|
||||
Entry("with a request to the to an unregistered path", &proxyTableInput{
|
||||
target: "http://example.localhost/unregistered",
|
||||
@@ -161,12 +167,11 @@ var _ = Describe("Proxy Suite", func() {
|
||||
Entry("with a request to the to backend registered to a single path", &proxyTableInput{
|
||||
target: "http://example.localhost/single-path",
|
||||
response: testHTTPResponse{
|
||||
code: 200,
|
||||
header: map[string][]string{
|
||||
gapUpstream: {"single-path-backend"},
|
||||
},
|
||||
raw: "Authenticated",
|
||||
code: 200,
|
||||
header: map[string][]string{},
|
||||
raw: "Authenticated",
|
||||
},
|
||||
upstream: "single-path-backend",
|
||||
}),
|
||||
Entry("with a request to the to a subpath of a backend registered to a single path", &proxyTableInput{
|
||||
target: "http://example.localhost/single-path/unregistered",
|
||||
|
||||
Reference in New Issue
Block a user