2023-01-30 16:27:06 +03:00
|
|
|
package message
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMsgWriter(t *testing.T) {
|
|
|
|
check := func(data string, want bool) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
b := &strings.Builder{}
|
2023-08-11 15:07:49 +03:00
|
|
|
mw := NewWriter(b)
|
2023-01-30 16:27:06 +03:00
|
|
|
if _, err := mw.Write([]byte(data)); err != nil {
|
|
|
|
t.Fatalf("write for message %q: %s", data, err)
|
|
|
|
}
|
2023-08-11 15:07:49 +03:00
|
|
|
if mw.HaveBody != want {
|
|
|
|
t.Fatalf("got %v, expected %v, for message %q", mw.HaveBody, want, data)
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
b = &strings.Builder{}
|
2023-08-11 15:07:49 +03:00
|
|
|
mw = NewWriter(b)
|
2023-01-30 16:27:06 +03:00
|
|
|
for i := range data {
|
|
|
|
if _, err := mw.Write([]byte(data[i : i+1])); err != nil {
|
|
|
|
t.Fatalf("write for message %q: %s", data, err)
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 15:07:49 +03:00
|
|
|
if mw.HaveBody != want {
|
|
|
|
t.Fatalf("got %v, expected %v, for message %q", mw.HaveBody, want, data)
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check("no header", false)
|
|
|
|
check("no header\r\n", false)
|
|
|
|
check("key: value\r\n\r\n", true)
|
|
|
|
check("key: value\r\n\r\nbody", true)
|
|
|
|
check("key: value\n\nbody", false)
|
|
|
|
check("key: value\r\rbody", false)
|
|
|
|
check("\r\n\r\n", true)
|
|
|
|
check("\r\n\r\nbody", true)
|
2023-08-11 15:07:49 +03:00
|
|
|
check("\r\nbody", true)
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|