(t *testing.T)
| 7 | ) |
| 8 | |
| 9 | func TestParsedBody(t *testing.T) { |
| 10 | t.Run("plain text passes through", func(t *testing.T) { |
| 11 | raw := "From: a@x.com\r\nTo: b@y.com\r\nSubject: Hi\r\nContent-Type: text/plain\r\n\r\nHello there.\r\n" |
| 12 | got, trunc := ParsedBody([]byte(raw), 0) |
| 13 | if got != "Hello there." || trunc { |
| 14 | t.Fatalf("got %q trunc=%v", got, trunc) |
| 15 | } |
| 16 | }) |
| 17 | |
| 18 | t.Run("html rendered to text", func(t *testing.T) { |
| 19 | raw := "From: a@x.com\r\nContent-Type: text/html\r\n\r\n<html><body><p>Hello</p><script>evil()</script><p>World</p></body></html>" |
| 20 | got, _ := ParsedBody([]byte(raw), 0) |
| 21 | if !strings.Contains(got, "Hello") || !strings.Contains(got, "World") { |
| 22 | t.Fatalf("expected Hello/World, got %q", got) |
| 23 | } |
| 24 | if strings.Contains(got, "evil") { |
| 25 | t.Fatalf("script content must be dropped, got %q", got) |
| 26 | } |
| 27 | }) |
| 28 | |
| 29 | t.Run("multipart prefers text/plain", func(t *testing.T) { |
| 30 | raw := "From: a@x.com\r\nContent-Type: multipart/alternative; boundary=B\r\n\r\n" + |
| 31 | "--B\r\nContent-Type: text/plain\r\n\r\nPlain version.\r\n" + |
| 32 | "--B\r\nContent-Type: text/html\r\n\r\n<p>HTML version</p>\r\n--B--\r\n" |
| 33 | got, _ := ParsedBody([]byte(raw), 0) |
| 34 | if got != "Plain version." { |
| 35 | t.Fatalf("expected plain part, got %q", got) |
| 36 | } |
| 37 | }) |
| 38 | |
| 39 | t.Run("quoted-printable decoded", func(t *testing.T) { |
| 40 | raw := "From: a@x.com\r\nContent-Type: text/plain\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\nCaf=C3=A9 time" |
| 41 | got, _ := ParsedBody([]byte(raw), 0) |
| 42 | if got != "Café time" { |
| 43 | t.Fatalf("expected decoded café, got %q", got) |
| 44 | } |
| 45 | }) |
| 46 | |
| 47 | t.Run("strips quoted reply (On ... wrote:)", func(t *testing.T) { |
| 48 | raw := "From: a@x.com\r\nContent-Type: text/plain\r\n\r\nMy reply.\r\n\r\nOn Mon, Jan 1, 2026, Bob <b@y.com> wrote:\r\n> original\r\n> more original\r\n" |
| 49 | got, _ := ParsedBody([]byte(raw), 0) |
| 50 | if got != "My reply." { |
| 51 | t.Fatalf("expected only the reply, got %q", got) |
| 52 | } |
| 53 | }) |
| 54 | |
| 55 | t.Run("strips Outlook original-message block", func(t *testing.T) { |
| 56 | raw := "From: a@x.com\r\nContent-Type: text/plain\r\n\r\nTop post.\r\n\r\n-----Original Message-----\r\nFrom: someone\r\nblah\r\n" |
| 57 | got, _ := ParsedBody([]byte(raw), 0) |
| 58 | if got != "Top post." { |
| 59 | t.Fatalf("expected top post, got %q", got) |
| 60 | } |
| 61 | }) |
| 62 | |
| 63 | t.Run("length cap truncates", func(t *testing.T) { |
| 64 | body := strings.Repeat("a", 100) |
| 65 | raw := "From: a@x.com\r\nContent-Type: text/plain\r\n\r\n" + body |
| 66 | got, trunc := ParsedBody([]byte(raw), 20) |
nothing calls this directly
no test coverage detected