| 418 | } |
| 419 | |
| 420 | func TestServingFiles(t *testing.T) { |
| 421 | t.Parallel() |
| 422 | |
| 423 | // Create a test server |
| 424 | rootFS := fstest.MapFS{ |
| 425 | "index.html": &fstest.MapFile{ |
| 426 | Data: []byte("index-bytes"), |
| 427 | }, |
| 428 | "favicon.ico": &fstest.MapFile{ |
| 429 | Data: []byte("favicon-bytes"), |
| 430 | }, |
| 431 | "dashboard.js": &fstest.MapFile{ |
| 432 | Data: []byte("dashboard-js-bytes"), |
| 433 | }, |
| 434 | "dashboard.css": &fstest.MapFile{ |
| 435 | Data: []byte("dashboard-css-bytes"), |
| 436 | }, |
| 437 | "install.sh": &fstest.MapFile{ |
| 438 | Data: []byte("install-sh-bytes"), |
| 439 | }, |
| 440 | } |
| 441 | |
| 442 | db, _ := dbtestutil.NewDB(t) |
| 443 | handler, err := site.New(&site.Options{ |
| 444 | Telemetry: telemetry.NewNoop(), |
| 445 | SiteFS: rootFS, |
| 446 | Database: db, |
| 447 | }) |
| 448 | require.NoError(t, err) |
| 449 | srv := httptest.NewServer(handler) |
| 450 | defer srv.Close() |
| 451 | client := &http.Client{} |
| 452 | |
| 453 | // Create a context |
| 454 | ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 455 | defer cancelFunc() |
| 456 | |
| 457 | testCases := []struct { |
| 458 | path string |
| 459 | expected string |
| 460 | }{ |
| 461 | // Index cases |
| 462 | {"/", "index-bytes"}, |
| 463 | {"/index.html", "index-bytes"}, |
| 464 | {"/nested", "index-bytes"}, |
| 465 | {"/nested/", "index-bytes"}, |
| 466 | {"/nested/index.html", "index-bytes"}, |
| 467 | |
| 468 | // These are nested paths that should lead back to index. We don't |
| 469 | // allow nested JS or CSS files. |
| 470 | {"/double/nested", "index-bytes"}, |
| 471 | {"/double/nested/", "index-bytes"}, |
| 472 | {"/double/nested/index.html", "index-bytes"}, |
| 473 | {"/nested/dashboard.js", "index-bytes"}, |
| 474 | {"/nested/dashboard.css", "index-bytes"}, |
| 475 | {"/double/nested/dashboard.js", "index-bytes"}, |
| 476 | {"/double/nested/dashboard.css", "index-bytes"}, |
| 477 | |