2017-07-14 13:51:16 +02:00
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
2019-02-10 08:37:45 -08:00
2020-09-30 01:44:42 +09:00
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
2020-07-19 14:20:20 -07:00
"github.com/stretchr/testify/assert"
2017-07-14 13:51:16 +02:00
)
2020-07-19 14:20:20 -07:00
const RequestLoggingFormatWithoutTime = "{{.Client}} - {{.Username}} [TIMELESS] {{.Host}} {{.RequestMethod}} {{.Upstream}} {{.RequestURI}} {{.Protocol}} {{.UserAgent}} {{.StatusCode}} {{.ResponseSize}} {{.RequestDuration}}"
2017-07-14 13:51:16 +02:00
2020-07-19 14:20:20 -07:00
func TestLoggingHandler_ServeHTTP ( t * testing . T ) {
2017-07-14 13:51:16 +02:00
tests := [ ] struct {
2020-07-19 14:20:20 -07:00
Format string
ExpectedLogMessage string
Path string
ExcludePaths [ ] string
2017-07-14 13:51:16 +02:00
} {
2020-07-19 14:20:20 -07:00
{
Format : RequestLoggingFormatWithoutTime ,
ExpectedLogMessage : "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n" ,
Path : "/foo/bar" ,
ExcludePaths : [ ] string { } ,
} ,
{
Format : RequestLoggingFormatWithoutTime ,
ExpectedLogMessage : "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n" ,
Path : "/foo/bar" ,
ExcludePaths : [ ] string { } ,
} ,
{
Format : RequestLoggingFormatWithoutTime ,
ExpectedLogMessage : "127.0.0.1 - - [TIMELESS] test-server GET - \"/foo/bar\" HTTP/1.1 \"\" 200 4 0.000\n" ,
Path : "/foo/bar" ,
ExcludePaths : [ ] string { "/ping" } ,
} ,
{
Format : RequestLoggingFormatWithoutTime ,
ExpectedLogMessage : "" ,
Path : "/foo/bar" ,
ExcludePaths : [ ] string { "/foo/bar" } ,
} ,
{
Format : RequestLoggingFormatWithoutTime ,
ExpectedLogMessage : "127.0.0.1 - - [TIMELESS] test-server GET - \"/ping\" HTTP/1.1 \"\" 200 4 0.000\n" ,
Path : "/ping" ,
ExcludePaths : [ ] string { } ,
} ,
{
Format : RequestLoggingFormatWithoutTime ,
ExpectedLogMessage : "" ,
Path : "/ping" ,
ExcludePaths : [ ] string { "/ping" } ,
} ,
{
Format : RequestLoggingFormatWithoutTime ,
ExpectedLogMessage : "" ,
Path : "/ping" ,
ExcludePaths : [ ] string { "/foo/bar" , "/ping" } ,
} ,
{
Format : "{{.RequestMethod}}" ,
ExpectedLogMessage : "GET\n" ,
Path : "/foo/bar" ,
ExcludePaths : [ ] string { "" } ,
} ,
{
Format : "{{.RequestMethod}}" ,
ExpectedLogMessage : "GET\n" ,
Path : "/foo/bar" ,
ExcludePaths : [ ] string { "/ping" } ,
} ,
{
Format : "{{.RequestMethod}}" ,
ExpectedLogMessage : "GET\n" ,
Path : "/ping" ,
ExcludePaths : [ ] string { "" } ,
} ,
{
Format : "{{.RequestMethod}}" ,
ExpectedLogMessage : "" ,
Path : "/ping" ,
ExcludePaths : [ ] string { "/ping" } ,
} ,
2017-07-14 13:51:16 +02:00
}
for _ , test := range tests {
buf := bytes . NewBuffer ( nil )
handler := func ( w http . ResponseWriter , req * http . Request ) {
2019-03-22 17:19:38 -04:00
_ , ok := w . ( http . Hijacker )
if ! ok {
t . Error ( "http.Hijacker is not available" )
}
2020-07-19 14:20:20 -07:00
_ , err := w . Write ( [ ] byte ( "test" ) )
assert . NoError ( t , err )
2017-07-14 13:51:16 +02:00
}
2019-02-10 08:37:45 -08:00
logger . SetOutput ( buf )
logger . SetReqTemplate ( test . Format )
2019-06-22 09:39:46 +12:00
logger . SetExcludePaths ( test . ExcludePaths )
2019-02-10 08:37:45 -08:00
h := LoggingHandler ( http . HandlerFunc ( handler ) )
2017-07-14 13:51:16 +02:00
2019-05-31 20:11:28 +12:00
r , _ := http . NewRequest ( "GET" , test . Path , nil )
2017-07-14 13:51:16 +02:00
r . RemoteAddr = "127.0.0.1"
r . Host = "test-server"
h . ServeHTTP ( httptest . NewRecorder ( ) , r )
actual := buf . String ( )
2020-07-19 14:20:20 -07:00
assert . Equal ( t , test . ExpectedLogMessage , actual )
2017-07-14 13:51:16 +02:00
}
}