-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_dev_fallback_test.go
More file actions
161 lines (142 loc) · 4.13 KB
/
Copy pathapp_dev_fallback_test.go
File metadata and controls
161 lines (142 loc) · 4.13 KB
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
156
157
158
159
160
161
package main
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"Talos/internal/manifest"
"Talos/internal/packages"
)
func TestHttpDevEndpointReachable(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(ts.Close)
if !httpDevEndpointReachable(ts.URL) {
t.Fatal("expected test server to be reachable")
}
if httpDevEndpointReachable("http://127.0.0.1:59199") {
t.Fatal("expected closed port to be unreachable")
}
}
func TestManifestDevIFrameURL_FallbackWhenDevUnreachable(t *testing.T) {
t.Setenv("TALOS_DEV_MODE", "1")
dir := t.TempDir()
distDir := filepath.Join(dir, "dist")
if err := os.MkdirAll(distDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(distDir, "index.html"), []byte("<!doctype html><html></html>"), 0o644); err != nil {
t.Fatal(err)
}
app := NewApp()
pkg := &packages.PackageInfo{
DirName: "FallbackPkg",
DirPath: dir,
Manifest: &manifest.Definition{
ID: "app.dev.fallback.test",
Name: "Fallback",
WebEntry: "dist/index.html",
Development: &manifest.Development{
URL: "http://127.0.0.1:59199",
AllowedOrigins: []string{"http://127.0.0.1:59199"},
},
},
}
u, origins, dev := app.manifestDevIFrameURL(pkg)
if dev {
t.Fatalf("expected packaged fallback (dev=false), got dev=true url=%q", u)
}
if len(origins) > 0 {
t.Fatalf("expected empty allowlist for packaged, got %v", origins)
}
if !strings.Contains(u, "/talos-pkg/") || !strings.Contains(u, "index.html") {
t.Fatalf("expected talos-pkg web entry URL, got %q", u)
}
}
func TestManifestDevIFrameURL_CommandOnlyUsesResolvedOverride(t *testing.T) {
t.Setenv("TALOS_DEV_MODE", "1")
dir := t.TempDir()
distDir := filepath.Join(dir, "dist")
if err := os.MkdirAll(distDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(distDir, "index.html"), []byte("<!doctype html><html></html>"), 0o644); err != nil {
t.Fatal(err)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(ts.Close)
app := NewApp()
pkg := &packages.PackageInfo{
DirName: "CmdOnly",
DirPath: dir,
Manifest: &manifest.Definition{
ID: "app.dev.cmdonly.test",
Name: "CmdOnly",
WebEntry: "dist/index.html",
Development: &manifest.Development{
Command: []string{"npm", "run", "dev"},
AllowedOrigins: []string{"http://127.0.0.1:59998"},
},
},
}
u0, _, _ := app.manifestDevIFrameURL(pkg)
if u0 != "about:blank" {
t.Fatalf("before resolve, expected about:blank, got %q", u0)
}
app.setDevResolvedURL(pkg.Manifest.ID, ts.URL+"/")
u1, origins, dev := app.manifestDevIFrameURL(pkg)
if !dev {
t.Fatal("expected dev mode true")
}
if u1 != ts.URL+"/" {
t.Fatalf("expected resolved dev URL %q, got %q", ts.URL+"/", u1)
}
if len(origins) == 0 {
t.Fatalf("expected origins merged from override, got empty")
}
}
func TestManifestDevIFrameURL_UsesDevWhenReachable(t *testing.T) {
t.Setenv("TALOS_DEV_MODE", "1")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(ts.Close)
dir := t.TempDir()
distDir := filepath.Join(dir, "dist")
if err := os.MkdirAll(distDir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(distDir, "index.html"), []byte("<!doctype html><html></html>"), 0o644); err != nil {
t.Fatal(err)
}
app := NewApp()
devURL := ts.URL + "/"
pkg := &packages.PackageInfo{
DirName: "LivePkg",
DirPath: dir,
Manifest: &manifest.Definition{
ID: "app.dev.live.test",
Name: "Live",
WebEntry: "dist/index.html",
Development: &manifest.Development{
URL: devURL,
AllowedOrigins: []string{ts.URL},
},
},
}
u, origins, dev := app.manifestDevIFrameURL(pkg)
if !dev {
t.Fatalf("expected dev mode, got dev=false url=%q", u)
}
if u != devURL {
t.Fatalf("expected dev URL %q, got %q", devURL, u)
}
if len(origins) == 0 {
t.Fatal("expected non-empty allowed_origins in dev")
}
}