Skip to content

Instantly share code, notes, and snippets.

@AnthonyClink
Last active June 7, 2016 21:08
Show Gist options
  • Save AnthonyClink/51b1ac9a0a68ea1c0542180f8ceab013 to your computer and use it in GitHub Desktop.
Save AnthonyClink/51b1ac9a0a68ea1c0542180f8ceab013 to your computer and use it in GitHub Desktop.
fix multiple slashes in urls generated from user input
public class CleanUserInputUnitTests{
private static final Pattern HAS_MORE_THAN_SINGLE_SLAHES_PATTERN = Pattern.compile("([^:])\\/+");
@Test
public void regexPatternForRemovingSlashesFromBadUrlsWorksCorrectly() {
String input = "https://www.google.com//we////super///love/slashes//////in//generated/urls";
String expectedOutput = "https://www.google.com/we/super/love/slashes/in/generated/urls";
Assert.assertEquals(Pattern.compile(HAS_MORE_THAN_SINGLE_SLAHES_PATTERN).matcher(input).replaceAll("$1/"), expectedOutput);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment