10func TestParseHeaderFields(t *testing.T) {
11 check := func(headers string, fields []string, expHdrs textproto.MIMEHeader, expErr error) {
14 buffields := [][]byte{}
15 for _, f := range fields {
16 buffields = append(buffields, []byte(f))
19 scratches := [][]byte{
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)
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.