forked from ipfs/go-ipfs-files
-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_test.go
142 lines (128 loc) · 2.39 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package files
import (
"io"
"mime/multipart"
"strings"
"testing"
)
func TestSliceFiles(t *testing.T) {
sf := NewMapDirectory(map[string]Node{
"1": NewBytesFile([]byte("Some text!\n")),
"2": NewBytesFile([]byte("beep")),
"3": NewBytesFile([]byte("boop")),
})
CheckDir(t, sf, []Event{
{
kind: TFile,
name: "1",
value: "Some text!\n",
},
{
kind: TFile,
name: "2",
value: "beep",
},
{
kind: TFile,
name: "3",
value: "boop",
},
})
}
func TestReaderFiles(t *testing.T) {
message := "beep boop"
rf := NewBytesFile([]byte(message))
buf := make([]byte, len(message))
if n, err := rf.Read(buf); n == 0 || err != nil {
t.Fatal("Expected to be able to read")
}
if err := rf.Close(); err != nil {
t.Fatal("Should be able to close")
}
if n, err := rf.Read(buf); n != 0 || err != io.EOF {
t.Fatal("Expected EOF when reading after close")
}
}
func TestMultipartFiles(t *testing.T) {
data := `
--Boundary!
Content-Type: text/plain
Content-Disposition: file; filename="name"
Some-Header: beep
beep
--Boundary!
Content-Type: application/x-directory
Content-Disposition: file; filename="dir"
--Boundary!
Content-Type: text/plain
Content-Disposition: file; filename="dir/nested"
some content
--Boundary!
Content-Type: application/symlink
Content-Disposition: file; filename="dir/simlynk"
anotherfile
--Boundary!
Content-Type: text/plain
Content-Disposition: file; filename="implicit1/implicit2/deep_implicit"
implicit file1
--Boundary!
Content-Type: text/plain
Content-Disposition: file; filename="implicit1/shallow_implicit"
implicit file2
--Boundary!--
`
reader := strings.NewReader(data)
mpReader := multipart.NewReader(reader, "Boundary!")
dir, err := NewFileFromPartReader(mpReader, multipartFormdataType)
if err != nil {
t.Fatal(err)
}
CheckDir(t, dir, []Event{
{
kind: TFile,
name: "name",
value: "beep",
},
{
kind: TDirStart,
name: "dir",
},
{
kind: TFile,
name: "nested",
value: "some content",
},
{
kind: TSymlink,
name: "simlynk",
value: "anotherfile",
},
{
kind: TDirEnd,
},
{
kind: TDirStart,
name: "implicit1",
},
{
kind: TDirStart,
name: "implicit2",
},
{
kind: TFile,
name: "deep_implicit",
value: "implicit file1",
},
{
kind: TDirEnd,
},
{
kind: TFile,
name: "shallow_implicit",
value: "implicit file2",
},
{
kind: TDirEnd,
},
})
}