traefik/middlewares/accesslog/logger_test.go

120 lines
3.6 KiB
Go
Raw Normal View History

2017-05-22 19:39:29 +00:00
package accesslog
2016-04-19 23:45:59 +00:00
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"testing"
shellwords "github.com/mattn/go-shellwords"
"github.com/stretchr/testify/assert"
2017-05-22 19:39:29 +00:00
"github.com/stretchr/testify/require"
2016-04-19 23:45:59 +00:00
)
type logtestResponseWriter struct{}
var (
2017-05-22 19:39:29 +00:00
logger *LogHandler
2017-04-27 15:36:04 +00:00
logfileNameSuffix = "/traefik/logger/test.log"
2016-04-19 23:45:59 +00:00
helloWorld = "Hello, World"
testBackendName = "http://127.0.0.1/testBackend"
testFrontendName = "testFrontend"
testStatus = 123
2017-05-22 19:39:29 +00:00
testContentSize int64 = 12
2016-04-19 23:45:59 +00:00
testHostname = "TestHost"
testUsername = "TestUser"
2017-05-22 19:39:29 +00:00
testPath = "testpath"
2016-04-19 23:45:59 +00:00
testPort = 8181
testProto = "HTTP/0.0"
testMethod = "POST"
testReferer = "testReferer"
testUserAgent = "testUserAgent"
)
func TestLogger(t *testing.T) {
2017-04-27 15:36:04 +00:00
tmp, err := ioutil.TempDir("", "testlogger")
if err != nil {
t.Fatalf("failed to create temp dir: %s", err)
2016-04-19 23:45:59 +00:00
}
2017-04-27 15:36:04 +00:00
defer os.RemoveAll(tmp)
logfilePath := filepath.Join(tmp, logfileNameSuffix)
2016-04-19 23:45:59 +00:00
2017-05-22 19:39:29 +00:00
logger, err = NewLogHandler(logfilePath)
require.NoError(t, err)
2017-04-27 15:36:04 +00:00
defer logger.Close()
if _, err := os.Stat(logfilePath); os.IsNotExist(err) {
t.Fatalf("logger should create %s", logfilePath)
}
2017-05-22 19:39:29 +00:00
req := &http.Request{
2016-04-19 23:45:59 +00:00
Header: map[string][]string{
"User-Agent": {testUserAgent},
"Referer": {testReferer},
},
Proto: testProto,
Host: testHostname,
Method: testMethod,
RemoteAddr: fmt.Sprintf("%s:%d", testHostname, testPort),
URL: &url.URL{
User: url.UserPassword(testUsername, ""),
Path: testPath,
},
}
logger.ServeHTTP(&logtestResponseWriter{}, req, LogWriterTestHandlerFunc)
2016-04-19 23:45:59 +00:00
if logdata, err := ioutil.ReadFile(logfilePath); err != nil {
fmt.Printf("%s\n%s\n", string(logdata), err.Error())
assert.Nil(t, err)
} else if tokens, err := shellwords.Parse(string(logdata)); err != nil {
fmt.Printf("%s\n", err.Error())
assert.Nil(t, err)
} else if assert.Equal(t, 14, len(tokens), printLogdata(logdata)) {
assert.Equal(t, testHostname, tokens[0], printLogdata(logdata))
assert.Equal(t, testUsername, tokens[2], printLogdata(logdata))
assert.Equal(t, fmt.Sprintf("%s %s %s", testMethod, testPath, testProto), tokens[5], printLogdata(logdata))
assert.Equal(t, fmt.Sprintf("%d", testStatus), tokens[6], printLogdata(logdata))
assert.Equal(t, fmt.Sprintf("%d", len(helloWorld)), tokens[7], printLogdata(logdata))
assert.Equal(t, testReferer, tokens[8], printLogdata(logdata))
assert.Equal(t, testUserAgent, tokens[9], printLogdata(logdata))
assert.Equal(t, "1", tokens[10], printLogdata(logdata))
assert.Equal(t, testFrontendName, tokens[11], printLogdata(logdata))
assert.Equal(t, testBackendName, tokens[12], printLogdata(logdata))
}
}
func printLogdata(logdata []byte) string {
return fmt.Sprintf(
"\nExpected: %s\n"+
"Actual: %s",
2017-05-22 19:39:29 +00:00
"TestHost - TestUser [13/Apr/2016:07:14:19 -0700] \"POST testpath HTTP/0.0\" 123 12 \"testReferer\" \"testUserAgent\" 1 \"testFrontend\" \"http://127.0.0.1/testBackend\" 1ms",
2016-04-19 23:45:59 +00:00
string(logdata))
}
func LogWriterTestHandlerFunc(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte(helloWorld))
rw.WriteHeader(testStatus)
2017-05-22 19:39:29 +00:00
logDataTable := GetLogDataTable(r)
logDataTable.Core[FrontendName] = testFrontendName
logDataTable.Core[BackendURL] = testBackendName
logDataTable.Core[OriginStatus] = testStatus
logDataTable.Core[OriginContentSize] = testContentSize
2016-04-19 23:45:59 +00:00
}
func (lrw *logtestResponseWriter) Header() http.Header {
return map[string][]string{}
}
func (lrw *logtestResponseWriter) Write(b []byte) (int, error) {
return len(b), nil
}
func (lrw *logtestResponseWriter) WriteHeader(s int) {
}