2016-04-19 23:45:59 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2017-05-09 12:02:44 +00:00
|
|
|
"context"
|
2016-04-19 23:45:59 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2016-12-30 08:21:13 +00:00
|
|
|
|
2017-05-09 12:02:44 +00:00
|
|
|
"github.com/containous/traefik/middlewares/accesslog"
|
2016-12-30 08:21:13 +00:00
|
|
|
shellwords "github.com/mattn/go-shellwords"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-04-19 23:45:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type logtestResponseWriter struct{}
|
|
|
|
|
|
|
|
var (
|
|
|
|
logger *Logger
|
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
|
|
|
|
testHostname = "TestHost"
|
|
|
|
testUsername = "TestUser"
|
|
|
|
testPath = "http://testpath"
|
|
|
|
testPort = 8181
|
|
|
|
testProto = "HTTP/0.0"
|
|
|
|
testMethod = "POST"
|
|
|
|
testReferer = "testReferer"
|
|
|
|
testUserAgent = "testUserAgent"
|
|
|
|
testBackend2FrontendMap = map[string]string{
|
|
|
|
testBackendName: testFrontendName,
|
|
|
|
}
|
|
|
|
printedLogdata bool
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
logger = NewLogger(logfilePath)
|
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)
|
|
|
|
}
|
|
|
|
|
2016-04-19 23:45:59 +00:00
|
|
|
SetBackend2FrontendMap(&testBackend2FrontendMap)
|
|
|
|
|
|
|
|
r := &http.Request{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-09 12:02:44 +00:00
|
|
|
// Temporary - until new access logger is fully implemented
|
|
|
|
// create the data table and populate frontend and backend
|
|
|
|
core := make(accesslog.CoreLogData)
|
|
|
|
logDataTable := &accesslog.LogData{Core: core, Request: r.Header}
|
|
|
|
logDataTable.Core[accesslog.FrontendName] = testFrontendName
|
|
|
|
logDataTable.Core[accesslog.BackendURL] = testBackendName
|
|
|
|
req := r.WithContext(context.WithValue(r.Context(), accesslog.DataTableKey, logDataTable))
|
|
|
|
|
|
|
|
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",
|
|
|
|
"TestHost - TestUser [13/Apr/2016:07:14:19 -0700] \"POST http://testpath HTTP/0.0\" 123 12 \"testReferer\" \"testUserAgent\" 1 \"testFrontend\" \"http://127.0.0.1/testBackend\" 1ms",
|
|
|
|
string(logdata))
|
|
|
|
}
|
|
|
|
|
|
|
|
func LogWriterTestHandlerFunc(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Write([]byte(helloWorld))
|
|
|
|
rw.WriteHeader(testStatus)
|
|
|
|
saveBackendNameForLogger(r, testBackendName)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
}
|