jsonlog_marshalling_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. package jsonlog
  2. import (
  3. "regexp"
  4. "testing"
  5. "encoding/json"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestJSONLogMarshalJSON(t *testing.T) {
  10. logs := map[*JSONLog]string{
  11. {Log: `"A log line with \\"`}: `^{\"log\":\"\\\"A log line with \\\\\\\\\\\"\",\"time\":\".{20,}\"}$`,
  12. {Log: "A log line"}: `^{\"log\":\"A log line\",\"time\":\".{20,}\"}$`,
  13. {Log: "A log line with \r"}: `^{\"log\":\"A log line with \\r\",\"time\":\".{20,}\"}$`,
  14. {Log: "A log line with & < >"}: `^{\"log\":\"A log line with \\u0026 \\u003c \\u003e\",\"time\":\".{20,}\"}$`,
  15. {Log: "A log line with utf8 : 🚀 ψ ω β"}: `^{\"log\":\"A log line with utf8 : 🚀 ψ ω β\",\"time\":\".{20,}\"}$`,
  16. {Stream: "stdout"}: `^{\"stream\":\"stdout\",\"time\":\".{20,}\"}$`,
  17. {}: `^{\"time\":\".{20,}\"}$`,
  18. // These ones are a little weird
  19. {Log: "\u2028 \u2029"}: `^{\"log\":\"\\u2028 \\u2029\",\"time\":\".{20,}\"}$`,
  20. {Log: string([]byte{0xaF})}: `^{\"log\":\"\\ufffd\",\"time\":\".{20,}\"}$`,
  21. {Log: string([]byte{0x7F})}: `^{\"log\":\"\x7f\",\"time\":\".{20,}\"}$`,
  22. }
  23. for jsonLog, expression := range logs {
  24. data, err := jsonLog.MarshalJSON()
  25. require.NoError(t, err)
  26. assert.Regexp(t, regexp.MustCompile(expression), string(data))
  27. assert.NoError(t, json.Unmarshal(data, &map[string]interface{}{}))
  28. }
  29. }