From 2f1b592ab47fa7b6888c2455186117020337c2b9 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Mon, 23 Dec 2019 03:25:09 -0500 Subject: [PATCH] link_checker: Handle non-success status codes (#897) The can_fail_404_links() test doesn't encounter a 404 response in actuality, since the google.comys domain doesn't resolve. When the test is updated such that the response's status code is a 404, the test fails because the check_url() function doesn't handle non-success responses how the test's assertions expect. This commit updates check_url() to handle non-success responses, treating them much like errors. --- components/link_checker/src/lib.rs | 32 +++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/components/link_checker/src/lib.rs b/components/link_checker/src/lib.rs index ebb301f..56f7401 100644 --- a/components/link_checker/src/lib.rs +++ b/components/link_checker/src/lib.rs @@ -71,7 +71,37 @@ pub fn check_url(url: &str, config: &LinkChecker) -> LinkResult { Err(e) => LinkResult { code: None, error: Some(e.to_string()) }, } } - Ok(response) => LinkResult { code: Some(response.status()), error: None }, + Ok(response) => { + if response.status().is_success() { + LinkResult { code: Some(response.status()), error: None } + } else { + let error_string = if response.status().is_informational() { + String::from(format!( + "Informational status code ({}) received", + response.status() + )) + } else if response.status().is_redirection() { + String::from(format!( + "Redirection status code ({}) received", + response.status() + )) + } else if response.status().is_client_error() { + String::from(format!( + "Client error status code ({}) received", + response.status() + )) + } else if response.status().is_server_error() { + String::from(format!( + "Server error status code ({}) received", + response.status() + )) + } else { + String::from("Non-success status code received") + }; + + LinkResult { code: None, error: Some(error_string) } + } + } Err(e) => LinkResult { code: None, error: Some(e.description().to_string()) }, };