-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver_test.go
155 lines (134 loc) · 3.11 KB
/
server_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package vueglue
import (
"embed"
"fmt"
"net/http"
"os"
"testing"
)
//go:embed testdata
var embedTest embed.FS
func TestInitLib(t *testing.T) {
config := &ViteConfig{
Environment: "development",
JSProjectPath: "testdata",
URLPrefix: "/",
FS: os.DirFS("testdata"),
EntryPoint: "server.js",
}
glue, err := initializeVueGlue(config)
if err != nil {
t.Fatalf("Library failed to initialize: %s", err)
}
if glue == nil {
t.Fatalf("No glue was returned")
}
if glue.MainModule != "server.js" {
t.Fatalf("Expected main module to be %s, got %s", "server.ts", glue.MainModule)
}
}
func TestServerHandler(t *testing.T) {
config := &ViteConfig{
Environment: "development",
JSProjectPath: "testdata",
URLPrefix: "/",
FS: os.DirFS("testdata"),
EntryPoint: "server.js",
}
glue, err := initializeVueGlue(config)
if err != nil {
t.Fatalf("no glue! %s", err)
}
_, err = glue.FileServer()
if err != nil {
t.Fatalf("no handler was returned: %s", err)
}
srv, err := startTestServer(glue)
if err != nil {
t.Fatalf("server did not bootstrap: %s", err)
}
defer srv.Close()
url := srv.URL
response, err := http.Head(url)
if err != nil {
t.Fatalf("could not ping server: %s", err)
}
if response.StatusCode != 200 {
t.Fatalf("HEAD / got %d, expected %d", response.StatusCode, 200)
}
}
func TestFileVisibility(t *testing.T) {
srv, err := bootStrapServer(nil)
if err != nil {
t.Fatalf("could not bootstrap test server: %s", err)
}
defer srv.Close()
var dataList = []struct {
Path string
Status int
}{
{"", 200},
{"index.html", 200},
{"regfile.txt", 200},
{"not-there", 404},
{".secret", 404},
{".secdir/file.txt", 404},
{"subdir", 404},
{"subdir/", 404},
{"subdir/regfile.txt", 200},
{"subdir/.env-file", 404},
}
base := srv.URL
for _, item := range dataList {
url := fmt.Sprintf("%s/%s", base, item.Path)
response, err := http.Head(url)
if err != nil {
t.Errorf("%s: Error on Head %s", item.Path, err)
} else {
if response.StatusCode != item.Status {
t.Errorf("%s: expected %d but got %d", item.Path, item.Status, response.StatusCode)
}
}
}
}
func TestEmbedAccess(t *testing.T) {
config := &ViteConfig{
Environment: "development",
JSProjectPath: "testdata",
URLPrefix: "/",
FS: embedTest,
EntryPoint: "server.js",
}
srv, err := bootStrapServer(config)
if err != nil {
t.Fatalf("could not bootstrap test server: %s", err)
}
defer srv.Close()
var dataList = []struct {
Path string
Status int
}{
{"", 200},
{"index.html", 200},
{"regfile.txt", 200},
{"not-there", 404},
{".secret", 404},
{".secdir/file.txt", 404},
{"subdir", 404},
{"subdir/", 404},
{"subdir/regfile.txt", 200},
{"subdir/.env-file", 404},
}
base := srv.URL
for _, item := range dataList {
url := fmt.Sprintf("%s/%s", base, item.Path)
response, err := http.Head(url)
if err != nil {
t.Errorf("%s: Error on Head %s", item.Path, err)
} else {
if response.StatusCode != item.Status {
t.Errorf("%s: expected %d but got %d", item.Path, item.Status, response.StatusCode)
}
}
}
}