Skip to content

Commit

Permalink
test: check_static_file_route_pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoupeiheng committed Jul 20, 2022
1 parent a49d880 commit cc274ee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,15 @@ impl MiniserveConfig {
// Generate some random routes for the favicon and css so that they are very unlikely to conflict with
// real files.
let (favicon_route, css_route) = if args.random_route {
(format!("/{}", nanoid::nanoid!(10, &ROUTE_ALPHABET)), format!("/{}", nanoid::nanoid!(10, &ROUTE_ALPHABET)))
(
format!("/{}", nanoid::nanoid!(10, &ROUTE_ALPHABET)),
format!("/{}", nanoid::nanoid!(10, &ROUTE_ALPHABET)),
)
} else {
(format!("{}/{}", route_prefix, nanoid::nanoid!(10, &ROUTE_ALPHABET)), format!("{}/{}", route_prefix, nanoid::nanoid!(10, &ROUTE_ALPHABET)))
(
format!("{}/{}", route_prefix, nanoid::nanoid!(10, &ROUTE_ALPHABET)),
format!("{}/{}", route_prefix, nanoid::nanoid!(10, &ROUTE_ALPHABET)),
)
};

let default_color_scheme = args.color_scheme;
Expand Down
28 changes: 28 additions & 0 deletions tests/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mod fixtures;

use fixtures::{server_no_stderr, Error, TestServer};
use regex::Regex;
use rstest::rstest;
use select::{document::Document, predicate::Attr};

#[rstest]
#[case(server_no_stderr(&[] as &[&str]), "/[a-f0-9]+")]
#[case(server_no_stderr(&["--random-route"]), "/[a-f0-9]+")]
#[case(server_no_stderr(&["--route-prefix", "foo"]), "/foo/[a-f0-9]+")]
fn check_static_file_route_pattern(
#[case] server: TestServer,
#[case] route_pattern: String,
) -> Result<(), Error> {
let body = reqwest::blocking::get(server.url())?;
let parsed = Document::from_read(body)?;
let re = Regex::new(&route_pattern).unwrap();

assert!(parsed
.find(Attr("rel", "stylesheet"))
.all(|x| re.is_match(x.attr("href").unwrap())));
assert!(parsed
.find(Attr("rel", "icon"))
.all(|x| re.is_match(x.attr("href").unwrap())));

Ok(())
}

0 comments on commit cc274ee

Please sign in to comment.