|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "k8s.io/apimachinery/pkg/util/validation" |
7 | 8 | ) |
8 | 9 |
|
| 10 | +func TestInjectKiteBase(t *testing.T) { |
| 11 | + html := `<html><head><link rel="modulepreload" href="__KITE_BASE__/assets/index.js"><script type="module" src="__KITE_BASE__/assets/main.js"></script></head></html>` |
| 12 | + |
| 13 | + t.Run("subpath", func(t *testing.T) { |
| 14 | + got := InjectKiteBase(html, "/kite") |
| 15 | + |
| 16 | + if strings.Contains(got, "__KITE_BASE__") { |
| 17 | + t.Fatalf("placeholder should be replaced: %s", got) |
| 18 | + } |
| 19 | + if strings.Contains(got, "<base ") { |
| 20 | + t.Fatalf("base tag should not be injected anymore: %s", got) |
| 21 | + } |
| 22 | + if !strings.Contains(got, `href="/kite/assets/index.js"`) { |
| 23 | + t.Fatalf("expected asset href to include subpath: %s", got) |
| 24 | + } |
| 25 | + if !strings.Contains(got, `src="/kite/assets/main.js"`) { |
| 26 | + t.Fatalf("expected asset src to include subpath: %s", got) |
| 27 | + } |
| 28 | + if !strings.Contains(got, `<script>window.__dynamic_base__="/kite";</script>`) { |
| 29 | + t.Fatalf("expected runtime base script: %s", got) |
| 30 | + } |
| 31 | + }) |
| 32 | + |
| 33 | + t.Run("root", func(t *testing.T) { |
| 34 | + got := InjectKiteBase(html, "") |
| 35 | + |
| 36 | + if !strings.Contains(got, `href="/assets/index.js"`) { |
| 37 | + t.Fatalf("expected root asset href: %s", got) |
| 38 | + } |
| 39 | + if !strings.Contains(got, `src="/assets/main.js"`) { |
| 40 | + t.Fatalf("expected root asset src: %s", got) |
| 41 | + } |
| 42 | + if !strings.Contains(got, `<script>window.__dynamic_base__="";</script>`) { |
| 43 | + t.Fatalf("expected empty runtime base script: %s", got) |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("escapes html attribute injection", func(t *testing.T) { |
| 48 | + got := InjectKiteBase(html, `/ki"te`) |
| 49 | + |
| 50 | + if strings.Contains(got, `href="/ki"te/assets/index.js"`) { |
| 51 | + t.Fatalf("expected asset href to be escaped: %s", got) |
| 52 | + } |
| 53 | + if !strings.Contains(got, `href="/ki"te/assets/index.js"`) { |
| 54 | + t.Fatalf("expected escaped quote in asset href: %s", got) |
| 55 | + } |
| 56 | + if !strings.Contains(got, `<script>window.__dynamic_base__="/ki\"te";</script>`) { |
| 57 | + t.Fatalf("expected runtime base script to remain safely quoted: %s", got) |
| 58 | + } |
| 59 | + }) |
| 60 | +} |
| 61 | + |
9 | 62 | func TestGetImageRegistryAndRepo(t *testing.T) { |
10 | 63 | testcase := []struct { |
11 | 64 | image string |
|
0 commit comments