2015-06-27 07:18:52 +00:00
|
|
|
package jsonlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-08-08 03:28:22 +00:00
|
|
|
func TestJSONLogsMarshalJSONBuf(t *testing.T) {
|
|
|
|
logs := map[*JSONLogs]string{
|
|
|
|
&JSONLogs{Log: []byte(`"A log line with \\"`)}: `^{\"log\":\"\\\"A log line with \\\\\\\\\\\"\",\"time\":}$`,
|
|
|
|
&JSONLogs{Log: []byte("A log line")}: `^{\"log\":\"A log line\",\"time\":}$`,
|
|
|
|
&JSONLogs{Log: []byte("A log line with \r")}: `^{\"log\":\"A log line with \\r\",\"time\":}$`,
|
|
|
|
&JSONLogs{Log: []byte("A log line with & < >")}: `^{\"log\":\"A log line with \\u0026 \\u003c \\u003e\",\"time\":}$`,
|
|
|
|
&JSONLogs{Log: []byte("A log line with utf8 : 🚀 ψ ω β")}: `^{\"log\":\"A log line with utf8 : 🚀 ψ ω β\",\"time\":}$`,
|
|
|
|
&JSONLogs{Stream: "stdout"}: `^{\"stream\":\"stdout\",\"time\":}$`,
|
|
|
|
&JSONLogs{Stream: "stdout", Log: []byte("A log line")}: `^{\"log\":\"A log line\",\"stream\":\"stdout\",\"time\":}$`,
|
|
|
|
&JSONLogs{Created: "time"}: `^{\"time\":time}$`,
|
|
|
|
&JSONLogs{}: `^{\"time\":}$`,
|
2015-06-27 07:18:52 +00:00
|
|
|
// These ones are a little weird
|
2015-08-08 03:28:22 +00:00
|
|
|
&JSONLogs{Log: []byte("\u2028 \u2029")}: `^{\"log\":\"\\u2028 \\u2029\",\"time\":}$`,
|
|
|
|
&JSONLogs{Log: []byte{0xaF}}: `^{\"log\":\"\\ufffd\",\"time\":}$`,
|
|
|
|
&JSONLogs{Log: []byte{0x7F}}: `^{\"log\":\"\x7f\",\"time\":}$`,
|
2015-06-27 07:18:52 +00:00
|
|
|
}
|
|
|
|
for jsonLog, expression := range logs {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := jsonLog.MarshalJSONBuf(&buf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
res := buf.String()
|
|
|
|
t.Logf("Result of WriteLog: %q", res)
|
|
|
|
logRe := regexp.MustCompile(expression)
|
|
|
|
if !logRe.MatchString(res) {
|
|
|
|
t.Fatalf("Log line not in expected format [%v]: %q", expression, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|