2020-04-02 04:59:14 +05:30
|
|
|
package eventsubscribers
|
|
|
|
|
|
|
|
import (
|
2020-04-03 19:48:02 +05:30
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-04-02 04:59:14 +05:30
|
|
|
"testing"
|
|
|
|
|
2020-04-04 00:50:56 +05:30
|
|
|
"github.com/mono83/slf"
|
2020-04-02 04:59:14 +05:30
|
|
|
"github.com/mono83/slf/params"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
2024-02-01 12:28:26 +05:30
|
|
|
"github.com/elyby/chrly/internal/dispatcher"
|
2020-04-02 04:59:14 +05:30
|
|
|
)
|
|
|
|
|
2020-04-04 00:50:56 +05:30
|
|
|
type LoggerMock struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareLoggerArgs(message string, params []slf.Param) []interface{} {
|
|
|
|
args := []interface{}{message}
|
|
|
|
for _, v := range params {
|
|
|
|
args = append(args, v.(interface{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LoggerMock) Trace(message string, params ...slf.Param) {
|
|
|
|
l.Called(prepareLoggerArgs(message, params)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LoggerMock) Debug(message string, params ...slf.Param) {
|
|
|
|
l.Called(prepareLoggerArgs(message, params)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LoggerMock) Info(message string, params ...slf.Param) {
|
|
|
|
l.Called(prepareLoggerArgs(message, params)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LoggerMock) Warning(message string, params ...slf.Param) {
|
|
|
|
l.Called(prepareLoggerArgs(message, params)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LoggerMock) Error(message string, params ...slf.Param) {
|
|
|
|
l.Called(prepareLoggerArgs(message, params)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LoggerMock) Alert(message string, params ...slf.Param) {
|
|
|
|
l.Called(prepareLoggerArgs(message, params)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LoggerMock) Emergency(message string, params ...slf.Param) {
|
|
|
|
l.Called(prepareLoggerArgs(message, params)...)
|
|
|
|
}
|
|
|
|
|
2020-04-02 04:59:14 +05:30
|
|
|
type LoggerTestCase struct {
|
|
|
|
Events [][]interface{}
|
|
|
|
ExpectedCalls [][]interface{}
|
|
|
|
}
|
|
|
|
|
2020-04-03 19:48:02 +05:30
|
|
|
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"
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-04-02 04:59:14 +05:30
|
|
|
|
|
|
|
func TestLogger(t *testing.T) {
|
|
|
|
for name, c := range loggerTestCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-04-04 00:50:56 +05:30
|
|
|
loggerMock := &LoggerMock{}
|
2020-04-02 04:59:14 +05:30
|
|
|
if c.ExpectedCalls != nil {
|
|
|
|
for _, c := range c.ExpectedCalls {
|
|
|
|
topicName, _ := c[0].(string)
|
2020-04-04 00:50:56 +05:30
|
|
|
loggerMock.On(topicName, c[1:]...)
|
2020-04-02 04:59:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reporter := &Logger{
|
2020-04-04 00:50:56 +05:30
|
|
|
Logger: loggerMock,
|
2020-04-02 04:59:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
d := dispatcher.New()
|
|
|
|
reporter.ConfigureWithDispatcher(d)
|
|
|
|
for _, args := range c.Events {
|
|
|
|
eventName, _ := args[0].(string)
|
|
|
|
d.Emit(eventName, args[1:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ExpectedCalls != nil {
|
|
|
|
for _, c := range c.ExpectedCalls {
|
|
|
|
topicName, _ := c[0].(string)
|
2020-04-04 00:50:56 +05:30
|
|
|
loggerMock.AssertCalled(t, topicName, c[1:]...)
|
2020-04-02 04:59:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|