Skip to content

Instantly share code, notes, and snippets.

@skeller88
Created June 18, 2015 18:26
Show Gist options
  • Save skeller88/c7a0f5888032f7e6d77e to your computer and use it in GitHub Desktop.
Save skeller88/c7a0f5888032f7e6d77e to your computer and use it in GitHub Desktop.
Java url regex tests
@Test
public void testShouldMatch() throws Exception {
Collection<ValidationError> validationErrors = Lists.newArrayList();
List<String> shouldMatch = new ArrayList<>(Arrays.asList(
"http://مثال.إختبار",
"http://[email protected]",
"http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
"http://उदाहरण.परीक्षा",
"ftps://foo.com",
"https://foo.com",
"ftps://foo.bar/",
"test-scheme://foo.com",
"test+scheme://foo.com",
"test.scheme://foo.com",
"http://[email protected]",
"http://[email protected]/",
"http://[email protected]:8080",
"http://[email protected]:8080/",
"http://userid:[email protected]",
"http://userid:[email protected]/",
"http://userid:[email protected]:8080",
"http://userid:[email protected]:8080/",
"http://fitbit.com",
"http://www.fitbit.com",
"http://✪df.ws/123",
"http://142.42.1.1/",
"http://1337.net",
"http://a.b-c.de",
"http://a.b--c.de/",
"http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
"http://➡.ws/䨹",
"http://⌘.ws",
"http://⌘.ws/",
"http://☺.damowmow.com/",
"http://j.mp",
"http://142.42.1.1:8080/",
"http://foo.com/blah_blah",
"http://foo.com/blah_blah/",
"http://foo.com/blah_blah_(wikipedia)",
"http://foo.com/blah_blah_(wikipedia)_(again)",
"http://foo.com/unicode_(✪)_in_parens",
"http://fitbit.com/user%20info/body",
"http://fitbit.com/",
"http://www.example.com/wpstyle/?p=364",
"https://www.example.com/foo/?bar=baz&inga=42&quux",
"http://fitbit.com/user/bob?first=is+this+a+user%28maybe%29%3F",
"http://foo.com/(something)?after=parens",
"http://foo.bar/?q=Test%20URL-encoded%20stuff",
"http://fitbit.com//user",
"http://foo.com/blah_(wikipedia)#cite-1",
"http://foo.com/blah_(wikipedia)_blah#cite-1",
"http://code.google.com/events/#&product=browser",
"foo.com"
));
convertUrls(shouldMatch, validationErrors);
errorCollector.checkThat(validationErrors.size(), is(0));
// for test debugging purposes
for (ValidationError error : validationErrors) {
System.out.println(((ScopedLocalizableError) error).getReplacementParameters()[2]);
}
}
@Test
public void testShouldNotMatch() throws Exception {
Collection<ValidationError> validationErrors = Lists.newArrayList();
List<String> shouldNotMatch = new ArrayList<>(Arrays.asList(
// multiple invalid components
"//",
"//a",
"///a",
"///",
// invalid scheme syntax
"-scheme://test.fitbit.com",
":// should fail",
// invalid domain name. Common errors:
// domain labels must start and end with alphanumeric characters
"http://##/",
"http://",
"http://.",
"http://..",
"http://../",
"http://?",
"http://??",
"http://??/",
"http://#",
"http://##",
"http://##/",
"http:///a",
"http:// shouldfail.com",
"http://3628126748",
"rdar://1234",
"h://test",
"http://-error-.invalid/",
"http://-a.b.co",
"http://a.b-.co",
"http://.www.foo.bar/",
"http://.www.foo.bar./",
"http://www.foo.bar./",
// invalid port
"http://www.fitbit.com:invalidPort",
"http://www.fitbit.com:1",
"http://www.fitbit.com:123456",
"http://www.fitbit.com:-4000",
// invalid path
"http://foo.bar/foo(bar)baz quux",
// invalid query strings
"http://foo.bar?q=Spaces should be encoded",
"http://fitbit.comname=bob&age=25",
"http://fitbit.comname=bob&age=25"
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment