|
| 1 | +package pathutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestBuildPath(t *testing.T) { |
| 9 | + t.Helper() |
| 10 | + |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + prefix string |
| 14 | + path string |
| 15 | + expected string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "no prefix configured", |
| 19 | + prefix: "", |
| 20 | + path: "/dashboard", |
| 21 | + expected: "/dashboard", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "with prefix - dashboard", |
| 25 | + prefix: "/pgbackweb", |
| 26 | + path: "/dashboard", |
| 27 | + expected: "/pgbackweb/dashboard", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "with prefix - api", |
| 31 | + prefix: "/pgbackweb", |
| 32 | + path: "/api/v1/health", |
| 33 | + expected: "/pgbackweb/api/v1/health", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "with prefix - root", |
| 37 | + prefix: "/pgbackweb", |
| 38 | + path: "", |
| 39 | + expected: "/pgbackweb", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "with prefix - auth", |
| 43 | + prefix: "/pgbackweb", |
| 44 | + path: "/auth/login", |
| 45 | + expected: "/pgbackweb/auth/login", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "empty prefix and empty path", |
| 49 | + prefix: "", |
| 50 | + path: "", |
| 51 | + expected: "", |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + originalPrefix := pathPrefix |
| 58 | + pathPrefixOnce = sync.Once{} |
| 59 | + SetPathPrefix(tt.prefix) |
| 60 | + defer func() { |
| 61 | + pathPrefixOnce = sync.Once{} |
| 62 | + pathPrefix = originalPrefix |
| 63 | + }() |
| 64 | + |
| 65 | + result := BuildPath(tt.path) |
| 66 | + if result != tt.expected { |
| 67 | + t.Errorf("BuildPath(%q) with prefix %q = %q, want %q", tt.path, tt.prefix, result, tt.expected) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestGetPathPrefix(t *testing.T) { |
| 74 | + t.Helper() |
| 75 | + originalPrefix := pathPrefix |
| 76 | + pathPrefixOnce = sync.Once{} |
| 77 | + SetPathPrefix("/test-prefix") |
| 78 | + defer func() { |
| 79 | + pathPrefixOnce = sync.Once{} |
| 80 | + pathPrefix = originalPrefix |
| 81 | + }() |
| 82 | + |
| 83 | + result := GetPathPrefix() |
| 84 | + if result != "/test-prefix" { |
| 85 | + t.Errorf("GetPathPrefix() = %q, want %q", result, "/test-prefix") |
| 86 | + } |
| 87 | +} |
0 commit comments