-
Notifications
You must be signed in to change notification settings - Fork 0
/
subfs_test.go
85 lines (80 loc) · 1.86 KB
/
subfs_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
package pi
import (
"bytes"
"errors"
"io"
"io/fs"
"net/http"
"testing"
"testing/fstest"
"time"
)
func TestSub(t *testing.T) {
root := fstest.MapFS{
"web/dist/index.html": &fstest.MapFile{Data: []byte("index.html"), Mode: fs.ModePerm, ModTime: time.Now()},
"web/dist/css/app.css": &fstest.MapFile{Data: []byte("app.css"), Mode: fs.ModePerm, ModTime: time.Now()},
}
tests := []struct {
name string
dir string
open string
wantBytes []byte
wantDir bool
wantErrNotExist bool
}{
{
name: "open web/dist/index.html by /index.html should ok",
dir: "web/dist",
open: "/index.html",
wantBytes: []byte("index.html"),
},
{
name: "open web/dist/index.html by index.html should ok",
dir: "web/dist",
open: "index.html",
wantBytes: []byte("index.html"),
},
{
name: "open / should got a dir",
dir: "web/dist",
open: "/",
wantDir: true,
},
{
name: "open unknown.txt should got ErrNotFound",
dir: "web/dist",
open: "unknown.txt",
wantErrNotExist: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := Sub(http.FS(root), tt.dir)
f, err := h.Open(tt.open)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) || !tt.wantErrNotExist {
t.Fatalf("%s got error = %v", tt.name, err)
}
}
if tt.wantDir {
fi, err := f.Stat()
if err != nil {
t.Fatalf("%s got error = %v", tt.name, err)
}
if !fi.IsDir() {
t.Fatalf("%s got a file", tt.name)
}
}
if tt.wantBytes != nil {
b, err := io.ReadAll(f)
if err != nil {
t.Fatalf("%s got error = %v", tt.name, err)
}
f.Close()
if !bytes.Equal(b, tt.wantBytes) {
t.Fatalf("%s want bytes = %s, got = %s", tt.name, tt.wantBytes, b)
}
}
})
}
}