-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_test.go
122 lines (102 loc) · 3.16 KB
/
list_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
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
package main
import (
"testing"
"os"
"path/filepath"
"sort"
"strings"
"net/http"
)
func TestListFiles(t *testing.T) {
dir, err := os.MkdirTemp("", "")
if (err != nil) {
t.Fatalf("failed to create a temporary directory; %v", err)
}
path := filepath.Join(dir, "A")
err = os.WriteFile(path, []byte(""), 0644)
if err != nil {
t.Fatalf("failed to create a mock file; %v", err)
}
subdir := filepath.Join(dir, "sub")
err = os.Mkdir(subdir, 0755)
if err != nil {
t.Fatalf("failed to create a temporary subdirectory; %v", err)
}
subpath := filepath.Join(subdir, "B")
err = os.WriteFile(subpath, []byte(""), 0644)
if err != nil {
t.Fatalf("failed to create a mock file; %v", err)
}
t.Run("simple", func(t *testing.T) {
// Checking that we pull out all the files.
all, err := listFiles(dir, true)
if (err != nil) {
t.Fatal(err)
}
sort.Strings(all)
if len(all) != 2 || all[0] != "A" || all[1] != "sub/B" {
t.Errorf("unexpected results from the listing (%q)", all)
}
// Checking that the directories are properly listed.
all, err = listFiles(dir, false)
if (err != nil) {
t.Fatal(err)
}
sort.Strings(all)
if len(all) != 2 || all[0] != "A" || all[1] != "sub/" {
t.Errorf("unexpected results from the listing (%q)", all)
}
})
t.Run("handler", func(t *testing.T) {
{
r, err := http.NewRequest("GET", "/list?path=sub", nil)
if err != nil {
t.Fatal(err)
}
all, err := listFilesHandler(r, dir)
if (err != nil) {
t.Fatal(err)
}
if len(all) != 1 || all[0] != "B" {
t.Errorf("unexpected results from the listing (%q)", all)
}
}
{
r, err := http.NewRequest("GET", "/list?recursive=true", nil)
if err != nil {
t.Fatal(err)
}
all, err := listFilesHandler(r, dir)
if (err != nil) {
t.Fatal(err)
}
sort.Strings(all)
if len(all) != 2 || all[0] != "A" || all[1] != "sub/B" {
t.Errorf("unexpected results from the listing (%q)", all)
}
}
{
r, err := http.NewRequest("GET", "/list?path=..", nil)
if err != nil {
t.Fatal(err)
}
_, err = listFilesHandler(r, dir)
if err == nil || !strings.Contains(err.Error(), "not local") {
t.Fatal("expected failure for non-local paths")
}
}
{
r, err := http.NewRequest("GET", "/list?path=sub%2F", nil)
if err != nil {
t.Fatal(err)
}
all, err := listFilesHandler(r, dir)
if err != nil {
t.Fatal(err)
}
if len(all) != 1 || all[0] != "B" {
t.Errorf("unexpected results from the listing (%q)", all)
}
}
})
}