file_test.go 5.33 KB
Newer Older
1
package files
Matt Bell's avatar
Matt Bell committed
2 3 4

import (
	"io"
Matt Bell's avatar
Matt Bell committed
5
	"io/ioutil"
Matt Bell's avatar
Matt Bell committed
6 7 8 9 10 11 12 13
	"mime/multipart"
	"strings"
	"testing"
)

func TestSliceFiles(t *testing.T) {
	name := "testname"
	files := []File{
14 15 16
		NewReaderFile("file.txt", "file.txt", ioutil.NopCloser(strings.NewReader("Some text!\n")), nil),
		NewReaderFile("beep.txt", "beep.txt", ioutil.NopCloser(strings.NewReader("beep")), nil),
		NewReaderFile("boop.txt", "boop.txt", ioutil.NopCloser(strings.NewReader("boop")), nil),
Matt Bell's avatar
Matt Bell committed
17 18 19
	}
	buf := make([]byte, 20)

20
	sf := NewSliceFile(name, name, files)
Matt Bell's avatar
Matt Bell committed
21 22

	if !sf.IsDirectory() {
Jeromy's avatar
Jeromy committed
23
		t.Fatal("SliceFile should always be a directory")
Matt Bell's avatar
Matt Bell committed
24
	}
Jeromy's avatar
Jeromy committed
25 26 27

	if n, err := sf.Read(buf); n > 0 || err != io.EOF {
		t.Fatal("Shouldn't be able to read data from a SliceFile")
Matt Bell's avatar
Matt Bell committed
28
	}
Jeromy's avatar
Jeromy committed
29

Matt Bell's avatar
Matt Bell committed
30
	if err := sf.Close(); err != ErrNotReader {
Jeromy's avatar
Jeromy committed
31
		t.Fatal("Shouldn't be able to call `Close` on a SliceFile")
Matt Bell's avatar
Matt Bell committed
32 33 34 35
	}

	file, err := sf.NextFile()
	if file == nil || err != nil {
Jeromy's avatar
Jeromy committed
36
		t.Fatal("Expected a file and nil error")
Matt Bell's avatar
Matt Bell committed
37 38 39
	}
	read, err := file.Read(buf)
	if read != 11 || err != nil {
Jeromy's avatar
Jeromy committed
40
		t.Fatal("NextFile got a file in the wrong order")
Matt Bell's avatar
Matt Bell committed
41 42 43 44
	}

	file, err = sf.NextFile()
	if file == nil || err != nil {
Jeromy's avatar
Jeromy committed
45
		t.Fatal("Expected a file and nil error")
Matt Bell's avatar
Matt Bell committed
46 47 48
	}
	file, err = sf.NextFile()
	if file == nil || err != nil {
Jeromy's avatar
Jeromy committed
49
		t.Fatal("Expected a file and nil error")
Matt Bell's avatar
Matt Bell committed
50 51 52 53
	}

	file, err = sf.NextFile()
	if file != nil || err != io.EOF {
Jeromy's avatar
Jeromy committed
54
		t.Fatal("Expected a nil file and io.EOF")
Matt Bell's avatar
Matt Bell committed
55 56 57 58 59
	}
}

func TestReaderFiles(t *testing.T) {
	message := "beep boop"
60
	rf := NewReaderFile("file.txt", "file.txt", ioutil.NopCloser(strings.NewReader(message)), nil)
Matt Bell's avatar
Matt Bell committed
61 62 63
	buf := make([]byte, len(message))

	if rf.IsDirectory() {
Jeromy's avatar
Jeromy committed
64
		t.Fatal("ReaderFile should never be a directory")
Matt Bell's avatar
Matt Bell committed
65 66 67
	}
	file, err := rf.NextFile()
	if file != nil || err != ErrNotDirectory {
Jeromy's avatar
Jeromy committed
68
		t.Fatal("Expected a nil file and ErrNotDirectory")
Matt Bell's avatar
Matt Bell committed
69 70 71
	}

	if n, err := rf.Read(buf); n == 0 || err != nil {
Jeromy's avatar
Jeromy committed
72
		t.Fatal("Expected to be able to read")
Matt Bell's avatar
Matt Bell committed
73 74
	}
	if err := rf.Close(); err != nil {
Jeromy's avatar
Jeromy committed
75
		t.Fatal("Should be able to close")
Matt Bell's avatar
Matt Bell committed
76 77
	}
	if n, err := rf.Read(buf); n != 0 || err != io.EOF {
Jeromy's avatar
Jeromy committed
78
		t.Fatal("Expected EOF when reading after close")
Matt Bell's avatar
Matt Bell committed
79 80 81 82 83 84 85 86 87 88 89 90
	}
}

