-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix URL validation and handling in URLUtil #12800
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
Changes from all commits
aed917c
4b99f41
4ea9e06
5f22761
04b92fe
52bba0f
fb8324b
5900647
fabfb1d
eaf63e3
de62e9e
106f978
598e04b
0ff2957
d92779c
2bed0cb
ab63569
d7967b6
ee6fb59
b67ea74
6cb2568
5c8c408
6cfa087
573e810
8a8deaf
82627f1
2ff7f9b
85fe62b
0cc8737
a2fa48e
95e367f
8dfbe88
0368f71
8b50c09
fce4ddc
8374633
3cee13c
65570d6
95f2f5e
20ebfaf
dfe5406
7465a15
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 |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package org.jabref.logic.net; | ||
|
|
||
| import java.net.URI; | ||
| import java.net.URL; | ||
|
|
||
| import org.jabref.logic.util.URLUtil; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class URLUtilTest { | ||
|
|
||
| @Test | ||
| void cleanGoogleSearchURL() throws Exception { | ||
| // empty text | ||
| assertEquals("", URLUtil.cleanGoogleSearchURL("")); | ||
| assertEquals(" ", URLUtil.cleanGoogleSearchURL(" ")); | ||
| // no URL | ||
| assertEquals("this is no url!", URLUtil.cleanGoogleSearchURL("this is no url!")); | ||
| // no Google search URL | ||
| assertEquals("http://dl.acm.org/citation.cfm?id=321811", URLUtil.cleanGoogleSearchURL("http://dl.acm.org/citation.cfm?id=321811")); | ||
| // malformed Google URL | ||
| assertEquals("https://www.google.de/url♥", URLUtil.cleanGoogleSearchURL("https://www.google.de/url♥")); | ||
| // no queries | ||
| assertEquals("https://www.google.de/url", URLUtil.cleanGoogleSearchURL("https://www.google.de/url")); | ||
| assertEquals("https://www.google.de/url?", URLUtil.cleanGoogleSearchURL("https://www.google.de/url?")); | ||
| // no multiple queries | ||
| assertEquals("https://www.google.de/url?key=value", URLUtil.cleanGoogleSearchURL("https://www.google.de/url?key=value")); | ||
| // no key values | ||
| assertEquals("https://www.google.de/url?key", URLUtil.cleanGoogleSearchURL("https://www.google.de/url?key")); | ||
| assertEquals("https://www.google.de/url?url", URLUtil.cleanGoogleSearchURL("https://www.google.de/url?url")); | ||
| assertEquals("https://www.google.de/url?key=", URLUtil.cleanGoogleSearchURL("https://www.google.de/url?key=")); | ||
| // no url param | ||
| assertEquals("https://www.google.de/url?key=value&key2=value2", URLUtil.cleanGoogleSearchURL("https://www.google.de/url?key=value&key2=value2")); | ||
| // no url param value | ||
| assertEquals("https://www.google.de/url?url=", URLUtil.cleanGoogleSearchURL("https://www.google.de/url?url=")); | ||
| // url param value no URL | ||
| assertEquals("https://www.google.de/url?url=this+is+no+url", URLUtil.cleanGoogleSearchURL("https://www.google.de/url?url=this+is+no+url")); | ||
| // Http | ||
| assertEquals( | ||
| "http://moz.com/ugc/the-ultimate-guide-to-the-google-search-parameters", | ||
| URLUtil.cleanGoogleSearchURL("http://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCEQFjAAahUKEwjJurHd2sfHAhWBsxQKHSrEAaM&url=http%3A%2F%2Fmoz.com%2Fugc%2Fthe-ultimate-guide-to-the-google-search-parameters&ei=0THeVYmOJIHnUqqIh5gK&usg=AFQjCNHnid_r_d2LP8_MqvI7lQnTC3lB_g&sig2=ICzxDroG2ENTJSUGmdhI2w")); | ||
| // Https | ||
| assertEquals( | ||
| "https://moz.com/ugc/the-ultimate-guide-to-the-google-search-parameters", | ||
| URLUtil.cleanGoogleSearchURL("https://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCEQFjAAahUKEwjJurHd2sfHAhWBsxQKHSrEAaM&url=https%3A%2F%2Fmoz.com%2Fugc%2Fthe-ultimate-guide-to-the-google-search-parameters&ei=0THeVYmOJIHnUqqIh5gK&usg=AFQjCNHnid_r_d2LP8_MqvI7lQnTC3lB_g&sig2=ICzxDroG2ENTJSUGmdhI2w")); | ||
| // root domain | ||
| assertEquals( | ||
| "https://moz.com/ugc/the-ultimate-guide-to-the-google-search-parameters", | ||
| URLUtil.cleanGoogleSearchURL("https://google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCEQFjAAahUKEwjJurHd2sfHAhWBsxQKHSrEAaM&url=https%3A%2F%2Fmoz.com%2Fugc%2Fthe-ultimate-guide-to-the-google-search-parameters&ei=0THeVYmOJIHnUqqIh5gK&usg=AFQjCNHnid_r_d2LP8_MqvI7lQnTC3lB_g&sig2=ICzxDroG2ENTJSUGmdhI2w")); | ||
| // foreign domain | ||
| assertEquals( | ||
| "https://moz.com/ugc/the-ultimate-guide-to-the-google-search-parameters", | ||
| URLUtil.cleanGoogleSearchURL("https://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCEQFjAAahUKEwjJurHd2sfHAhWBsxQKHSrEAaM&url=https%3A%2F%2Fmoz.com%2Fugc%2Fthe-ultimate-guide-to-the-google-search-parameters&ei=0THeVYmOJIHnUqqIh5gK&usg=AFQjCNHnid_r_d2LP8_MqvI7lQnTC3lB_g&sig2=ICzxDroG2ENTJSUGmdhI2w")); | ||
| // foreign domain co.uk | ||
| assertEquals( | ||
| "https://moz.com/ugc/the-ultimate-guide-to-the-google-search-parameters", | ||
| URLUtil.cleanGoogleSearchURL("https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCEQFjAAahUKEwjJurHd2sfHAhWBsxQKHSrEAaM&url=https%3A%2F%2Fmoz.com%2Fugc%2Fthe-ultimate-guide-to-the-google-search-parameters&ei=0THeVYmOJIHnUqqIh5gK&usg=AFQjCNHnid_r_d2LP8_MqvI7lQnTC3lB_g&sig2=ICzxDroG2ENTJSUGmdhI2w")); | ||
| // accept ftp results | ||
| assertEquals( | ||
| "ftp://moz.com/ugc/the-ultimate-guide-to-the-google-search-parameters", | ||
| URLUtil.cleanGoogleSearchURL("https://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCEQFjAAahUKEwjJurHd2sfHAhWBsxQKHSrEAaM&url=ftp%3A%2F%2Fmoz.com%2Fugc%2Fthe-ultimate-guide-to-the-google-search-parameters&ei=0THeVYmOJIHnUqqIh5gK&usg=AFQjCNHnid_r_d2LP8_MqvI7lQnTC3lB_g&sig2=ICzxDroG2ENTJSUGmdhI2w")); | ||
| } | ||
|
|
||
| @Test | ||
| void isURLshouldAcceptValidURL() { | ||
| assertTrue(URLUtil.isURL("http://www.google.com")); | ||
| assertTrue(URLUtil.isURL("https://www.google.com")); | ||
| } | ||
|
|
||
| @Test | ||
| void isURLshouldRejectInvalidURL() { | ||
| assertFalse(URLUtil.isURL("www.google.com")); | ||
|
sfahaddev marked this conversation as resolved.
|
||
| assertFalse(URLUtil.isURL("google.com")); | ||
| } | ||
|
|
||
| @Test | ||
| void isURLshouldRejectEmbeddedURL() { | ||
| boolean result = URLUtil.isURL("dblp computer science bibliography, http://dblp.org"); | ||
| assertEquals(false, result); | ||
| } | ||
|
|
||
| @Test | ||
| void createUriShouldHandlePipeCharacter() { | ||
| String input = "http://example.com/test|file"; | ||
| URI uri = URLUtil.createUri(input); | ||
| assertEquals("http://example.com/test%7Cfile", uri.toString()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| // Relative URLs (should default to HTTPS) | ||
| "www.example.com, https://www.example.com", | ||
|
Member
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 don't get this - maybe it should be reversed: expected is first, input is second
Member
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. ChatGPT and Co-Pilot always generate this ordering; I don't know why ^^
Author
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. Hi @koppor, I really appreciate the learning experience and your feedback throughout this process. Since this was my first open-source contribution, I gave it my best shot but I realize I couldn’t meet the expectations fully. I’ve decided to step back and will be closing my PR. Thank you again for the opportunity and guidance. |
||
| "example.com, https://example.com", | ||
|
|
||
| // Absolute URLs (should remain unchanged) | ||
| "http://www.example.com, http://www.example.com", | ||
| "https://www.example.com, https://www.example.com" | ||
| }) | ||
| void createShouldHandleURLs(String expected, String input) throws Exception { | ||
| URL url = URLUtil.create(input); | ||
| assertEquals(expected, url.toString()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "ftp://example.com, ftp://example.com", | ||
| "file:///path/to/file, file:/path/to/file" | ||
| }) | ||
| void createShouldHandleOtherProtocols(String expectedUrl, String inputUrl) throws Exception { | ||
| URL actualUrl = URLUtil.create(inputUrl); | ||
| assertEquals(expectedUrl, actualUrl.toString()); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.