github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/commands/http/multifilereader_test.go (about)

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