multifilereader_test.go 3.2 KB
Newer Older
1 2 3 4
package http

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

Steven Allen's avatar
Steven Allen committed
10
	"gx/ipfs/QmUyfy4QSr3NXym4etEiRyxBLqqAeKHJuRdi8AACxg63fZ/go-ipfs-cmdkit/files"
11 12 13 14
)

func TestOutput(t *testing.T) {
	text := "Some text! :)"
15
	fileset := []files.File{
16 17 18 19
		files.NewReaderFile("file.txt", "file.txt", ioutil.NopCloser(strings.NewReader(text)), nil),
		files.NewSliceFile("boop", "boop", []files.File{
			files.NewReaderFile("boop/a.txt", "boop/a.txt", ioutil.NopCloser(strings.NewReader("bleep")), nil),
			files.NewReaderFile("boop/b.txt", "boop/b.txt", ioutil.NopCloser(strings.NewReader("bloop")), nil),
20
		}),
21
		files.NewReaderFile("beep.txt", "beep.txt", ioutil.NopCloser(strings.NewReader("beep")), nil),
22
	}
23
	sf := files.NewSliceFile("", "", fileset)
24 25 26 27 28 29 30 31
	buf := make([]byte, 20)

	// testing output by reading it with the go stdlib "mime/multipart" Reader
	mfr := NewMultiFileReader(sf, true)
	mpReader := multipart.NewReader(mfr, mfr.Boundary())

	part, err := mpReader.NextPart()
	if part == nil || err != nil {
Jeromy's avatar
Jeromy committed
32
		t.Fatal("Expected non-nil part, nil error")
33
	}
34
	mpf, err := files.NewFileFromPart(part)
35
	if mpf == nil || err != nil {
Jeromy's avatar
Jeromy committed
36
		t.Fatal("Expected non-nil MultipartFile, nil error")
37 38
	}
	if mpf.IsDirectory() {
Jeromy's avatar
Jeromy committed
39
		t.Fatal("Expected file to not be a directory")
40 41
	}
	if mpf.FileName() != "file.txt" {
Jeromy's avatar
Jeromy committed
42
		t.Fatal("Expected filename to be \"file.txt\"")
43 44
	}
	if n, err := mpf.Read(buf); n != len(text) || err != nil {
Jeromy's avatar
Jeromy committed
45
		t.Fatal("Expected to read from file", n, err)
46 47
	}
	if string(buf[:len(text)]) != text {
Jeromy's avatar
Jeromy committed
48
		t.Fatal("Data read was different than expected")
49 50 51 52
	}

	part, err = mpReader.NextPart()
	if part == nil || err != nil {
Jeromy's avatar
Jeromy committed
53
		t.Fatal("Expected non-nil part, nil error")
54
	}
55
	mpf, err = files.NewFileFromPart(part)
56
	if mpf == nil || err != nil {
Jeromy's avatar
Jeromy committed
57
		t.Fatal("Expected non-nil MultipartFile, nil error")
58 59
	}
	if !mpf.IsDirectory() {
Jeromy's avatar
Jeromy committed
60
		t.Fatal("Expected file to be a directory")
61 62
	}
	if mpf.FileName() != "boop" {
Jeromy's avatar
Jeromy committed
63
		t.Fatal("Expected filename to be \"boop\"")
64 65
	}

Jeromy's avatar
Jeromy committed
66 67 68 69 70
	part, err = mpReader.NextPart()
	if part == nil || err != nil {
		t.Fatal("Expected non-nil part, nil error")
	}
	child, err := files.NewFileFromPart(part)
71
	if child == nil || err != nil {
Jeromy's avatar
Jeromy committed
72
		t.Fatal("Expected to be able to read a child file")
73 74
	}
	if child.IsDirectory() {
Jeromy's avatar
Jeromy committed
75
		t.Fatal("Expected file to not be a directory")
76 77
	}
	if child.FileName() != "boop/a.txt" {
Jeromy's avatar
Jeromy committed
78
		t.Fatal("Expected filename to be \"some/file/path\"")
79 80
	}

Jeromy's avatar
Jeromy committed
81 82 83 84 85
	part, err = mpReader.NextPart()
	if part == nil || err != nil {
		t.Fatal("Expected non-nil part, nil error")
	}
	child, err = files.NewFileFromPart(part)
86
	if child == nil || err != nil {
Jeromy's avatar
Jeromy committed
87
		t.Fatal("Expected to be able to read a child file")
88 89
	}
	if child.IsDirectory() {
Jeromy's avatar
Jeromy committed
90
		t.Fatal("Expected file to not be a directory")
91 92
	}
	if child.FileName() != "boop/b.txt" {
Jeromy's avatar
Jeromy committed
93
		t.Fatal("Expected filename to be \"some/file/path\"")
94 95 96 97
	}

	child, err = mpf.NextFile()
	if child != nil || err != io.EOF {
Jeromy's avatar
Jeromy committed
98
		t.Fatal("Expected to get (nil, io.EOF)")
99 100 101 102
	}

	part, err = mpReader.NextPart()
	if part == nil || err != nil {
Jeromy's avatar
Jeromy committed
103
		t.Fatal("Expected non-nil part, nil error")
104
	}
105
	mpf, err = files.NewFileFromPart(part)
106
	if mpf == nil || err != nil {
Jeromy's avatar
Jeromy committed
107
		t.Fatal("Expected non-nil MultipartFile, nil error")
108 109 110 111
	}

	part, err = mpReader.NextPart()
	if part != nil || err != io.EOF {
Jeromy's avatar
Jeromy committed
112
		t.Fatal("Expected to get (nil, io.EOF)")
113 114
	}
}