forked from fastschema/fastschema
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdash_embed_test.go
More file actions
64 lines (58 loc) · 1.88 KB
/
Copy pathdash_embed_test.go
File metadata and controls
64 lines (58 loc) · 1.88 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
package fastschema
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
)
// TestDashEmbedServesAssets verifies the compiled-in dash bundle is served at
// the dash base path and that its HTML references the matching asset base, so a
// stale or wrongly-based bundle is caught before release. The Fiber mount mirrors
// the production static handler in pkg/restfulresolver/resource.go.
func TestDashEmbedServesAssets(t *testing.T) {
app := fiber.New()
app.Use("/dash", filesystem.New(filesystem.Config{
Root: http.FS(embedDashStatic),
PathPrefix: "dash",
Next: func(c *fiber.Ctx) bool {
return c.Path() == "/" || c.Path() == ""
},
}))
// index.html is served at /dash/ and points assets at the /dash/ base.
resp, err := app.Test(httptest.NewRequest(http.MethodGet, "/dash/", nil))
if err != nil {
t.Fatalf("request /dash/ failed: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("/dash/ status = %d, want 200", resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
if !strings.Contains(string(body), "/dash/assets/") {
t.Fatal("/dash/ index.html does not reference the /dash/assets/ base")
}
// A hashed JS asset (name varies per build) must serve 200.
entries, err := embedDashStatic.ReadDir("dash/assets")
if err != nil {
t.Fatalf("read embedded dash/assets: %v", err)
}
var asset string
for _, e := range entries {
if strings.HasSuffix(e.Name(), ".js") {
asset = e.Name()
break
}
}
if asset == "" {
t.Fatal("no .js asset found in embedded dash/assets")
}
resp2, err := app.Test(httptest.NewRequest(http.MethodGet, "/dash/assets/"+asset, nil))
if err != nil {
t.Fatalf("request /dash/assets/%s failed: %v", asset, err)
}
if resp2.StatusCode != http.StatusOK {
t.Fatalf("/dash/assets/%s status = %d, want 200", asset, resp2.StatusCode)
}
}