func TestMultipartFiles(t *testing.T) {
	data := `
--Boundary!
Content-Type: text/plain
Content-Disposition: file; filename="name"
Some-Header: beep

beep
--Boundary!
Jeromy's avatar
Jeromy committed
91
Content-Type: application/x-directory
Matt Bell's avatar
Matt Bell committed
92 93
Content-Disposition: file; filename="dir"

Jeromy's avatar
Jeromy committed
94 95 96 97 98 99 100 101 102 103
--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
Matt Bell's avatar
Matt Bell committed
104 105 106 107 108 109 110 111 112 113 114
--Boundary!--

`

	reader := strings.NewReader(data)
	mpReader := multipart.NewReader(reader, "Boundary!")
	buf := make([]byte, 20)

	// test properties of a file created from the first part
	part, err := mpReader.NextPart()
	if part == nil || err != nil {
Jeromy's avatar
Jeromy committed
115
		t.Fatal("Expected non-nil part, nil error")
Matt Bell's avatar
Matt Bell committed
116 117 118
	}
	mpf, err := NewFileFromPart(part)
	if mpf == nil || err != nil {
Jeromy's avatar
Jeromy committed
119
		t.Fatal("Expected non-nil MultipartFile, nil error")
Matt Bell's avatar
Matt Bell committed
120 121
	}
	if mpf.IsDirectory() {
Jeromy's avatar
Jeromy committed
122
		t.Fatal("Expected file to not be a directory")
Matt Bell's avatar
Matt Bell committed
123 124
	}
	if mpf.FileName() != "name" {
Jeromy's avatar
Jeromy committed
125
		t.Fatal("Expected filename to be \"name\"")
Matt Bell's avatar
Matt Bell committed
126 127
	}
	if file, err := mpf.NextFile(); file != nil || err != ErrNotDirectory {
Jeromy's avatar
Jeromy committed
128
		t.Fatal("Expected a nil file and ErrNotDirectory")
Matt Bell's avatar
Matt Bell committed
129 130
	}
	if n, err := mpf.Read(buf); n != 4 || err != nil {
Jeromy's avatar
Jeromy committed
131
		t.Fatal("Expected to be able to read 4 bytes")
Matt Bell's avatar
Matt Bell committed
132 133
	}
	if err := mpf.Close(); err != nil {
Jeromy's avatar
Jeromy committed
134
		t.Fatal("Expected to be able to close file")
Matt Bell's avatar
Matt Bell committed
135 136 137 138 139
	}

	// test properties of file created from second part (directory)
	part, err = mpReader.NextPart()
	if part == nil || err != nil {
Jeromy's avatar
Jeromy committed
140
		t.Fatal("Expected non-nil part, nil error")
Matt Bell's avatar
Matt Bell committed
141 142 143
	}
	mpf, err = NewFileFromPart(part)
	if mpf == nil || err != nil {
Jeromy's avatar
Jeromy committed
144
		t.Fatal("Expected non-nil MultipartFile, nil error")
Matt Bell's avatar
Matt Bell committed
145 146
	}
	if !mpf.IsDirectory() {
Jeromy's avatar
Jeromy committed
147
		t.Fatal("Expected file to be a directory")
Matt Bell's avatar
Matt Bell committed
148 149
	}
	if mpf.FileName() != "dir" {
Jeromy's avatar
Jeromy committed
150
		t.Fatal("Expected filename to be \"dir\"")
Matt Bell's avatar
Matt Bell committed
151 152
	}
	if n, err := mpf.Read(buf); n > 0 || err != ErrNotReader {
Jeromy's avatar
Jeromy committed
153
		t.Fatal("Shouldn't be able to call `Read` on a directory")
Matt Bell's avatar
Matt Bell committed
154 155
	}
	if err := mpf.Close(); err != ErrNotReader {
Jeromy's avatar
Jeromy committed
156
		t.Fatal("Shouldn't be able to call `Close` on a directory")
Matt Bell's avatar
Matt Bell committed
157 158
	}

Jeromy's avatar
Jeromy committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	// test properties of file created from third part (nested file)
	part, err = mpReader.NextPart()
	if part == nil || err != nil {
		t.Fatal("Expected non-nil part, nil error")
	}
	mpf, err = NewFileFromPart(part)
	if mpf == nil || err != nil {
		t.Fatal("Expected non-nil MultipartFile, nil error")
	}
	if mpf.IsDirectory() {
		t.Fatal("Expected file, got directory")
	}
	if mpf.FileName() != "dir/nested" {
		t.Fatalf("Expected filename to be \"nested\", got %s", mpf.FileName())
	}
	if n, err := mpf.Read(buf); n != 12 || err != nil {
		t.Fatalf("expected to be able to read 12 bytes from file: %s (got %d)", err, n)
	}
	if err := mpf.Close(); err != nil {
		t.Fatal("should be able to close file: %s", err)
	}

	// test properties of symlink created from fourth part (symlink)
	part, err = mpReader.NextPart()
	if part == nil || err != nil {
		t.Fatal("Expected non-nil part, nil error")
	}
	mpf, err = NewFileFromPart(part)
	if mpf == nil || err != nil {
		t.Fatal("Expected non-nil MultipartFile, nil error")
	}
	if mpf.IsDirectory() {
		t.Fatal("Expected file to be a symlink")
	}
	if mpf.FileName() != "dir/simlynk" {
		t.Fatal("Expected filename to be \"dir/simlynk\"")
	}
	slink, ok := mpf.(*Symlink)
	if !ok {
		t.Fatalf("expected file to be a symlink")
	}
	if slink.Target != "anotherfile" {
		t.Fatal("expected link to point to anotherfile")
	}
Matt Bell's avatar
Matt Bell committed
203
}