8func TestMsgWriter(t *testing.T) {
9 check := func(data string, want bool) {
12 b := &strings.Builder{}
14 if _, err := mw.Write([]byte(data)); err != nil {
15 t.Fatalf("write for message %q: %s", data, err)
17 if mw.HaveBody != want {
18 t.Fatalf("got %v, expected %v, for message %q", mw.HaveBody, want, data)
21 b = &strings.Builder{}
24 if _, err := mw.Write([]byte(data[i : i+1])); err != nil {
25 t.Fatalf("write for message %q: %s", data, err)
28 if mw.HaveBody != want {
29 t.Fatalf("got %v, expected %v, for message %q", mw.HaveBody, want, data)
33 check("no header", false)
34 check("no header\r\n", false)
35 check("key: value\r\n\r\n", true)
36 check("key: value\r\n\r\nbody", true)
37 check("key: value\n\nbody", true)
38 check("key: value\n\r\nbody", true)
39 check("key: value\r\rbody", false)
40 check("\r\n\r\n", true)
41 check("\r\n\r\nbody", true)
42 check("\r\nbody", true)
44 // Check \n is replaced with \r\n.
47 msg := "key: value\n\nline1\r\nline2\nx\n.\n"
48 _, err := mw.Write([]byte(msg))
49 tcheck(t, err, "write")
51 exp := "key: value\r\n\r\nline1\r\nline2\r\nx\r\n.\r\n"
53 t.Fatalf("got %q, expected %q", got, exp)
57func TestMsgWriterIssue117(t *testing.T) {
61 // Write header and header/body separator, but with CR missing.
62 _, err := mw.Write([]byte("a: b\n\n"))
63 tcheck(t, err, "write")
65 // Write start of a line. The newline follows in a second write. Just because of buffering.
66 _, err = mw.Write([]byte("body\r"))
67 tcheck(t, err, "write")
69 // Finish the line. The bug is that w.tail was only updated while writing headers,
70 // not while writing message data for the body. That makes the code think this \n
71 // wasn't preceded by a \r, causing it to add a \r. And we end up with \r\r\n in
73 _, err = mw.Write([]byte("\n"))
74 tcheck(t, err, "write")
77 exp := "a: b\r\n\r\nbody\r\n"
79 t.Fatalf("got %q, expected %q", got, exp)