Treat 304 as valid, add mock tests, fix mock path issue #900
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This continues the work started in #897.
Redirections
Regarding the discussion in that PR, 301, 302, 303, 307, and 308 redirections are handled by reqwest and the
response.status().is_redirection()
conditional block incheck_url()
wouldn't be reached under those circumstances. However, 300 (Multiple Choices), 304 (Not Modified), 305 (Use Proxy), and 306 (Switch Proxy, deprecated in HTTP 1.1) still reach that conditional block, since they're not handled by reqwest. As such, I've decided to keep theis_redirection()
conditional block.At least to me, the 304 response seems like it would be okay to treat as a valid response, so I modified
is_valid()
andcheck_url()
to treat it like a success response. If you disagree, I can remove these changes and it will be treated like an error. Either way, it will likely never be encountered in practice, since it's sent in response to a request'sIf-Modified-Since
orIf-Match
headers (which we're not using here).Added Tests
I added
link_checker
tests for 301-to-200 status codes, 301-to-404 status codes, and 500 status codes, to test redirections and the previously-addedresponse.status()
checking for non-success status codes incheck_url()
.Mockito Path Collisions
While adding the redirection tests, I encountered an issue with Mockito, where mocks with the same path (e.g.,
mock("GET", "/test")
) can potentially bleed between tests, so you may end up with an unexpected response which causes the test to sometimes pass and sometimes fail. To avoid this, I made the mock paths unique across tests, using pseudorandom alphanumeric paths. The current path naming scheme makes it easy to add new tests (without having to keep all the existing path names in your head) but if you would like to go about it a different way, just let me know.The path names should be kept unique going forward as tests are added or changed, so I added a note above the tests in
/components/link_checker/src/lib.rs
and/components/templates/src/global_fns/load_data.rs
. I may try to create an example to demonstrate the issue and see if the Mockito folks will look into it but this is the current state of affairs, at least.Etc.
Besides this, I fixed Clippy warnings about using
String::from()
aroundformat!()
. I forgot thatformat!()
returns aString
(not astr
), so theString::from()
was unnecessary.