dist_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package controller
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. )
  7. func TestWithServerBasePath(t *testing.T) {
  8. spec := []byte(`{"openapi":"3.0.3","info":{"title":"x"},"servers":[{"url":"/","description":"old"}],"paths":{"/p":{"get":{"summary":"s"}}}}`)
  9. out, err := withServerBasePath(spec, "/test/")
  10. if err != nil {
  11. t.Fatalf("withServerBasePath: %v", err)
  12. }
  13. var doc map[string]any
  14. if err := json.Unmarshal(out, &doc); err != nil {
  15. t.Fatalf("unmarshal result: %v", err)
  16. }
  17. servers, ok := doc["servers"].([]any)
  18. if !ok || len(servers) != 1 {
  19. t.Fatalf("servers = %v, want one entry", doc["servers"])
  20. }
  21. srv, _ := servers[0].(map[string]any)
  22. if srv["url"] != "/test" {
  23. t.Errorf("server url = %v, want /test (trailing slash trimmed)", srv["url"])
  24. }
  25. if doc["openapi"] != "3.0.3" {
  26. t.Errorf("openapi field not preserved: %v", doc["openapi"])
  27. }
  28. if _, ok := doc["paths"].(map[string]any)["/p"]; !ok {
  29. t.Errorf("paths content not preserved verbatim")
  30. }
  31. }
  32. func TestWithServerBasePathInvalidJSON(t *testing.T) {
  33. if _, err := withServerBasePath([]byte("not json"), "/test/"); err == nil {
  34. t.Errorf("expected error on invalid spec, got nil")
  35. }
  36. }
  37. func TestPWAHeadInjectionUsesRuntimeBasePath(t *testing.T) {
  38. tests := []struct {
  39. name string
  40. basePath string
  41. wantPath string
  42. }{
  43. {name: "root", basePath: "/", wantPath: "/manifest.webmanifest"},
  44. {name: "secret path", basePath: "panel-secret", wantPath: "/panel-secret/manifest.webmanifest"},
  45. {name: "trailing slash", basePath: "/panel-secret/", wantPath: "/panel-secret/manifest.webmanifest"},
  46. }
  47. for _, test := range tests {
  48. t.Run(test.name, func(t *testing.T) {
  49. head := string(pwaHeadInjection(test.basePath, "login.html"))
  50. if !strings.Contains(head, `href="`+test.wantPath+`"`) {
  51. t.Fatalf("manifest URL = %q, want %q", head, test.wantPath)
  52. }
  53. if !strings.Contains(head, `src="`+strings.Replace(test.wantPath, "manifest.webmanifest", "pwa-register.js", 1)+`"`) {
  54. t.Fatalf("registration URL = %q", head)
  55. }
  56. })
  57. }
  58. }
  59. func TestPWAHeadInjectionSkipsSubscriptionPage(t *testing.T) {
  60. if got := pwaHeadInjection("/panel-secret/", "subpage.html"); got != nil {
  61. t.Fatalf("subpage injection = %q, want nil", got)
  62. }
  63. }