Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat 304 as valid, add mock tests, fix mock path issue #900

Merged
merged 1 commit into from
Dec 24, 2019

Conversation

samford
Copy link
Contributor

@samford samford commented Dec 24, 2019

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 in check_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 the is_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() and check_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's If-Modified-Since or If-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-added response.status() checking for non-success status codes in check_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() around format!(). I forgot that format!() returns a String (not a str), so the String::from() was unnecessary.

@samford samford force-pushed the link_checker-edits branch 2 times, most recently from 51c428c to 291b21d Compare December 24, 2019 02:21
@samford samford changed the title link_checker: Remove unneeded code and add tests link_checker: Fix Clippy warnings and add tests Dec 24, 2019
* Treat 304 (Not Modified) requests as valid.

* Add tests for 301-to-200 links, 301-to-404 links, and 500 links.
This helps to test redirections and the previously-added
response.status() checking for non-success status codes in check_url().

* Make names for HTTP mock paths unique, to avoid weird behavior. It
seems like mocks with the same path can potentially bleed between
tests, so you may end up with an unexpected response which causes the
test to sometimes pass and sometimes fail.

* Fix Clippy warnings about String::from(format!()).
@samford samford changed the title link_checker: Fix Clippy warnings and add tests Treat 304s as valid, add HTTP mock tests, fix mock path issue, fix Clippy warnings Dec 24, 2019
@samford samford changed the title Treat 304s as valid, add HTTP mock tests, fix mock path issue, fix Clippy warnings Treat 304 as valid, add mock tests, fix mock path issue Dec 24, 2019
@Keats Keats merged commit 2773ca8 into getzola:next Dec 24, 2019
@Keats
Copy link
Collaborator

Keats commented Dec 24, 2019

Thanks!

@samford samford deleted the link_checker-edits branch December 24, 2019 14:11
Keats pushed a commit that referenced this pull request Feb 3, 2020
* Treat 304 (Not Modified) requests as valid.

* Add tests for 301-to-200 links, 301-to-404 links, and 500 links.
This helps to test redirections and the previously-added
response.status() checking for non-success status codes in check_url().

* Make names for HTTP mock paths unique, to avoid weird behavior. It
seems like mocks with the same path can potentially bleed between
tests, so you may end up with an unexpected response which causes the
test to sometimes pass and sometimes fail.

* Fix Clippy warnings about String::from(format!()).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants