|
5 | 5 | "io" |
6 | 6 | "io/fs" |
7 | 7 | "net/http" |
| 8 | + "net/http/httptest" |
8 | 9 | "os" |
9 | 10 | "path/filepath" |
10 | 11 | "testing" |
@@ -242,3 +243,162 @@ func TestGetOrCreateResponseItemDirWithCompressionNotFound(t *testing.T) { |
242 | 243 | t.Fatalf("expected status %d, got %d", http.StatusNotFound, code) |
243 | 244 | } |
244 | 245 | } |
| 246 | + |
| 247 | +func TestMapRequestPath(t *testing.T) { |
| 248 | + params := param.Params{ |
| 249 | + Directory: ".", |
| 250 | + BasePath: "/app", |
| 251 | + } |
| 252 | + app := NewApp(¶ms) |
| 253 | + |
| 254 | + tests := []struct { |
| 255 | + name string |
| 256 | + in string |
| 257 | + want string |
| 258 | + }{ |
| 259 | + {name: "base path exact", in: "/app", want: "/"}, |
| 260 | + {name: "base path with trailing slash", in: "/app/", want: "/"}, |
| 261 | + {name: "asset under base path", in: "/app/assets/main.js", want: "/assets/main.js"}, |
| 262 | + {name: "outside prefix fallback", in: "/assets/main.js", want: "/assets/main.js"}, |
| 263 | + {name: "prefix-like but not matching", in: "/application/main.js", want: "/application/main.js"}, |
| 264 | + } |
| 265 | + |
| 266 | + for _, tt := range tests { |
| 267 | + t.Run(tt.name, func(t *testing.T) { |
| 268 | + got := app.mapRequestPath(tt.in) |
| 269 | + if got != tt.want { |
| 270 | + t.Fatalf("expected %q, got %q", tt.want, got) |
| 271 | + } |
| 272 | + }) |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +func TestMapRequestPathRootBasePathNoop(t *testing.T) { |
| 277 | + params := param.Params{ |
| 278 | + Directory: ".", |
| 279 | + BasePath: "/", |
| 280 | + } |
| 281 | + app := NewApp(¶ms) |
| 282 | + |
| 283 | + got := app.mapRequestPath("/app/assets/main.js") |
| 284 | + if got != "/app/assets/main.js" { |
| 285 | + t.Fatalf("expected path unchanged, got %q", got) |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +func TestMapRequestPathEmptyBasePathNoop(t *testing.T) { |
| 290 | + params := param.Params{ |
| 291 | + Directory: ".", |
| 292 | + BasePath: "", |
| 293 | + } |
| 294 | + app := NewApp(¶ms) |
| 295 | + |
| 296 | + got := app.mapRequestPath("/app/assets/main.js") |
| 297 | + if got != "/app/assets/main.js" { |
| 298 | + t.Fatalf("expected path unchanged, got %q", got) |
| 299 | + } |
| 300 | +} |
| 301 | + |
| 302 | +func TestHandlerFuncNewBasePathCanonicalIndex(t *testing.T) { |
| 303 | + dir := t.TempDir() |
| 304 | + indexPath := filepath.Join(dir, "index.html") |
| 305 | + if err := os.WriteFile(indexPath, []byte("index"), 0600); err != nil { |
| 306 | + t.Fatalf("failed to write index: %v", err) |
| 307 | + } |
| 308 | + |
| 309 | + params := param.Params{ |
| 310 | + Directory: dir, |
| 311 | + BasePath: "/app", |
| 312 | + SpaMode: true, |
| 313 | + } |
| 314 | + app := NewApp(¶ms) |
| 315 | + |
| 316 | + for _, reqPath := range []string{"/app", "/app/"} { |
| 317 | + req := httptest.NewRequest("GET", reqPath, nil) |
| 318 | + rec := httptest.NewRecorder() |
| 319 | + app.HandlerFuncNew(rec, req) |
| 320 | + |
| 321 | + if rec.Code != http.StatusOK { |
| 322 | + t.Fatalf("expected status 200 for %s, got %d", reqPath, rec.Code) |
| 323 | + } |
| 324 | + if rec.Body.String() != "index" { |
| 325 | + t.Fatalf("expected index body for %s, got %q", reqPath, rec.Body.String()) |
| 326 | + } |
| 327 | + } |
| 328 | +} |
| 329 | + |
| 330 | +func TestHandlerFuncNewBasePathAssetMappingAndIgnoreCacheControl(t *testing.T) { |
| 331 | + dir := t.TempDir() |
| 332 | + assetPath := filepath.Join(dir, "asset.bin") |
| 333 | + if err := os.WriteFile(assetPath, []byte("asset"), 0600); err != nil { |
| 334 | + t.Fatalf("failed to write asset: %v", err) |
| 335 | + } |
| 336 | + |
| 337 | + params := param.Params{ |
| 338 | + Directory: dir, |
| 339 | + BasePath: "/app", |
| 340 | + SpaMode: true, |
| 341 | + IgnoreCacheControlPaths: []string{"/asset.bin"}, |
| 342 | + CacheControlMaxAge: 3600, |
| 343 | + } |
| 344 | + app := NewApp(¶ms) |
| 345 | + |
| 346 | + req := httptest.NewRequest("GET", "/app/asset.bin", nil) |
| 347 | + rec := httptest.NewRecorder() |
| 348 | + app.HandlerFuncNew(rec, req) |
| 349 | + |
| 350 | + if rec.Code != http.StatusOK { |
| 351 | + t.Fatalf("expected status 200, got %d", rec.Code) |
| 352 | + } |
| 353 | + if rec.Header().Get("Cache-Control") != "no-store" { |
| 354 | + t.Fatalf("expected no-store cache control, got %s", rec.Header().Get("Cache-Control")) |
| 355 | + } |
| 356 | + if rec.Body.String() != "asset" { |
| 357 | + t.Fatalf("expected asset body, got %q", rec.Body.String()) |
| 358 | + } |
| 359 | +} |
| 360 | + |
| 361 | +func TestHandlerFuncNewBasePathTraversalRejected(t *testing.T) { |
| 362 | + dir := t.TempDir() |
| 363 | + params := param.Params{ |
| 364 | + Directory: dir, |
| 365 | + BasePath: "/app", |
| 366 | + SpaMode: true, |
| 367 | + } |
| 368 | + app := NewApp(¶ms) |
| 369 | + |
| 370 | + req := httptest.NewRequest("GET", "/app/../secret.txt", nil) |
| 371 | + rec := httptest.NewRecorder() |
| 372 | + app.HandlerFuncNew(rec, req) |
| 373 | + |
| 374 | + if rec.Code != http.StatusNotFound { |
| 375 | + t.Fatalf("expected 404, got %d", rec.Code) |
| 376 | + } |
| 377 | +} |
| 378 | + |
| 379 | +func TestHandlerFuncNewBasePathOutsidePrefixFallsBackToRoot(t *testing.T) { |
| 380 | + dir := t.TempDir() |
| 381 | + assetPath := filepath.Join(dir, "asset.bin") |
| 382 | + if err := os.WriteFile(assetPath, []byte("asset"), 0600); err != nil { |
| 383 | + t.Fatalf("failed to write asset: %v", err) |
| 384 | + } |
| 385 | + |
| 386 | + params := param.Params{ |
| 387 | + Directory: dir, |
| 388 | + BasePath: "/app", |
| 389 | + SpaMode: true, |
| 390 | + CacheControlMaxAge: 3600, |
| 391 | + } |
| 392 | + app := NewApp(¶ms) |
| 393 | + |
| 394 | + req := httptest.NewRequest("GET", "/asset.bin", nil) |
| 395 | + rec := httptest.NewRecorder() |
| 396 | + app.HandlerFuncNew(rec, req) |
| 397 | + |
| 398 | + if rec.Code != http.StatusOK { |
| 399 | + t.Fatalf("expected status 200, got %d", rec.Code) |
| 400 | + } |
| 401 | + if rec.Body.String() != "asset" { |
| 402 | + t.Fatalf("expected asset body, got %q", rec.Body.String()) |
| 403 | + } |
| 404 | +} |
0 commit comments