-
Notifications
You must be signed in to change notification settings - Fork 710
Closes #1976, HttpUrlConnection now allowed to follow redirects #2027
Changes from all commits
7c96fe3
902c150
829806c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -219,11 +219,12 @@ private void showServerError(final ManualAddSearchEngineSettingsFragment that) { | |
HttpURLConnection connection = null; | ||
try { | ||
connection = (HttpURLConnection) searchURL.openConnection(); | ||
connection.setInstanceFollowRedirects(true); | ||
connection.setConnectTimeout(SEARCH_QUERY_VALIDATION_TIMEOUT_MILLIS); | ||
connection.setReadTimeout(SEARCH_QUERY_VALIDATION_TIMEOUT_MILLIS); | ||
|
||
// A non-error HTTP response is good enough as a sanity check, some search engines redirect to https. | ||
return connection.getResponseCode() < 400; | ||
// Now that redirects are followed, 300 is a better and stronger sanity check, checks for a non error and non redirect response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: general coding tip: this comment references the old state of the code "now that redirects...", "...is a better..." but someone reading this code has no idea what the previous code looked like. Thus, comments should either:
In this case, a dev doesn't need to understand the old state to understand the code so I'd only reference the current code (e.g. "We follow redirects, check for success codes only"). Note that your comment could be a good extended commit message though! That's usually the place to write, "I changed this code for these reasons". That being said, I don't think this is worth creating another commit for - I'm just going to land it. |
||
return connection.getResponseCode() < 300; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this change I request breaks the tests: https://travis-ci.org/mozilla-mobile/focus-android/builds/325730045?utm_source=github_status&utm_medium=notification since the test explicitly checks request codes in 3xx – do you mind updating the tests too? Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah sure! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if I've done that right though |
||
|
||
} catch (final IOException e) { | ||
// Don't log exception to avoid leaking URL. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,9 @@ class SearchEngineValidationTest { | |
} | ||
|
||
@Test | ||
fun `URL using HTTP redirect is valid`() = withMockWebServer(responseWithStatus(301)) { | ||
// Currently we accept redirects as valid. We might want to follow the redirect (Issue #1976) | ||
assertTrue(isValidSearchQueryURL(it.rootUrl())) | ||
fun `URL using HTTP redirect is invalid`() = withMockWebServer(responseWithStatus(301)) { | ||
// We now follow redirects(Issue #1976). This test now asserts false. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: similar to my comment above, this comment references the past state of the code, "We now follow redirects... This test now asserts...". I think a better description might be: "We follow redirects so we should never receive a 3xx. If we do, we want to fail." Or something. But I don't think it's worth the turn around time to fix now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will keep in mind the next time 😄 Thanks for helping me throughout! |
||
assertFalse(isValidSearchQueryURL(it.rootUrl())) | ||
} | ||
|
||
@Test | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few lines below this, we check against responseCode < 400 – we should check < 300 now because we're following redirects. If we get a 300 even after following redirects, something must be crazy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh right, covering all bases