if len(boundary) < 1 || len(boundary) > 70 {
return errors.New("mime: invalid boundary length")
}
- for _, b := range boundary {
+ end := len(boundary) - 1
+ for i, b := range boundary {
if 'A' <= b && b <= 'Z' || 'a' <= b && b <= 'z' || '0' <= b && b <= '9' {
continue
}
switch b {
case '\'', '(', ')', '+', '_', ',', '-', '.', '/', ':', '=', '?':
continue
+ case ' ':
+ if i != end {
+ continue
+ }
}
return errors.New("mime: invalid boundary character")
}
}
func TestWriterSetBoundary(t *testing.T) {
- var b bytes.Buffer
- w := NewWriter(&b)
tests := []struct {
b string
ok bool
{strings.Repeat("x", 71), false},
{"bad!ascii!", false},
{"my-separator", true},
+ {"with space", true},
+ {"badspace ", false},
}
for i, tt := range tests {
+ var b bytes.Buffer
+ w := NewWriter(&b)
err := w.SetBoundary(tt.b)
got := err == nil
if got != tt.ok {
if got != tt.b {
t.Errorf("boundary = %q; want %q", got, tt.b)
}
+ w.Close()
+ wantSub := "\r\n--" + tt.b + "--\r\n"
+ if got := b.String(); !strings.Contains(got, wantSub) {
+ t.Errorf("expected %q in output. got: %q", wantSub, got)
+ }
}
}
- w.Close()
- if got := b.String(); !strings.Contains(got, "\r\n--my-separator--\r\n") {
- t.Errorf("expected my-separator in output. got: %q", got)
- }
}
func TestWriterBoundaryGoroutines(t *testing.T) {