-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer_test.go
72 lines (63 loc) · 1.68 KB
/
transformer_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
package multipart
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
)
type MockReadSeekCloser struct {
*bytes.Reader
}
func NewMockReadSeekCloser(reader *bytes.Reader) *MockReadSeekCloser {
return &MockReadSeekCloser{Reader: reader}
}
func (rc *MockReadSeekCloser) Close() error {
return nil
}
func TestTransformer(t *testing.T) {
type HttpRange struct {
contentType string
rangeStart string
rangeEnd string
fileSize string
// content string
}
type TestCase struct {
content string
rangeHeader string
// parts []*Part
expectedOut string
}
sep := "BOUNDARY"
t.Run("normal cases", func(t *testing.T) {
testCases := []*TestCase{
&TestCase{
content: "0123456789",
rangeHeader: "bytes=0-3, 8-8",
expectedOut: "\r\n--BOUNDARY\r\nContent-Type: application/octet-stream\r\nContent-Range: bytes 0-3/10\r\n\r\n0123\r\n--BOUNDARY\r\nContent-Type: application/octet-stream\r\nContent-Range: bytes 8-8/10\r\n\r\n8\r\n--BOUNDARY--",
},
}
for _, tc := range testCases {
parts, err := RangeToParts(tc.rangeHeader, "application/octet-stream", fmt.Sprintf("%d", len(tc.content)))
if err != nil {
t.Fatal(err)
}
mockFd := &MockReadSeekCloser{bytes.NewReader([]byte(tc.content))}
w := NewTransformerWithBoundary(mockFd, parts, sep)
buf := bytes.NewBuffer([]byte{})
err = w.WriteMultiParts(buf)
if err != nil {
t.Fatal(err)
}
respBytes, err := ioutil.ReadAll(buf)
if err != nil {
t.Fatal(err)
}
if string(respBytes) != tc.expectedOut {
t.Errorf("\nnot equal 1.expected(%d) 2.got(%d)", len(tc.expectedOut), len(string(respBytes)))
t.Error(tc.expectedOut)
t.Error(string(respBytes))
}
}
})
}