file_test.go 4.95 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{
Matt Bell's avatar
Matt Bell committed
14 15 16
		&ReaderFile{"file.txt", ioutil.NopCloser(strings.NewReader("Some text!\n"))},
		&ReaderFile{"beep.txt", ioutil.NopCloser(strings.NewReader("beep"))},
		&ReaderFile{"boop.txt", ioutil.NopCloser(strings.NewReader("boop"))},
Matt Bell's avatar
Matt Bell committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	}
	buf := make([]byte, 20)

	sf := &SliceFile{name, files}

	if !sf.IsDirectory() {
		t.Error("SliceFile should always be a directory")
	}
	if n, err := sf.Read(buf); n > 0 || err != ErrNotReader {
		t.Error("Shouldn't be able to call `Read` on a SliceFile")
	}
	if err := sf.Close(); err != ErrNotReader {
		t.Error("Shouldn't be able to call `Close` on a SliceFile")
	}

	file, err := sf.NextFile()
	if file == nil || err != nil {
		t.Error("Expected a file and nil error")
	}
	read, err := file.Read(buf)
	if read != 11 || err != nil {
		t.Error("NextFile got a file in the wrong order")
	}

	file, err = sf.NextFile()
	if file == nil || err != nil {
		t.Error("Expected a file and nil error")
	}
	file, err = sf.NextFile()
	if file == nil || err != nil {
		t.Error("Expected a file and nil error")
	}

	file, err = sf.NextFile()
	if file != nil || err != io.EOF {
		t.Error("Expected a nil file and io.EOF")
	}
}

func TestReaderFiles(t *testing.T) {
	message := "beep boop"
Matt Bell's avatar
Matt Bell committed
58
	rf := &ReaderFile{"file.txt", ioutil.NopCloser(strings.NewReader(message))}
Matt Bell's avatar
Matt Bell committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 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
	buf := make([]byte, len(message))

	if rf.IsDirectory() {
		t.Error("ReaderFile should never be a directory")
	}
	file, err := rf.NextFile()
	if file != nil || err != ErrNotDirectory {
		t.Error("Expected a nil file and ErrNotDirectory")
	}

	if n, err := rf.Read(buf); n == 0 || err != nil {
		t.Error("Expected to be able to read")
	}
	if err := rf.Close(); err != nil {
		t.Error("Should be able to close")
	}
	if n, err := rf.Read(buf); n != 0 || err != io.EOF {
		t.Error("Expected EOF when reading after close")
	}
}

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

beep
--Boundary!
Content-Type: multipart/mixed; boundary=OtherBoundary
Content-Disposition: file; filename="dir"

--OtherBoundary
Content-Type: text/plain
Content-Disposition: file; filename="some/file/path"

test
--OtherBoundary
Content-Type: text/plain

boop
--OtherBoundary
Content-Type: text/plain

bloop
--OtherBoundary--
--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 {
		t.Error("Expected non-nil part, nil error")
	}
	mpf, err := NewFileFromPart(part)
	if mpf == nil || err != nil {
		t.Error("Expected non-nil MultipartFile, nil error")
	}
	if mpf.IsDirectory() {
		t.Error("Expected file to not be a directory")
	}
	if mpf.FileName() != "name" {
		t.Error("Expected filename to be \"name\"")
	}
	if file, err := mpf.NextFile(); file != nil || err != ErrNotDirectory {
		t.Error("Expected a nil file and ErrNotDirectory")
	}
	if n, err := mpf.Read(buf); n != 4 || err != nil {
		t.Error("Expected to be able to read 4 bytes")
	}
	if err := mpf.Close(); err != nil {
		t.Error("Expected to be able to close file")
	}

	// test properties of file created from second part (directory)
	part, err = mpReader.NextPart()
	if part == nil || err != nil {
		t.Error("Expected non-nil part, nil error")
	}
	mpf, err = NewFileFromPart(part)
	if mpf == nil || err != nil {
		t.Error("Expected non-nil MultipartFile, nil error")
	}
	if !mpf.IsDirectory() {
		t.Error("Expected file to be a directory")
	}
	if mpf.FileName() != "dir" {
		t.Error("Expected filename to be \"dir\"")
	}
	if n, err := mpf.Read(buf); n > 0 || err != ErrNotReader {
		t.Error("Shouldn't be able to call `Read` on a directory")
	}
	if err := mpf.Close(); err != ErrNotReader {
		t.Error("Shouldn't be able to call `Close` on a directory")
	}

	// test properties of first child file
	child, err := mpf.NextFile()
	if child == nil || err != nil {
		t.Error("Expected to be able to read a child file")
	}
	if child.IsDirectory() {
		t.Error("Expected file to not be a directory")
	}
	if child.FileName() != "some/file/path" {
		t.Error("Expected filename to be \"some/file/path\"")
	}

	// test processing files out of order
	child, err = mpf.NextFile()
	if child == nil || err != nil {
		t.Error("Expected to be able to read a child file")
	}
	child2, err := mpf.NextFile()
	if child == nil || err != nil {
		t.Error("Expected to be able to read a child file")
	}
	if n, err := child2.Read(buf); n != 5 || err != nil {
		t.Error("Expected to be able to read")
	}
	if n, err := child.Read(buf); n != 0 || err == nil {
		t.Error("Expected to not be able to read after advancing NextFile() past this file")
	}

	// make sure the end is handled properly
	child, err = mpf.NextFile()
	if child != nil || err == nil {
		t.Error("Expected NextFile to return (nil, EOF)")
	}
}