health.rs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //! Integration tests for the BFF shell.
  2. //!
  3. //! These drive the [`Router`] through `tower::ServiceExt::oneshot`, which
  4. //! exercises the real handler stack without binding a port or starting a
  5. //! server.
  6. use std::net::SocketAddr;
  7. use std::path::{Path, PathBuf};
  8. use axum::body::Body;
  9. use axum::http::{Request, StatusCode};
  10. use server::app::build_app;
  11. use server::config::ServerConfig;
  12. use server::state::AppState;
  13. use tower::ServiceExt as _;
  14. /// Path to the SPA build output owned by Wave 4, resolved independently of
  15. /// the test's working directory.
  16. fn dist_dir() -> PathBuf {
  17. Path::new(env!("CARGO_MANIFEST_DIR"))
  18. .join("..")
  19. .join("web")
  20. .join("dist")
  21. }
  22. /// A router built from an explicit config (no environment dependence).
  23. fn test_app() -> axum::Router {
  24. let config = ServerConfig::new(SocketAddr::from(([127, 0, 0, 1], 3001)), dist_dir());
  25. build_app(AppState::new(config))
  26. }
  27. #[tokio::test]
  28. async fn healthz_returns_200_without_binding_a_port() -> Result<(), Box<dyn std::error::Error>> {
  29. let response = test_app()
  30. .oneshot(Request::builder().uri("/healthz").body(Body::empty())?)
  31. .await?;
  32. assert_eq!(response.status(), StatusCode::OK);
  33. let bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await?;
  34. let body = std::str::from_utf8(&bytes)?;
  35. assert!(body.contains("ok"), "unexpected health body: {body:?}");
  36. Ok(())
  37. }
  38. #[tokio::test]
  39. async fn unknown_path_is_404_or_spa_fallback_when_dist_exists()
  40. -> Result<(), Box<dyn std::error::Error>> {
  41. let response = test_app()
  42. .oneshot(Request::builder().uri("/nope").body(Body::empty())?)
  43. .await?;
  44. if dist_dir().join("index.html").is_file() {
  45. assert_eq!(
  46. response.status(),
  47. StatusCode::OK,
  48. "SPA fallback should serve index.html for an unknown path"
  49. );
  50. } else {
  51. assert_eq!(
  52. response.status(),
  53. StatusCode::NOT_FOUND,
  54. "without a built dist, an unknown path must be 404"
  55. );
  56. }
  57. Ok(())
  58. }