jsonlog_marshalling_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. package jsonlog
  2. import (
  3. "regexp"
  4. "testing"
  5. )
  6. func TestJSONLogMarshalJSON(t *testing.T) {
  7. logs := map[*JSONLog]string{
  8. {Log: `"A log line with \\"`}: `^{\"log\":\"\\\"A log line with \\\\\\\\\\\"\",\"time\":\".{20,}\"}$`,
  9. {Log: "A log line"}: `^{\"log\":\"A log line\",\"time\":\".{20,}\"}$`,
  10. {Log: "A log line with \r"}: `^{\"log\":\"A log line with \\r\",\"time\":\".{20,}\"}$`,
  11. {Log: "A log line with & < >"}: `^{\"log\":\"A log line with \\u0026 \\u003c \\u003e\",\"time\":\".{20,}\"}$`,
  12. {Log: "A log line with utf8 : 🚀 ψ ω β"}: `^{\"log\":\"A log line with utf8 : 🚀 ψ ω β\",\"time\":\".{20,}\"}$`,
  13. {Stream: "stdout"}: `^{\"stream\":\"stdout\",\"time\":\".{20,}\"}$`,
  14. {}: `^{\"time\":\".{20,}\"}$`,
  15. // These ones are a little weird
  16. {Log: "\u2028 \u2029"}: `^{\"log\":\"\\u2028 \\u2029\",\"time\":\".{20,}\"}$`,
  17. {Log: string([]byte{0xaF})}: `^{\"log\":\"\\ufffd\",\"time\":\".{20,}\"}$`,
  18. {Log: string([]byte{0x7F})}: `^{\"log\":\"\x7f\",\"time\":\".{20,}\"}$`,
  19. }
  20. for jsonLog, expression := range logs {
  21. data, err := jsonLog.MarshalJSON()
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. res := string(data)
  26. t.Logf("Result of WriteLog: %q", res)
  27. logRe := regexp.MustCompile(expression)
  28. if !logRe.MatchString(res) {
  29. t.Fatalf("Log line not in expected format [%v]: %q", expression, res)
  30. }
  31. }
  32. }