-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_test.go
125 lines (111 loc) · 3.73 KB
/
file_test.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package e5e_test
import (
_ "embed"
"encoding/json"
"io"
"strings"
"testing"
"go.anx.io/e5e/v2"
)
//go:embed testdata/binary_request_with_multiple_files.json
var binaryRequestWithMultipleFiles []byte
func TestFile(t *testing.T) {
t.Parallel()
t.Run("SetText encodes the content properly", func(t *testing.T) {
t.Parallel()
f := &e5e.File{}
_ = f.SetPlainText("Hello world!")
Equal(t, "utf-8", f.Charset, "Charset does not match")
Equal(t, 12, int(f.SizeInBytes), "file size does not match")
Equal(t, "text/plain", f.ContentType, "content type does not match")
var encodedBytes = []byte{72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33}
DeepEqual(t, encodedBytes, f.Bytes(), "bytes do not match")
})
t.Run("JSON serialization matches expectation", func(t *testing.T) {
t.Parallel()
var expect = `{"binary":"SGVsbG8gd29ybGQh","type":"binary","size":12,"content_type":"text/plain","charset":"utf-8"}`
f := &e5e.File{}
_, err := f.Write([]byte("Hello world!"))
if err != nil {
t.Errorf("expected no write error, got: %v", err)
}
// One test with the pointer
actual, err := json.Marshal(f)
if err != nil {
t.Errorf("JSON marshalling failed: %v", err)
}
Equal(t, expect, string(actual), "JSON does not match")
// And one without it
actual, err = json.Marshal(*f)
if err != nil {
t.Errorf("JSON marshalling failed: %v", err)
}
Equal(t, expect, string(actual), "JSON does not match")
})
t.Run("JSON deserialization works", func(t *testing.T) {
t.Parallel()
var expected = e5e.File{
Type: "binary",
SizeInBytes: 12,
Name: "my-file-1.name",
ContentType: "application/my-content-type-1",
Charset: "utf-8",
}
expected.SetPlainText("Hello world!")
expected.ContentType = "application/my-content-type-1"
const input = `{
"binary": "SGVsbG8gd29ybGQh",
"type": "binary",
"name": "my-file-1.name",
"size": 12,
"content_type": "application/my-content-type-1",
"charset": "utf-8"
}`
var actual e5e.File
if err := json.Unmarshal([]byte(input), &actual); err != nil {
t.Errorf("JSON unmarshaling failed: %v", err)
}
DeepEqual(t, expected, actual, "files do not match")
})
t.Run("original slice is ignored", func(t *testing.T) {
var original = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
var modified = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
f := &e5e.File{}
n, _ := f.Write(modified)
Equal(t, 9, n, "written bytes do not match")
modified = append(modified, 10)
DeepEqual(t, f.Bytes(), original, "slice got passed by reference")
})
t.Run("request can be deserialized", func(t *testing.T) {
request := e5e.Request[[]e5e.File, any]{}
if err := json.Unmarshal(binaryRequestWithMultipleFiles, &request); err != nil {
t.Errorf("JSON unmarshaling failed: %v", err)
}
Equal(t, 2, len(request.Data()), "expected two files")
for _, file := range request.Data() {
Equal(t, "binary", file.Type, "file type does not match")
Equal(t, 12, file.SizeInBytes, "file size does not match")
Equal(t, "utf-8", file.Charset, "charset does not match")
if !strings.HasPrefix(file.ContentType, "application/my-content-type") {
t.Errorf("invalid content type prefix, got: %s", file.ContentType)
}
if !strings.HasPrefix(file.Name, "my-file-") {
t.Errorf("invalid name prefix, got: %s", file.Name)
}
}
})
t.Run("file can be read", func(t *testing.T) {
t.Parallel()
file := &e5e.File{}
if err := file.SetPlainText("Hello world!"); err != nil {
t.Errorf("setting file content failed: %v", err)
}
var buf strings.Builder
n, err := io.Copy(&buf, file)
if err != nil {
t.Errorf("copying failed: %v", err)
}
Equal(t, 12, n, "read bytes do not match")
Equal(t, "Hello world!", buf.String(), "file content does not match")
})
}