fix: do not check the fragment when http response err but accepted (#1763)

Signed-off-by: Keming <kemingy94@gmail.com>
This commit is contained in:
Keming 2025-07-10 12:32:15 +08:00 committed by GitHub
parent 068b4850d5
commit 696a7cafc8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 2 deletions

5
fixtures/TEST_FRAGMENT_ERR_CODE.md vendored Normal file
View file

@ -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)

View file

@ -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();

View file

@ -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())
{

View file

@ -139,7 +139,7 @@ pub(crate) fn extract_markdown(input: &str, include_verbatim: bool) -> Vec<RawUr
.collect()
}
/// Extract fragments/anchors/fragments from a Markdown string.
/// Extract fragments/anchors from a Markdown string.
///
/// Fragments are generated from headings using the same unique kebab case method as GitHub.
/// If a [heading attribute](https://github.com/raphlinus/pulldown-cmark/blob/master/specs/heading_attrs.txt)