-
Notifications
You must be signed in to change notification settings - Fork 1
/
localfilefetcher_test.go
73 lines (59 loc) · 1.35 KB
/
localfilefetcher_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"io/ioutil"
"log"
"testing"
)
func NewTestFetcher(filename string) FileFetcher {
// TODO: delete tmp dir
tmpDir, _ := ioutil.TempDir("", "webserver_test")
_ = ioutil.WriteFile(
tmpDir+"/"+filename,
[]byte("abc"),
0600)
log.Printf("tmpDir: %v", tmpDir)
return NewLocalFileFetcher(tmpDir)
}
func TestGetFile(t *testing.T) {
testFile := "testFile"
fetcher := NewTestFetcher(testFile)
file, err := fetcher.GetFile(testFile)
if err != nil {
t.Errorf("GetFile failed: %v", err)
}
buf := make([]byte, 4)
n, err := file.Payload.Read(buf)
if err != nil || n != 3 {
t.Errorf("bad payload: %v, err: %v", buf, err)
}
if buf[0] == 'a' && buf[1] == 'b' && buf[2] == '3' {
t.Errorf("bad payload: %v", buf)
}
}
func TestListDir(t *testing.T) {
testFile := "testFile"
fetcher := NewTestFetcher(testFile)
tmp, err := fetcher.ListDir("")
if err != nil || len(tmp) != 1 {
t.Errorf("failed ListDir: %v", tmp)
return
}
if tmp[0].Name != testFile {
t.Errorf("wrong file found: %v", tmp[0])
return
}
tmp, err = fetcher.ListDir("tes")
if err != nil || len(tmp) != 1 {
t.Errorf("failed ListDir: %v", tmp)
return
}
if tmp[0].Name != testFile {
t.Errorf("wrong file found: %v", tmp[0])
return
}
tmp, err = fetcher.ListDir("Tes")
if err != nil || len(tmp) != 0 {
t.Errorf("failed ListDir: %v", tmp)
return
}
}