| 350 | } |
| 351 | |
| 352 | func TestCaching(t *testing.T) { |
| 353 | t.Parallel() |
| 354 | |
| 355 | // Create a test server |
| 356 | rootFS := fstest.MapFS{ |
| 357 | "bundle.js": &fstest.MapFile{}, |
| 358 | "image.png": &fstest.MapFile{}, |
| 359 | "static/image.png": &fstest.MapFile{}, |
| 360 | "favicon.ico": &fstest.MapFile{ |
| 361 | Data: []byte("folderFile"), |
| 362 | }, |
| 363 | |
| 364 | "service-worker.js": &fstest.MapFile{}, |
| 365 | "index.html": &fstest.MapFile{ |
| 366 | Data: []byte("folderFile"), |
| 367 | }, |
| 368 | "terminal.html": &fstest.MapFile{ |
| 369 | Data: []byte("folderFile"), |
| 370 | }, |
| 371 | } |
| 372 | |
| 373 | db, _ := dbtestutil.NewDB(t) |
| 374 | s, err := site.New(&site.Options{ |
| 375 | Telemetry: telemetry.NewNoop(), |
| 376 | SiteFS: rootFS, |
| 377 | Database: db, |
| 378 | }) |
| 379 | require.NoError(t, err) |
| 380 | srv := httptest.NewServer(s) |
| 381 | defer srv.Close() |
| 382 | |
| 383 | // Create a context |
| 384 | ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitShort) |
| 385 | defer cancelFunc() |
| 386 | |
| 387 | testCases := []struct { |
| 388 | path string |
| 389 | isExpectingCache bool |
| 390 | }{ |
| 391 | {"/bundle.js", true}, |
| 392 | {"/image.png", true}, |
| 393 | {"/static/image.png", true}, |
| 394 | {"/favicon.ico", true}, |
| 395 | |
| 396 | {"/", false}, |
| 397 | {"/service-worker.js", false}, |
| 398 | {"/index.html", false}, |
| 399 | {"/double/nested/terminal.html", false}, |
| 400 | } |
| 401 | |
| 402 | for _, testCase := range testCases { |
| 403 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+testCase.path, nil) |
| 404 | require.NoError(t, err, "create request") |
| 405 | |
| 406 | res, err := srv.Client().Do(req) |
| 407 | require.NoError(t, err, "get index") |
| 408 | |
| 409 | cache := res.Header.Get("Cache-Control") |