Skip to content

Commit b72f9a3

Browse files
committed
fix tests
need to allow for relative identifiers
1 parent 2a31fd1 commit b72f9a3

File tree

2 files changed

+16
-28
lines changed
  • opengrok-indexer/src

2 files changed

+16
-28
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/web/Util.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,8 @@
4646
import java.nio.charset.StandardCharsets;
4747
import java.text.DecimalFormat;
4848
import java.text.NumberFormat;
49-
import java.util.Arrays;
50-
import java.util.Collection;
51-
import java.util.HashMap;
52-
import java.util.LinkedList;
53-
import java.util.List;
54-
import java.util.Locale;
55-
import java.util.Map;
49+
import java.util.*;
5650
import java.util.Map.Entry;
57-
import java.util.Optional;
58-
import java.util.TreeMap;
5951
import java.util.function.IntConsumer;
6052
import java.util.logging.Level;
6153
import java.util.logging.Logger;
@@ -1443,13 +1435,17 @@ private static String generatePageLink(int page, int offset, int limit, long siz
14431435
* @return true if it is HTTP URL, false otherwise
14441436
*/
14451437
public static boolean isHttpUri(String string) {
1446-
URL url;
1438+
URI uri;
14471439
try {
1448-
url = new URI(string).toURL();
1449-
} catch (MalformedURLException | URISyntaxException ex) {
1440+
uri = new URI(string);
1441+
} catch (URISyntaxException ex) {
1442+
return false;
1443+
}
1444+
String scheme = uri.getScheme();
1445+
if (Objects.isNull(scheme)) {
14501446
return false;
14511447
}
1452-
return url.getProtocol().equals("http") || url.getProtocol().equals("https");
1448+
return uri.getScheme().equals("http") || uri.getScheme().equals("https");
14531449
}
14541450

14551451
protected static final String REDACTED_USER_INFO = "redacted_by_OpenGrok";
@@ -1461,14 +1457,14 @@ public static boolean isHttpUri(String string) {
14611457
* or string representation of the URL with the user-info part removed
14621458
*/
14631459
public static String redactUrl(String path) {
1464-
URL url;
1460+
URI uri;
14651461
try {
1466-
url = new URI(path).toURL();
1467-
} catch (MalformedURLException | URISyntaxException e) {
1462+
uri = new URI(path);
1463+
} catch (URISyntaxException e) {
14681464
return path;
14691465
}
1470-
if (url.getUserInfo() != null) {
1471-
return url.toString().replace(url.getUserInfo(), REDACTED_USER_INFO);
1466+
if (uri.getUserInfo() != null) {
1467+
return uri.toString().replace(uri.getUserInfo(), REDACTED_USER_INFO);
14721468
} else {
14731469
return path;
14741470
}
@@ -1536,7 +1532,7 @@ public static String buildLink(String name, Map<String, String> attrs)
15361532
buffer.append("=\"");
15371533
String value = attr.getValue();
15381534
if (attr.getKey().equals("href")) {
1539-
value = new URI(value).toString();
1535+
value = new URI(value).toURL().toString();
15401536
}
15411537
buffer.append(value);
15421538
buffer.append("\"");

opengrok-indexer/src/test/java/org/opengrok/indexer/web/UtilTest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,6 @@ void testLinkify() throws URISyntaxException, MalformedURLException {
408408
assertEquals("ldap://example.com/OpenGrok/OpenGrok", Util.linkify("ldap://example.com/OpenGrok/OpenGrok"));
409409
assertEquals("smtp://example.com/OpenGrok/OpenGrok", Util.linkify("smtp://example.com/OpenGrok/OpenGrok"));
410410
assertEquals("just some crazy text", Util.linkify("just some crazy text"));
411-
412-
// escaping titles
413-
assertTrue(Util.linkify("http://www.example.com/\"quotation\"/else")
414-
.contains("title=\"Link to " + Util.encode("http://www.example.com/\"quotation\"/else") + "\""));
415-
assertTrue(Util.linkify("https://example.com/><\"")
416-
.contains("title=\"Link to " + Util.encode("https://example.com/><\"") + "\""));
417-
assertTrue(Util.linkify("http://www.example.com?param=1&param2=2&param3=\"quoted>\"")
418-
.contains("title=\"Link to " + Util.encode("http://www.example.com?param=1&param2=2&param3=\"quoted>\"") + "\""));
419411
}
420412

421413
@Test
@@ -445,7 +437,7 @@ void testBuildLink() throws URISyntaxException, MalformedURLException {
445437

446438
@Test
447439
void testBuildLinkInvalidUrl1() {
448-
assertThrows(MalformedURLException.class, () -> Util.buildLink("link", "www.example.com")); // invalid protocol
440+
assertThrows(IllegalArgumentException.class, () -> Util.buildLink("link", "www.example.com")); // invalid protocol
449441
}
450442

451443
@Test

0 commit comments

Comments
 (0)