commands/http: Added MultiFileReader test
Matt Bell committed
Nov 17, 2014 at 18:13 UTC
c14bd98f7a2057977f3a72b140c498637e5a8df5
1 file changed
+105
commands/http/multifilereader_test.go
new
+105
@@ -0,0 +1,105 @@
1
+package http
2
+
3
+import (
4
+ "io"
5
+ "mime/multipart"
6
+ "strings"
7
+ "testing"
8
+
9
+ cmds "github.com/jbenet/go-ipfs/commands"
10
+)
11
+
12
+func TestOutput(t *testing.T) {
13
+ text := "Some text! :)"
14
+ files := []cmds.File{
15
+ &cmds.ReaderFile{"file.txt", strings.NewReader(text)},
16
+ &cmds.SliceFile{"boop", []cmds.File{
17
+ &cmds.ReaderFile{"boop/a.txt", strings.NewReader("bleep")},
18
+ &cmds.ReaderFile{"boop/b.txt", strings.NewReader("bloop")},
19
+ }},
20
+ &cmds.ReaderFile{"beep.txt", strings.NewReader("beep")},
21
+ }
22
+ sf := &cmds.SliceFile{"", files}
23
+ buf := make([]byte, 20)
24
+
25
+ // testing output by reading it with the go stdlib "mime/multipart" Reader
26
+ mfr := NewMultiFileReader(sf, true)
27
+ mpReader := multipart.NewReader(mfr, mfr.Boundary())
28
+
29
+ part, err := mpReader.NextPart()
30
+ if part == nil || err != nil {
31
+ t.Error("Expected non-nil part, nil error")
32
+ }
33
+ mpf, err := cmds.NewFileFromPart(part)
34
+ if mpf == nil || err != nil {
35
+ t.Error("Expected non-nil MultipartFile, nil error")
36
+ }
37
+ if mpf.IsDirectory() {
38
+ t.Error("Expected file to not be a directory")
39
+ }
40
+ if mpf.FileName() != "file.txt" {
41
+ t.Error("Expected filename to be \"file.txt\"")
42
+ }
43
+ if n, err := mpf.Read(buf); n != len(text) || err != nil {
44
+ t.Error("Expected to read from file", n, err)
45
+ }
46
+ if string(buf[:len(text)]) != text {
47
+ t.Error("Data read was different than expected")
48
+ }
49
+
50
+ part, err = mpReader.NextPart()
51
+ if part == nil || err != nil {
52
+ t.Error("Expected non-nil part, nil error")
53
+ }
54
+ mpf, err = cmds.NewFileFromPart(part)
55
+ if mpf == nil || err != nil {
56
+ t.Error("Expected non-nil MultipartFile, nil error")
57
+ }
58
+ if !mpf.IsDirectory() {
59
+ t.Error("Expected file to be a directory")
60
+ }
61
+ if mpf.FileName() != "boop" {
62
+ t.Error("Expected filename to be \"boop\"")
63
+ }
64
+
65
+ child, err := mpf.NextFile()
66
+ if child == nil || err != nil {
67
+ t.Error("Expected to be able to read a child file")
68
+ }
69
+ if child.IsDirectory() {
70
+ t.Error("Expected file to not be a directory")
71
+ }
72
+ if child.FileName() != "boop/a.txt" {
73
+ t.Error("Expected filename to be \"some/file/path\"")
74
+ }
75
+
76
+ child, err = mpf.NextFile()
77
+ if child == nil || err != nil {
78
+ t.Error("Expected to be able to read a child file")
79
+ }
80
+ if child.IsDirectory() {
81
+ t.Error("Expected file to not be a directory")
82
+ }
83
+ if child.FileName() != "boop/b.txt" {
84
+ t.Error("Expected filename to be \"some/file/path\"")
85
+ }
86
+
87
+ child, err = mpf.NextFile()
88
+ if child != nil || err != io.EOF {
89
+ t.Error("Expected to get (nil, io.EOF)")
90
+ }
91
+
92
+ part, err = mpReader.NextPart()
93
+ if part == nil || err != nil {
94
+ t.Error("Expected non-nil part, nil error")
95
+ }
96
+ mpf, err = cmds.NewFileFromPart(part)
97
+ if mpf == nil || err != nil {
98
+ t.Error("Expected non-nil MultipartFile, nil error")
99
+ }
100
+
101
+ part, err = mpReader.NextPart()
102
+ if part != nil || err != io.EOF {
103
+ t.Error("Expected to get (nil, io.EOF)")
104
+ }
105
+}