| 1 | use jv::har::{extract_har_files, extract_path_segment, is_hex_like}; |
| 2 | use serde_json::json; |
| 3 | use serde_json::Value; |
| 4 | |
| 5 | fn make_entry(method: &str, url: &str, status: u64, mime: &str, body: &str) -> Value { |
| 6 | make_entry_with_opts(method, url, status, mime, body, None, None) |
| 7 | } |
| 8 | |
| 9 | fn make_entry_with_opts( |
| 10 | method: &str, |
| 11 | url: &str, |
| 12 | status: u64, |
| 13 | mime: &str, |
| 14 | body: &str, |
| 15 | encoding: Option<&str>, |
| 16 | post_data: Option<Value>, |
| 17 | ) -> Value { |
| 18 | let mut entry = json!({ |
| 19 | "request": { |
| 20 | "method": method, |
| 21 | "url": url, |
| 22 | }, |
| 23 | "response": { |
| 24 | "status": status, |
| 25 | "content": { |
| 26 | "mimeType": mime, |
| 27 | "text": body, |
| 28 | } |
| 29 | } |
| 30 | }); |
| 31 | if let Some(enc) = encoding { |
| 32 | entry["response"]["content"]["encoding"] = json!(enc); |
| 33 | } |
| 34 | if let Some(pd) = post_data { |
| 35 | entry["request"]["postData"] = pd; |
| 36 | } |
| 37 | entry |
| 38 | } |
| 39 | |
| 40 | fn make_har(entries: Vec<Value>) -> Value { |
| 41 | json!({ "log": { "entries": entries } }) |
| 42 | } |
| 43 | |
| 44 | #[test] |
| 45 | fn test_extract_har_files_basic() { |
| 46 | let entry = make_entry( |
| 47 | "GET", |
| 48 | "https://api.example.com/api/v2/flights?origin=LAX&date=2026-03-01", |
| 49 | 200, |
| 50 | "application/json", |
| 51 | r#"{"flights":[]}"#, |
| 52 | ); |
| 53 | let har = make_har(vec![entry]); |
| 54 | let results = extract_har_files(&har); |
| 55 | assert_eq!(results.len(), 1); |
| 56 | assert_eq!(results[0].0, "flights_LAX_2026-03-01.json"); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn test_extract_har_files_post() { |
| 61 | let entry = make_entry_with_opts( |
| 62 | "POST", |
| 63 | "https://example.com/auth/login", |
| 64 | 200, |
| 65 | "application/json", |
| 66 | r#"{"token":"abc"}"#, |
| 67 | None, |
| 68 | Some(json!({ "text": r#"{"username":"alice"}"# })), |
| 69 | ); |
| 70 | let har = make_har(vec![entry]); |
| 71 | let results = extract_har_files(&har); |
| 72 | assert_eq!(results.len(), 1); |
| 73 | assert_eq!(results[0].0, "login_alice.json"); |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn test_extract_har_skips_non_json() { |
| 78 | let entry = make_entry("GET", "https://example.com/image", 200, "image/png", ""); |
| 79 | let har = make_har(vec![entry]); |
| 80 | let results = extract_har_files(&har); |
| 81 | assert!(results.is_empty()); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn test_extract_har_skips_non_2xx() { |
| 86 | let entry = make_entry( |
| 87 | "GET", |
| 88 | "https://example.com/missing", |
| 89 | 404, |
| 90 | "application/json", |
| 91 | r#"{"error":"not found"}"#, |
| 92 | ); |
| 93 | let har = make_har(vec![entry]); |
| 94 | let results = extract_har_files(&har); |
| 95 | assert!(results.is_empty()); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn test_extract_har_skips_base64() { |
| 100 | let entry = make_entry_with_opts( |
| 101 | "GET", |
| 102 | "https://example.com/data", |
| 103 | 200, |
| 104 | "application/json", |
| 105 | "eyJrZXkiOiJ2YWx1ZSJ9", |
| 106 | Some("base64"), |
| 107 | None, |
| 108 | ); |
| 109 | let har = make_har(vec![entry]); |
| 110 | let results = extract_har_files(&har); |
| 111 | assert!(results.is_empty()); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn test_extract_path_segment() { |
| 116 | assert_eq!( |
| 117 | extract_path_segment("https://api.example.com/api/v2/flights?origin=LAX"), |
| 118 | "flights" |
| 119 | ); |
| 120 | assert_eq!( |
| 121 | extract_path_segment("https://example.com/users/42/bookings?status=confirmed"), |
| 122 | "bookings" |
| 123 | ); |
| 124 | assert_eq!( |
| 125 | extract_path_segment("https://example.com/health"), |
| 126 | "health" |
| 127 | ); |
| 128 | assert_eq!( |
| 129 | extract_path_segment("https://example.com/rest/v1/items"), |
| 130 | "items" |
| 131 | ); |
| 132 | assert_eq!( |
| 133 | extract_path_segment("https://example.com/auth/login"), |
| 134 | "login" |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn test_is_hex_like() { |
| 140 | |
| 141 | assert!(is_hex_like("550e8400-e29b-41d4-a716-446655440000")); |
| 142 | |
| 143 | assert!(is_hex_like("abcdef1234567890abcdef")); |
| 144 | |
| 145 | assert!(!is_hex_like("LAX")); |
| 146 | assert!(!is_hex_like("hello")); |
| 147 | |
| 148 | assert!(!is_hex_like("abcdef")); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn test_deduplication() { |
| 153 | let entry1 = make_entry( |
| 154 | "GET", |
| 155 | "https://example.com/api/v2/flights?origin=LAX", |
| 156 | 200, |
| 157 | "application/json", |
| 158 | r#"{"id":1}"#, |
| 159 | ); |
| 160 | let entry2 = make_entry( |
| 161 | "GET", |
| 162 | "https://example.com/api/v2/flights?origin=LAX", |
| 163 | 200, |
| 164 | "application/json", |
| 165 | r#"{"id":2}"#, |
| 166 | ); |
| 167 | let har = make_har(vec![entry1, entry2]); |
| 168 | let results = extract_har_files(&har); |
| 169 | assert_eq!(results.len(), 2); |
| 170 | assert_eq!(results[0].0, "flights_LAX.json"); |
| 171 | assert_eq!(results[1].0, "flights_LAX_2.json"); |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn test_excludes_long_tokens() { |
| 176 | let entry = make_entry( |
| 177 | "GET", |
| 178 | "https://example.com/api/data?token=abcdefghijklmnopqrstuvwxyz&lang=en", |
| 179 | 200, |
| 180 | "application/json", |
| 181 | r#"{"ok":true}"#, |
| 182 | ); |
| 183 | let har = make_har(vec![entry]); |
| 184 | let results = extract_har_files(&har); |
| 185 | assert_eq!(results.len(), 1); |
| 186 | |
| 187 | assert_eq!(results[0].0, "data_en.json"); |
| 188 | } |