@cryptotaxi247 / kubo / commits / c904e6c46

commands: Added File tests

Matt Bell committed Nov 17, 2014 at 04:45 UTC c904e6c46da68bf9470eb3e1891465f58df77ac0
1 file changed +193
commands/file_test.go new
+193
@@ -0,0 +1,193 @@
1 +package commands
2 +
3 +import (
4 + "io"
5 + "mime/multipart"
6 + "strings"
7 + "testing"
8 +)
9 +
10 +func TestSliceFiles(t *testing.T) {
11 + name := "testname"
12 + files := []File{
13 + &ReaderFile{"file.txt", strings.NewReader("Some text!\n")},
14 + &ReaderFile{"beep.txt", strings.NewReader("beep")},
15 + &ReaderFile{"boop.txt", strings.NewReader("boop")},
16 + }
17 + buf := make([]byte, 20)
18 +
19 + sf := &SliceFile{name, files}
20 +
21 + if !sf.IsDirectory() {
22 + t.Error("SliceFile should always be a directory")
23 + }
24 + if n, err := sf.Read(buf); n > 0 || err != ErrNotReader {
25 + t.Error("Shouldn't be able to call `Read` on a SliceFile")
26 + }
27 + if err := sf.Close(); err != ErrNotReader {
28 + t.Error("Shouldn't be able to call `Close` on a SliceFile")
29 + }
30 +
31 + file, err := sf.NextFile()
32 + if file == nil || err != nil {
33 + t.Error("Expected a file and nil error")
34 + }
35 + read, err := file.Read(buf)
36 + if read != 11 || err != nil {
37 + t.Error("NextFile got a file in the wrong order")
38 + }
39 +
40 + file, err = sf.NextFile()
41 + if file == nil || err != nil {
42 + t.Error("Expected a file and nil error")
43 + }
44 + file, err = sf.NextFile()
45 + if file == nil || err != nil {
46 + t.Error("Expected a file and nil error")
47 + }
48 +
49 + file, err = sf.NextFile()
50 + if file != nil || err != io.EOF {
51 + t.Error("Expected a nil file and io.EOF")
52 + }
53 +}
54 +
55 +func TestReaderFiles(t *testing.T) {
56 + message := "beep boop"
57 + rf := &ReaderFile{"file.txt", strings.NewReader(message)}
58 + buf := make([]byte, len(message))
59 +
60 + if rf.IsDirectory() {
61 + t.Error("ReaderFile should never be a directory")
62 + }
63 + file, err := rf.NextFile()
64 + if file != nil || err != ErrNotDirectory {
65 + t.Error("Expected a nil file and ErrNotDirectory")
66 + }
67 +
68 + if n, err := rf.Read(buf); n == 0 || err != nil {
69 + t.Error("Expected to be able to read")
70 + }
71 + if err := rf.Close(); err != nil {
72 + t.Error("Should be able to close")
73 + }
74 + if n, err := rf.Read(buf); n != 0 || err != io.EOF {
75 + t.Error("Expected EOF when reading after close")
76 + }
77 +}
78 +
79 +func TestMultipartFiles(t *testing.T) {
80 + data := `
81 +--Boundary!
82 +Content-Type: text/plain
83 +Content-Disposition: file; filename="name"
84 +Some-Header: beep
85 +
86 +beep
87 +--Boundary!
88 +Content-Type: multipart/mixed; boundary=OtherBoundary
89 +Content-Disposition: file; filename="dir"
90 +
91 +--OtherBoundary
92 +Content-Type: text/plain
93 +Content-Disposition: file; filename="some/file/path"
94 +
95 +test
96 +--OtherBoundary
97 +Content-Type: text/plain
98 +
99 +boop
100 +--OtherBoundary
101 +Content-Type: text/plain
102 +
103 +bloop
104 +--OtherBoundary--
105 +--Boundary!--
106 +
107 +`
108 +
109 + reader := strings.NewReader(data)
110 + mpReader := multipart.NewReader(reader, "Boundary!")
111 + buf := make([]byte, 20)
112 +
113 + // test properties of a file created from the first part
114 + part, err := mpReader.NextPart()
115 + if part == nil || err != nil {
116 + t.Error("Expected non-nil part, nil error")
117 + }
118 + mpf, err := NewFileFromPart(part)
119 + if mpf == nil || err != nil {
120 + t.Error("Expected non-nil MultipartFile, nil error")
121 + }
122 + if mpf.IsDirectory() {
123 + t.Error("Expected file to not be a directory")
124 + }
125 + if mpf.FileName() != "name" {
126 + t.Error("Expected filename to be \"name\"")
127 + }
128 + if file, err := mpf.NextFile(); file != nil || err != ErrNotDirectory {
129 + t.Error("Expected a nil file and ErrNotDirectory")
130 + }
131 + if n, err := mpf.Read(buf); n != 4 || err != nil {
132 + t.Error("Expected to be able to read 4 bytes")
133 + }
134 + if err := mpf.Close(); err != nil {
135 + t.Error("Expected to be able to close file")
136 + }
137 +
138 + // test properties of file created from second part (directory)
139 + part, err = mpReader.NextPart()
140 + if part == nil || err != nil {
141 + t.Error("Expected non-nil part, nil error")
142 + }
143 + mpf, err = NewFileFromPart(part)
144 + if mpf == nil || err != nil {
145 + t.Error("Expected non-nil MultipartFile, nil error")
146 + }
147 + if !mpf.IsDirectory() {
148 + t.Error("Expected file to be a directory")
149 + }
150 + if mpf.FileName() != "dir" {
151 + t.Error("Expected filename to be \"dir\"")
152 + }
153 + if n, err := mpf.Read(buf); n > 0 || err != ErrNotReader {
154 + t.Error("Shouldn't be able to call `Read` on a directory")
155 + }
156 + if err := mpf.Close(); err != ErrNotReader {
157 + t.Error("Shouldn't be able to call `Close` on a directory")
158 + }
159 +
160 + // test properties of first child file
161 + child, err := mpf.NextFile()
162 + if child == nil || err != nil {
163 + t.Error("Expected to be able to read a child file")
164 + }
165 + if child.IsDirectory() {
166 + t.Error("Expected file to not be a directory")
167 + }
168 + if child.FileName() != "some/file/path" {
169 + t.Error("Expected filename to be \"some/file/path\"")
170 + }
171 +
172 + // test processing files out of order
173 + child, err = mpf.NextFile()
174 + if child == nil || err != nil {
175 + t.Error("Expected to be able to read a child file")
176 + }
177 + child2, err := mpf.NextFile()
178 + if child == nil || err != nil {
179 + t.Error("Expected to be able to read a child file")
180 + }
181 + if n, err := child2.Read(buf); n != 5 || err != nil {
182 + t.Error("Expected to be able to read")
183 + }
184 + if n, err := child.Read(buf); n != 0 || err == nil {
185 + t.Error("Expected to not be able to read after advancing NextFile() past this file")
186 + }
187 +
188 + // make sure the end is handled properly
189 + child, err = mpf.NextFile()
190 + if child != nil || err == nil {
191 + t.Error("Expected NextFile to return (nil, EOF)")
192 + }
193 +}