OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / ipfs / go-ipfs-files / file_test.go
1 package files
2
3 import (
4         "io"
5         "mime/multipart"
6         "strings"
7         "testing"
8 )
9
10 func TestSliceFiles(t *testing.T) {
11         sf := NewMapDirectory(map[string]Node{
12                 "1": NewBytesFile([]byte("Some text!\n")),
13                 "2": NewBytesFile([]byte("beep")),
14                 "3": NewBytesFile([]byte("boop")),
15         })
16
17         CheckDir(t, sf, []Event{
18                 {
19                         kind:  TFile,
20                         name:  "1",
21                         value: "Some text!\n",
22                 },
23                 {
24                         kind:  TFile,
25                         name:  "2",
26                         value: "beep",
27                 },
28                 {
29                         kind:  TFile,
30                         name:  "3",
31                         value: "boop",
32                 },
33         })
34 }
35
36 func TestReaderFiles(t *testing.T) {
37         message := "beep boop"
38         rf := NewBytesFile([]byte(message))
39         buf := make([]byte, len(message))
40
41         if n, err := rf.Read(buf); n == 0 || err != nil {
42                 t.Fatal("Expected to be able to read")
43         }
44         if err := rf.Close(); err != nil {
45                 t.Fatal("Should be able to close")
46         }
47         if n, err := rf.Read(buf); n != 0 || err != io.EOF {
48                 t.Fatal("Expected EOF when reading after close")
49         }
50 }
51 func TestMultipartFiles(t *testing.T) {
52         data := `
53 --Boundary!
54 Content-Type: text/plain
55 Content-Disposition: file; filename="name"
56 Some-Header: beep
57
58 beep
59 --Boundary!
60 Content-Type: application/x-directory
61 Content-Disposition: file; filename="dir"
62
63 --Boundary!
64 Content-Type: text/plain
65 Content-Disposition: file; filename="dir/nested"
66
67 some content
68 --Boundary!
69 Content-Type: application/symlink
70 Content-Disposition: file; filename="dir/simlynk"
71
72 anotherfile
73 --Boundary!
74 Content-Type: text/plain
75 Content-Disposition: file; filename="implicit1/implicit2/deep_implicit"
76
77 implicit file1
78 --Boundary!
79 Content-Type: text/plain
80 Content-Disposition: file; filename="implicit1/shallow_implicit"
81
82 implicit file2
83 --Boundary!--
84
85 `
86
87         reader := strings.NewReader(data)
88         mpReader := multipart.NewReader(reader, "Boundary!")
89         dir, err := NewFileFromPartReader(mpReader, multipartFormdataType)
90         if err != nil {
91                 t.Fatal(err)
92         }
93
94         CheckDir(t, dir, []Event{
95                 {
96                         kind:  TFile,
97                         name:  "name",
98                         value: "beep",
99                 },
100                 {
101                         kind: TDirStart,
102                         name: "dir",
103                 },
104                 {
105                         kind:  TFile,
106                         name:  "nested",
107                         value: "some content",
108                 },
109                 {
110                         kind:  TSymlink,
111                         name:  "simlynk",
112                         value: "anotherfile",
113                 },
114                 {
115                         kind: TDirEnd,
116                 },
117                 {
118                         kind: TDirStart,
119                         name: "implicit1",
120                 },
121                 {
122                         kind: TDirStart,
123                         name: "implicit2",
124                 },
125                 {
126                         kind:  TFile,
127                         name:  "deep_implicit",
128                         value: "implicit file1",
129                 },
130                 {
131                         kind: TDirEnd,
132                 },
133                 {
134                         kind:  TFile,
135                         name:  "shallow_implicit",
136                         value: "implicit file2",
137                 },
138                 {
139                         kind: TDirEnd,
140                 },
141         })
142 }