| 1198 | #[test] |
| 1199 | #[cfg(feature = "stylesheet-cache")] |
| 1200 | fn test_cache() { |
| 1201 | use std::{ |
| 1202 | num::NonZeroUsize, |
| 1203 | sync::{Arc, Mutex}, |
| 1204 | }; |
| 1205 | |
| 1206 | let html = r#" |
| 1207 | <html> |
| 1208 | <head> |
| 1209 | <link href="http://127.0.0.1:1234/external.css" rel="stylesheet"> |
| 1210 | <style> |
| 1211 | h2 { color: red; } |
| 1212 | </style> |
| 1213 | </head> |
| 1214 | <body> |
| 1215 | <h1>Big Text</h1> |
| 1216 | <h2>Smaller Text</h2> |
| 1217 | </body> |
| 1218 | </html>"#; |
| 1219 | |
| 1220 | #[derive(Debug, Default)] |
| 1221 | pub struct CustomStylesheetResolver { |
| 1222 | hits: Arc<Mutex<usize>>, |
| 1223 | } |
| 1224 | |
| 1225 | impl css_inline::StylesheetResolver for CustomStylesheetResolver { |
| 1226 | fn retrieve(&self, _: &str) -> css_inline::Result<String> { |
| 1227 | let mut hits = self.hits.lock().expect("Lock is poisoned"); |
| 1228 | *hits += 1; |
| 1229 | Ok("h1 { color: blue; }".to_string()) |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | let hits = Arc::new(Mutex::new(0)); |
| 1234 | |
| 1235 | let inliner = CSSInliner::options() |
| 1236 | .resolver(Arc::new(CustomStylesheetResolver { hits: hits.clone() })) |
| 1237 | .cache(css_inline::StylesheetCache::new( |
| 1238 | NonZeroUsize::new(3).unwrap(), |
| 1239 | )) |
| 1240 | .build(); |
| 1241 | for _ in 0..5 { |
| 1242 | let inlined = inliner.inline(html); |
| 1243 | let expected = r#"<body> |
| 1244 | <h1 style="color: blue;">Big Text</h1> |
| 1245 | <h2 style="color: red;">Smaller Text</h2> |
| 1246 | |
| 1247 | </body></html>"#; |
| 1248 | assert!(inlined.expect("Inlining failed").ends_with(expected)); |
| 1249 | } |
| 1250 | |
| 1251 | let hits = hits.lock().expect("Lock is poisoned"); |
| 1252 | assert_eq!(*hits, 1); |
| 1253 | } |
| 1254 | |
| 1255 | #[test] |
| 1256 | #[cfg(feature = "stylesheet-cache")] |