Skip to content

Commit

Permalink
KiwiUrls#prependLeadingSlash should accept empty strings
Browse files Browse the repository at this point in the history
Closes #1106
  • Loading branch information
sleberknight committed Mar 5, 2024
1 parent 7427112 commit 4afc32e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/org/kiwiproject/net/KiwiUrls.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ public static URL toURL(URI uri) {
* @return a new String with a leading slash
*/
public static String prependLeadingSlash(String path) {
var trimmedPath = path.trim();
var trimmedPath = path.strip();

if (trimmedPath.isEmpty()) {
return "/";
}

if (trimmedPath.charAt(0) == '/') {
return trimmedPath;
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/kiwiproject/net/KiwiUrlsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,27 @@ void testPrependLeadingSlash_WhenExtraPadding() {
assertThat(KiwiUrls.prependLeadingSlash(padded)).isEqualTo("/" + notPadded);
}

@ParameterizedTest
@ValueSource(strings = {"", " ", "\r\n", " \t "})
void shouldPrependLeadingSlash_WhenPathIsEmptyOrBlank(String path) {
assertThat(KiwiUrls.prependLeadingSlash(path)).isEqualTo("/");
}

@ParameterizedTest
@CsvSource(textBlock = """
/, /
' / ', /
/q, /q
q, /q
foo, /foo
/foo, /foo
bar/baz, /bar/baz
/bar/baz, /bar/baz
""")
void shouldPrependLeadingSlash_WithVariousPaths(String path, String expectedResult) {
assertThat(KiwiUrls.prependLeadingSlash(path)).isEqualTo(expectedResult);
}

@Test
void testReplaceDomainIn_BiggerDomain() {
assertThat(KiwiUrls.replaceDomainsIn(
Expand Down

0 comments on commit 4afc32e

Please sign in to comment.