Skip to content

Commit

Permalink
Merge pull request #53 from alecsmrekar/static-assets-by-type
Browse files Browse the repository at this point in the history
Log static assets by type
  • Loading branch information
jeremyandrews authored Jan 18, 2023
2 parents 939989b + de29615 commit c578e36
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- add support for building with [rustls](https://docs.rs/rustls) via the `rustls-tls` feature
- **API change**: update goose to [0.17](https://github.com/tag1consulting/goose/releases/tag/0.17.0)
- **API change**: Box `TransactionError` to avoid over-allocating memory on the stack (to match goose 0.17.0)
- `load_static_elements` logs asset loading in multiple groups: css, js and img

## 0.4.1 June 16, 2022
- introduce `validate_page` to validate a page without loading static assets, an alternative to `validate_and_load_static_assets`
Expand Down
18 changes: 8 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,22 +733,20 @@ pub async fn load_static_elements(user: &mut GooseUser, html: &str) {
// Use a case-insensitive regular expression to find all src=<foo> in the html, where
// <foo> is the URL to local image and js assets.
// @TODO: parse HTML5 srcset= also
let image = Regex::new(format!(r#"(?i)src="(({}|/).*?)""#, base_url).as_str()).unwrap();
let mut urls = Vec::new();
for url in image.captures_iter(html) {
urls.push(url[1].to_string());
let src_elements = Regex::new(format!(r#"(?i)src="(({}|/).*?)""#, base_url).as_str()).unwrap();
for url in src_elements.captures_iter(html) {
let is_js = url[1].to_string().contains(".js");
let resource_type = if is_js { "js" } else { "img" };
let _ = user
.get_named(&url[1], &("static asset: ".to_owned() + resource_type))
.await;
}

// Use a case-insensitive regular expression to find all href=<foo> in the html, where
// <foo> is the URL to local css assets.
let css = Regex::new(format!(r#"(?i)href="(({}|/).*?\.css.*?)""#, base_url).as_str()).unwrap();
for url in css.captures_iter(html) {
urls.push(url[1].to_string());
}

// Load all the static assets found on the page.
for asset in &urls {
let _ = user.get_named(asset, "static asset").await;
let _ = user.get_named(&url[1], "static asset: css").await;
}
}

Expand Down

0 comments on commit c578e36

Please sign in to comment.