2020-09-12 11:34:59 +02:00
|
|
|
package accesslog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kataras/iris/v12/core/memstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCSV(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
ac := New(buf)
|
2020-09-13 01:56:22 +02:00
|
|
|
ac.RequestBody = false
|
2020-09-12 11:34:59 +02:00
|
|
|
staticNow, _ := time.Parse(defaultTimeFormat, "1993-01-01 05:00:00")
|
|
|
|
ac.Clock = TClock(staticNow)
|
|
|
|
ac.SetFormatter(&CSV{
|
2020-09-13 01:56:22 +02:00
|
|
|
Header: true,
|
2020-09-12 11:34:59 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
lat, _ := time.ParseDuration("1s")
|
|
|
|
|
2021-01-09 04:41:20 +01:00
|
|
|
printFunc := func() {
|
2020-09-12 11:34:59 +02:00
|
|
|
ac.Print(
|
|
|
|
nil,
|
|
|
|
lat,
|
|
|
|
"",
|
|
|
|
200,
|
|
|
|
"GET",
|
|
|
|
"/",
|
|
|
|
"::1",
|
|
|
|
"",
|
|
|
|
"Index",
|
|
|
|
573,
|
|
|
|
81,
|
|
|
|
nil,
|
|
|
|
[]memstore.StringEntry{{Key: "sleep", Value: "1s"}},
|
|
|
|
nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// print twice, the header should only be written once.
|
2021-01-09 04:41:20 +01:00
|
|
|
printFunc()
|
|
|
|
printFunc()
|
2020-09-12 11:34:59 +02:00
|
|
|
|
2020-09-13 01:56:22 +02:00
|
|
|
expected := `Timestamp,Latency,Code,Method,Path,IP,Req Values,In,Out
|
|
|
|
725864400000,1s,200,GET,/,::1,sleep=1s,573,81
|
|
|
|
725864400000,1s,200,GET,/,::1,sleep=1s,573,81
|
2020-09-12 11:34:59 +02:00
|
|
|
`
|
|
|
|
|
2020-09-13 01:56:22 +02:00
|
|
|
ac.Close()
|
2020-09-12 11:34:59 +02:00
|
|
|
if got := buf.String(); expected != got {
|
|
|
|
t.Fatalf("expected:\n%s\n\nbut got:\n%s", expected, got)
|
|
|
|
}
|
|
|
|
}
|