|
3 | 3 | package pathutil |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "path/filepath" |
6 | 7 | "runtime" |
| 8 | + "strings" |
7 | 9 | "testing" |
8 | 10 | ) |
9 | 11 |
|
@@ -80,3 +82,141 @@ func TestURIToPath_Windows(t *testing.T) { |
80 | 82 | }) |
81 | 83 | } |
82 | 84 | } |
| 85 | + |
| 86 | +func TestNormalizeURL(t *testing.T) { |
| 87 | + tmpDir := t.TempDir() |
| 88 | + |
| 89 | + tests := []struct { |
| 90 | + name string |
| 91 | + input string |
| 92 | + baseDir string |
| 93 | + wantPrefix string |
| 94 | + wantErr bool |
| 95 | + }{ |
| 96 | + { |
| 97 | + name: "HTTP URL unchanged", |
| 98 | + input: "https://api.example.com/$metadata", |
| 99 | + baseDir: "", |
| 100 | + wantPrefix: "https://", |
| 101 | + wantErr: false, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "HTTPS URL unchanged", |
| 105 | + input: "http://localhost:8080/odata/$metadata", |
| 106 | + baseDir: "", |
| 107 | + wantPrefix: "http://", |
| 108 | + wantErr: false, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "Absolute file:// URL unchanged", |
| 112 | + input: "file:///tmp/metadata.xml", |
| 113 | + baseDir: "", |
| 114 | + wantPrefix: "file://", |
| 115 | + wantErr: false, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "Relative path with ./ normalized", |
| 119 | + input: "./metadata.xml", |
| 120 | + baseDir: tmpDir, |
| 121 | + wantPrefix: "file://", |
| 122 | + wantErr: false, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "Bare relative path normalized", |
| 126 | + input: "metadata.xml", |
| 127 | + baseDir: tmpDir, |
| 128 | + wantPrefix: "file://", |
| 129 | + wantErr: false, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "Absolute path normalized to file://", |
| 133 | + input: "/tmp/metadata.xml", |
| 134 | + baseDir: "", |
| 135 | + wantPrefix: "file://", |
| 136 | + wantErr: false, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "Subdirectory relative path", |
| 140 | + input: "contracts/metadata.xml", |
| 141 | + baseDir: tmpDir, |
| 142 | + wantPrefix: "file://", |
| 143 | + wantErr: false, |
| 144 | + }, |
| 145 | + } |
| 146 | + |
| 147 | + for _, tt := range tests { |
| 148 | + t.Run(tt.name, func(t *testing.T) { |
| 149 | + result, err := NormalizeURL(tt.input, tt.baseDir) |
| 150 | + |
| 151 | + if tt.wantErr { |
| 152 | + if err == nil { |
| 153 | + t.Errorf("Expected error but got none") |
| 154 | + } |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + if err != nil { |
| 159 | + t.Errorf("Unexpected error: %v", err) |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + if !strings.HasPrefix(result, tt.wantPrefix) { |
| 164 | + t.Errorf("Result %q does not start with %q", result, tt.wantPrefix) |
| 165 | + } |
| 166 | + |
| 167 | + // Verify file:// URLs contain absolute paths |
| 168 | + if strings.HasPrefix(result, "file://") { |
| 169 | + path := strings.TrimPrefix(result, "file://") |
| 170 | + if !filepath.IsAbs(path) { |
| 171 | + t.Errorf("file:// URL contains relative path: %q", result) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Verify relative paths are resolved correctly |
| 176 | + if tt.baseDir != "" && !strings.HasPrefix(tt.input, "http") && !strings.HasPrefix(tt.input, "file://") { |
| 177 | + path := strings.TrimPrefix(result, "file://") |
| 178 | + if !strings.Contains(path, filepath.ToSlash(tmpDir)) { |
| 179 | + t.Errorf("Relative path not resolved against baseDir. Got: %q", result) |
| 180 | + } |
| 181 | + } |
| 182 | + }) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestPathFromURL(t *testing.T) { |
| 187 | + tests := []struct { |
| 188 | + name string |
| 189 | + input string |
| 190 | + expected string |
| 191 | + }{ |
| 192 | + { |
| 193 | + name: "file:// URL extracts path", |
| 194 | + input: "file:///tmp/metadata.xml", |
| 195 | + expected: "/tmp/metadata.xml", |
| 196 | + }, |
| 197 | + { |
| 198 | + name: "HTTP URL returns empty", |
| 199 | + input: "https://api.example.com/$metadata", |
| 200 | + expected: "", |
| 201 | + }, |
| 202 | + { |
| 203 | + name: "HTTPS URL returns empty", |
| 204 | + input: "http://localhost:8080/metadata", |
| 205 | + expected: "", |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "bare path returns empty", |
| 209 | + input: "/tmp/file.xml", |
| 210 | + expected: "", |
| 211 | + }, |
| 212 | + } |
| 213 | + |
| 214 | + for _, tt := range tests { |
| 215 | + t.Run(tt.name, func(t *testing.T) { |
| 216 | + result := PathFromURL(tt.input) |
| 217 | + if runtime.GOOS != "windows" && result != tt.expected { |
| 218 | + t.Errorf("PathFromURL(%q) = %q, want %q", tt.input, result, tt.expected) |
| 219 | + } |
| 220 | + }) |
| 221 | + } |
| 222 | +} |
0 commit comments