diff --git a/fixtures/TEST_FRAGMENT_ERR_CODE.md b/fixtures/TEST_FRAGMENT_ERR_CODE.md new file mode 100644 index 0000000..49ad545 --- /dev/null +++ b/fixtures/TEST_FRAGMENT_ERR_CODE.md @@ -0,0 +1,5 @@ +Requesting a 404 page from Wikipedia. + +It's common for user to accept 429, but let's test with 404 since triggering 429 may annoy the server. + +- [404 Wikipedia Page](https://en.wikipedia.org/wiki/Should404#ignore-fragment) diff --git a/lychee-bin/tests/cli.rs b/lychee-bin/tests/cli.rs index 7bf9f73..9e32d24 100644 --- a/lychee-bin/tests/cli.rs +++ b/lychee-bin/tests/cli.rs @@ -1920,6 +1920,27 @@ mod cli { .stdout(contains("12 Errors")); } + #[test] + fn test_fragments_when_accept_error_status_codes() { + let mut cmd = main_command(); + let input = fixtures_path().join("TEST_FRAGMENT_ERR_CODE.md"); + + // it's common for user to accept 429, but let's test with 404 since + // triggering 429 may annoy the server + cmd.arg("--verbose") + .arg("--accept=200,404") + .arg("--include-fragments") + .arg(input) + .assert() + .success() + .stderr(contains( + "https://en.wikipedia.org/wiki/Should404#ignore-fragment", + )) + .stdout(contains("0 Errors")) + .stdout(contains("1 OK")) + .stdout(contains("1 Total")); + } + #[test] fn test_fallback_extensions() { let mut cmd = main_command(); diff --git a/lychee-lib/src/checker/website.rs b/lychee-lib/src/checker/website.rs index ef1a2a5..2b32e62 100644 --- a/lychee-lib/src/checker/website.rs +++ b/lychee-lib/src/checker/website.rs @@ -103,8 +103,10 @@ impl WebsiteChecker { match self.reqwest_client.execute(request).await { Ok(response) => { let status = Status::new(&response, &self.accepted); + // when `accept=200,429`, `status_code=429` will be treated as success + // but we are not able the check the fragment since it's inapplicable. if self.include_fragments - && status.is_success() + && response.status().is_success() && method == Method::GET && response.url().fragment().is_some_and(|x| !x.is_empty()) { diff --git a/lychee-lib/src/extract/markdown.rs b/lychee-lib/src/extract/markdown.rs index 41f0c72..41d6b51 100644 --- a/lychee-lib/src/extract/markdown.rs +++ b/lychee-lib/src/extract/markdown.rs @@ -139,7 +139,7 @@ pub(crate) fn extract_markdown(input: &str, include_verbatim: bool) -> Vec