File: writer_test.go

package info (click to toggle)
golang-github-emersion-go-message 0.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 348 kB
  • sloc: makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,333 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package message

import (
	"bytes"
	"io"
	"testing"
)

func TestWriter_multipartWithoutCreatePart(t *testing.T) {
	var h Header
	h.Set("Content-Type", "multipart/alternative; boundary=IMTHEBOUNDARY")

	var b bytes.Buffer
	mw, err := CreateWriter(&b, h)
	if err != nil {
		t.Fatal("Expected no error while creating message writer, got:", err)
	}

	io.WriteString(mw, testMultipartBody)
	mw.Close()

	if s := b.String(); s != testMultipartText {
		t.Errorf("Expected output to be \n%s\n but go \n%s", testMultipartText, s)
	}
}

func TestWriter_multipartWithoutBoundary(t *testing.T) {
	var h Header
	h.Set("Content-Type", "multipart/alternative")

	var b bytes.Buffer
	mw, err := CreateWriter(&b, h)
	if err != nil {
		t.Fatal("Expected no error while creating message writer, got:", err)
	}
	mw.Close()

	e, err := Read(&b)
	if err != nil {
		t.Fatal("Expected no error while reading message, got:", err)
	}

	mediaType, mediaParams, err := e.Header.ContentType()
	if err != nil {
		t.Fatal("Expected no error while parsing Content-Type, got:", err)
	} else if mediaType != "multipart/alternative" {
		t.Errorf("Expected media type to be %q, but got %q", "multipart/alternative", mediaType)
	} else if boundary, ok := mediaParams["boundary"]; !ok || boundary == "" {
		t.Error("Expected boundary to be automatically generated")
	}
}