file_test.go 3.8 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 94 95 96 97 98 99 100 101 102 103 104
Content-Disposition: file; filename="dir"

--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
105
		t.Fatal("Expected non-nil part, nil error")
Matt Bell's avatar
Matt Bell committed
106 107 108
	}
	mpf, err := NewFileFromPart(part)
	if mpf == nil || err != nil {
Jeromy's avatar
Jeromy committed
109
		t.Fatal("Expected non-nil MultipartFile, nil error")
Matt Bell's avatar
Matt Bell committed
110 111
	}
	if mpf.IsDirectory() {
Jeromy's avatar
Jeromy committed
112
		t.Fatal("Expected file to not be a directory")
Matt Bell's avatar
Matt Bell committed
113 114
	}
	if mpf.FileName() != "name" {
Jeromy's avatar
Jeromy committed
115
		t.Fatal("Expected filename to be \"name\"")
Matt Bell's avatar
Matt Bell committed
116 117
	}
	if file, err := mpf.NextFile(); file != nil || err != ErrNotDirectory {
Jeromy's avatar
Jeromy committed
118
		t.Fatal("Expected a nil file and ErrNotDirectory")
Matt Bell's avatar
Matt Bell committed
119 120
	}
	if n, err := mpf.Read(buf); n != 4 || err != nil {
Jeromy's avatar
Jeromy committed
121
		t.Fatal("Expected to be able to read 4 bytes")
Matt Bell's avatar
Matt Bell committed
122 123
	}
	if err := mpf.Close(); err != nil {
Jeromy's avatar
Jeromy committed
124
		t.Fatal("Expected to be able to close file")
Matt Bell's avatar
Matt Bell committed
125 126 127 128 129
	}

	// test properties of file created from second part (directory)
	part, err = mpReader.NextPart()
	if part == nil || err != nil {
Jeromy's avatar
Jeromy committed
130
		t.Fatal("Expected non-nil part, nil error")
Matt Bell's avatar
Matt Bell committed
131 132 133
	}
	mpf, err = NewFileFromPart(part)
	if mpf == nil || err != nil {
Jeromy's avatar
Jeromy committed
134
		t.Fatal("Expected non-nil MultipartFile, nil error")
Matt Bell's avatar
Matt Bell committed
135 136
	}
	if !mpf.IsDirectory() {
Jeromy's avatar
Jeromy committed
137
		t.Fatal("Expected file to be a directory")
Matt Bell's avatar
Matt Bell committed
138 139
	}
	if mpf.FileName() != "dir" {
Jeromy's avatar
Jeromy committed
140
		t.Fatal("Expected filename to be \"dir\"")
Matt Bell's avatar
Matt Bell committed
141 142
	}
	if n, err := mpf.Read(buf); n > 0 || err != ErrNotReader {
Jeromy's avatar
Jeromy committed
143
		t.Fatal("Shouldn't be able to call `Read` on a directory")
Matt Bell's avatar
Matt Bell committed
144 145
	}
	if err := mpf.Close(); err != ErrNotReader {
Jeromy's avatar
Jeromy committed
146
		t.Fatal("Shouldn't be able to call `Close` on a directory")
Matt Bell's avatar
Matt Bell committed
147 148 149
	}

}