mirror of
https://github.com/elyby/chrly.git
synced 2024-11-23 05:33:18 +05:30
Add tests for requests logger
This commit is contained in:
parent
532f2206da
commit
20a8d90ad7
@ -4,6 +4,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/mono83/slf"
|
||||
@ -25,19 +26,19 @@ func (l *Logger) ConfigureWithDispatcher(d dispatcher.EventDispatcher) {
|
||||
}
|
||||
|
||||
func (l *Logger) handleAfterSkinsystemRequest(req *http.Request, statusCode int) {
|
||||
forwardedIp := req.Header.Get("X-Forwarded-For")
|
||||
if forwardedIp == "" {
|
||||
forwardedIp = req.Header.Get("X-Real-Ip")
|
||||
path := req.URL.Path
|
||||
if req.URL.RawQuery != "" {
|
||||
path += "?" + req.URL.RawQuery
|
||||
}
|
||||
|
||||
l.Info(
|
||||
":ip - - \":method :path\" :statusCode \":userAgent\" \":forwardedIp\"",
|
||||
wd.StringParam("ip", req.RemoteAddr),
|
||||
":ip - - \":method :path\" :statusCode - \":userAgent\" \":forwardedIp\"",
|
||||
wd.StringParam("ip", trimPort(req.RemoteAddr)),
|
||||
wd.StringParam("method", req.Method),
|
||||
wd.StringParam("path", req.URL.Path),
|
||||
wd.StringParam("path", path),
|
||||
wd.IntParam("statusCode", statusCode),
|
||||
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) {
|
||||
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]
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package eventsubscribers
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"syscall"
|
||||
"testing"
|
||||
@ -19,7 +21,73 @@ type LoggerTestCase struct {
|
||||
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{}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user