Add tests for requests logger

This commit is contained in:
ErickSkrauch 2020-04-03 17:18:02 +03:00
parent 532f2206da
commit 20a8d90ad7
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
2 changed files with 84 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import (
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"syscall" "syscall"
"github.com/mono83/slf" "github.com/mono83/slf"
@ -25,19 +26,19 @@ func (l *Logger) ConfigureWithDispatcher(d dispatcher.EventDispatcher) {
} }
func (l *Logger) handleAfterSkinsystemRequest(req *http.Request, statusCode int) { func (l *Logger) handleAfterSkinsystemRequest(req *http.Request, statusCode int) {
forwardedIp := req.Header.Get("X-Forwarded-For") path := req.URL.Path
if forwardedIp == "" { if req.URL.RawQuery != "" {
forwardedIp = req.Header.Get("X-Real-Ip") path += "?" + req.URL.RawQuery
} }
l.Info( l.Info(
":ip - - \":method :path\" :statusCode \":userAgent\" \":forwardedIp\"", ":ip - - \":method :path\" :statusCode - \":userAgent\" \":forwardedIp\"",
wd.StringParam("ip", req.RemoteAddr), wd.StringParam("ip", trimPort(req.RemoteAddr)),
wd.StringParam("method", req.Method), wd.StringParam("method", req.Method),
wd.StringParam("path", req.URL.Path), wd.StringParam("path", path),
wd.IntParam("statusCode", statusCode), wd.IntParam("statusCode", statusCode),
wd.StringParam("userAgent", req.UserAgent()), wd.StringParam("userAgent", req.UserAgent()),
wd.StringParam("forwardedIp", forwardedIp), wd.StringParam("forwardedIp", req.Header.Get("X-Forwarded-For")),
) )
} }
@ -87,3 +88,10 @@ func (l *Logger) createMojangTexturesErrorHandler(provider string) func(identity
func (l *Logger) logMojangTexturesWarning(providerParam slf.Param, errParam slf.Param) { func (l *Logger) logMojangTexturesWarning(providerParam slf.Param, errParam slf.Param) {
l.Warning(":name: :err", providerParam, errParam) l.Warning(":name: :err", providerParam, errParam)
} }
func trimPort(ip string) string {
// Don't care about possible -1 result because RemoteAddr will always contain ip and port
cutTo := strings.LastIndexByte(ip, ':')
return ip[0:cutTo]
}

View File

@ -2,6 +2,8 @@ package eventsubscribers
import ( import (
"net" "net"
"net/http"
"net/http/httptest"
"net/url" "net/url"
"syscall" "syscall"
"testing" "testing"
@ -19,7 +21,73 @@ type LoggerTestCase struct {
ExpectedCalls [][]interface{} ExpectedCalls [][]interface{}
} }
var loggerTestCases = map[string]*LoggerTestCase{} var loggerTestCases = map[string]*LoggerTestCase{
"should log each request to the skinsystem": {
Events: [][]interface{}{
{"skinsystem:after_request",
(func() *http.Request {
req := httptest.NewRequest("GET", "http://localhost/skins/username.png", nil)
req.Header.Add("User-Agent", "Test user agent")
return req
})(),
201,
},
},
ExpectedCalls: [][]interface{}{
{"Info",
":ip - - \":method :path\" :statusCode - \":userAgent\" \":forwardedIp\"",
mock.MatchedBy(func(strParam params.String) bool {
return strParam.Key == "ip" && strParam.Value == "192.0.2.1"
}),
mock.MatchedBy(func(strParam params.String) bool {
return strParam.Key == "method" && strParam.Value == "GET"
}),
mock.MatchedBy(func(strParam params.String) bool {
return strParam.Key == "path" && strParam.Value == "/skins/username.png"
}),
mock.MatchedBy(func(strParam params.Int) bool {
return strParam.Key == "statusCode" && strParam.Value == 201
}),
mock.MatchedBy(func(strParam params.String) bool {
return strParam.Key == "userAgent" && strParam.Value == "Test user agent"
}),
mock.MatchedBy(func(strParam params.String) bool {
return strParam.Key == "forwardedIp" && strParam.Value == ""
}),
},
},
},
"should log each request to the skinsystem 2": {
Events: [][]interface{}{
{"skinsystem:after_request",
(func() *http.Request {
req := httptest.NewRequest("GET", "http://localhost/skins/username.png?authlib=1.5.2", nil)
req.Header.Add("User-Agent", "Test user agent")
req.Header.Add("X-Forwarded-For", "1.2.3.4")
return req
})(),
201,
},
},
ExpectedCalls: [][]interface{}{
{"Info",
":ip - - \":method :path\" :statusCode - \":userAgent\" \":forwardedIp\"",
mock.Anything, // Already tested
mock.Anything, // Already tested
mock.MatchedBy(func(strParam params.String) bool {
return strParam.Key == "path" && strParam.Value == "/skins/username.png?authlib=1.5.2"
}),
mock.Anything, // Already tested
mock.Anything, // Already tested
mock.MatchedBy(func(strParam params.String) bool {
return strParam.Key == "forwardedIp" && strParam.Value == "1.2.3.4"
}),
},
},
},
}
type timeoutError struct{} type timeoutError struct{}