1
0

access_log_parse_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package service
  2. import "testing"
  3. func TestParseAccessLogFields(t *testing.T) {
  4. malformed := []string{
  5. "",
  6. "singletoken",
  7. "2024/01/02",
  8. "2024/01/02 15:04:05.000000 from",
  9. "2024/01/02 15:04:05.000000 accepted",
  10. "2024/01/02 15:04:05.000000 email:",
  11. }
  12. for _, line := range malformed {
  13. func() {
  14. defer func() {
  15. if r := recover(); r != nil {
  16. t.Errorf("parseAccessLogFields panicked on %q: %v", line, r)
  17. }
  18. }()
  19. _ = parseAccessLogFields(line)
  20. }()
  21. }
  22. line := "2024/01/02 15:04:05.123456 from 1.2.3.4:555 accepted tcp:example.com:443 [inbound-tag >> outbound-tag] email: [email protected]"
  23. entry := parseAccessLogFields(line)
  24. if entry.FromAddress != "1.2.3.4:555" {
  25. t.Errorf("FromAddress = %q, want %q", entry.FromAddress, "1.2.3.4:555")
  26. }
  27. if entry.ToAddress != "tcp:example.com:443" {
  28. t.Errorf("ToAddress = %q, want %q", entry.ToAddress, "tcp:example.com:443")
  29. }
  30. if entry.Inbound != "inbound-tag" {
  31. t.Errorf("Inbound = %q, want %q", entry.Inbound, "inbound-tag")
  32. }
  33. if entry.Outbound != "outbound-tag" {
  34. t.Errorf("Outbound = %q, want %q", entry.Outbound, "outbound-tag")
  35. }
  36. if entry.Email != "[email protected]" {
  37. t.Errorf("Email = %q, want %q", entry.Email, "[email protected]")
  38. }
  39. if entry.DateTime.IsZero() {
  40. t.Error("DateTime was not parsed from a well-formed line")
  41. }
  42. }