forked from TwiN/gatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_test.go
74 lines (72 loc) · 1.83 KB
/
static_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
package static
import (
"io/fs"
"strings"
"testing"
)
func TestEmbed(t *testing.T) {
scenarios := []struct {
path string
shouldExist bool
expectedContainString string
}{
{
path: "index.html",
shouldExist: true,
expectedContainString: "</body>",
},
{
path: "favicon.ico",
shouldExist: true,
expectedContainString: "", // not checking because it's an image
},
{
path: "img/logo.svg",
shouldExist: true,
expectedContainString: "</svg>",
},
{
path: "css/app.css",
shouldExist: true,
expectedContainString: "background-color",
},
{
path: "js/app.js",
shouldExist: true,
expectedContainString: "function",
},
{
path: "js/chunk-vendors.js",
shouldExist: true,
expectedContainString: "function",
},
{
path: "file-that-does-not-exist.html",
shouldExist: false,
},
}
staticFileSystem, err := fs.Sub(FileSystem, RootPath)
if err != nil {
t.Fatal(err)
}
for _, scenario := range scenarios {
t.Run(scenario.path, func(t *testing.T) {
content, err := fs.ReadFile(staticFileSystem, scenario.path)
if !scenario.shouldExist {
if err == nil {
t.Errorf("%s should not have existed", scenario.path)
}
} else {
if err != nil {
t.Errorf("opening %s should not have returned an error, got %s", scenario.path, err.Error())
}
if len(content) == 0 {
t.Errorf("%s should have existed in the static FileSystem, but was empty", scenario.path)
}
if !strings.Contains(string(content), scenario.expectedContainString) {
t.Errorf("%s should have contained %s, but did not", scenario.path, scenario.expectedContainString)
}
}
})
}
}