1package message
2
3import (
4 "net/textproto"
5 "reflect"
6 "strings"
7 "testing"
8)
9
10func TestParseHeaderFields(t *testing.T) {
11 check := func(headers string, fields []string, expHdrs textproto.MIMEHeader, expErr error) {
12 t.Helper()
13
14 buffields := [][]byte{}
15 for _, f := range fields {
16 buffields = append(buffields, []byte(f))
17 }
18
19 scratches := [][]byte{
20 make([]byte, 0),
21 make([]byte, 4*1024),
22 }
23 for _, scratch := range scratches {
24 hdrs, err := ParseHeaderFields([]byte(strings.ReplaceAll(headers, "\n", "\r\n")), scratch, buffields)
25 if !reflect.DeepEqual(hdrs, expHdrs) || !reflect.DeepEqual(err, expErr) {
26 t.Fatalf("got %v %v, expected %v %v", hdrs, err, expHdrs, expErr)
27 }
28 }
29 }
30
31 check("", []string{"subject"}, textproto.MIMEHeader(nil), nil)
32 check("Subject: test\n", []string{"subject"}, textproto.MIMEHeader{"Subject": []string{"test"}}, nil)
33 check("References: <id@host>\nOther: ignored\nSubject: first\nSubject: test\n\tcontinuation\n", []string{"subject", "REFERENCES"}, textproto.MIMEHeader{"References": []string{"<id@host>"}, "Subject": []string{"first", "test continuation"}}, nil)
34 check(":\n", []string{"subject"}, textproto.MIMEHeader(nil), nil)
35 check("bad\n", []string{"subject"}, textproto.MIMEHeader(nil), nil)
36 check("subject: test\n continuation without end\n", []string{"subject"}, textproto.MIMEHeader{"Subject": []string{"test continuation without end"}}, nil)
37 check("subject: test\n", []string{"subject"}, textproto.MIMEHeader{"Subject": []string{"test"}}, nil)
38 check("subject \t: test\n", []string{"subject"}, textproto.MIMEHeader(nil), nil) // Note: In go1.20, this would be interpreted as valid "Subject" header. Not in go1.21.
39 // note: in go1.20, missing end of line would cause it to be ignored, in go1.21 it is used.
40}
41