| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | use topcoat::{ |
| 13 | Result, |
| 14 | context::Cx, |
| 15 | router::{StatusCode, route}, |
| 16 | }; |
| 17 | |
| 18 | |
| 19 | pub const ROBOTS: &str = "User-agent: * |
| 20 | Disallow: /*/repos/*/archive/ |
| 21 | Disallow: /*/repos/*/search |
| 22 | Disallow: /*/repos/*/blame/ |
| 23 | Disallow: /*/repos/*/compare |
| 24 | Disallow: /*/repos/*/log/ |
| 25 | Disallow: /*/repos/*/commits/ |
| 26 | Disallow: /auth/ |
| 27 | "; |
| 28 | |
| 29 | #[route(GET "/robots.txt")] |
| 30 | async fn robots(_cx: &Cx) -> Result<(StatusCode, &'static str)> { |
| 31 | Ok((StatusCode::OK, ROBOTS)) |
| 32 | } |
| 33 | |
| 34 | #[cfg(test)] |
| 35 | mod tests { |
| 36 | use super::*; |
| 37 | |
| 38 | #[test] |
| 39 | fn every_expensive_path_is_disallowed_and_browsing_is_not() { |
| 40 | for path in [ |
| 41 | "archive/", "search", "blame/", "compare", "log/", "commits/", |
| 42 | ] { |
| 43 | assert!( |
| 44 | ROBOTS.contains(&format!("Disallow: /*/repos/*/{path}")), |
| 45 | "{path}" |
| 46 | ); |
| 47 | } |
| 48 | assert!(!ROBOTS.contains("Disallow: /*/repos/*/tree")); |
| 49 | assert!(!ROBOTS.contains("Disallow: /*/repos/*/issues")); |
| 50 | assert!(ROBOTS.starts_with("User-agent: *\n")); |
| 51 | } |
| 52 | } |