Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

add unit test for Domains.stripToPort, simplify ipv6 check

regerac6e198bd1dc
diff --git a/source/net/yacy/cora/protocol/Domains.java b/source/net/yacy/cora/protocol/Domains.java
index f14cb1917..04155c589 100644
--- a/source/net/yacy/cora/protocol/Domains.java
+++ b/source/net/yacy/cora/protocol/Domains.java
@@ -842,7 +842,8 @@ public class Domains {
/**
* Reads the port out of a url string (the url must start with a protocol
- * like http://). If no port is given, default ports are returned
+ * like http:// to return correct default port). If no port is given, default
+ * ports are returned. On missing protocol, port=80 is assumed.
* @param target url (must start with protocol)
* @return port number
*/
@@ -870,8 +871,7 @@ public class Domains {
if ( p < 0 ) return port;
// the ':' must be a port divider or part of ipv6
- int pos;
- if (((pos = target.lastIndexOf(']')) < 0) || (pos < p)) {
+ if (target.lastIndexOf(']') < p) {
port = Integer.parseInt(target.substring(p + 1));
}
return port;
diff --git a/test/java/net/yacy/cora/protocol/DomainsTest.java b/test/java/net/yacy/cora/protocol/DomainsTest.java
new file mode 100644
index 000000000..a953117ce
--- /dev/null
+++ b/test/java/net/yacy/cora/protocol/DomainsTest.java
@@ -0,0 +1,52 @@
+/**
+ * DomainsTest
+ * part of YaCy
+ * Copyright 2016 by reger24; https://github.com/reger24
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program in the file lgpl21.txt
+ * If not, see <http://www.gnu.org/licenses/>.
+ */
+package net.yacy.cora.protocol;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * Unit tests for ReferenceContainer class.
+ */
+public class DomainsTest {
+
+ /**
+ * Test of stripToPort method, of class Domains.
+ */
+ @Test
+ public void testStripToPort() {
+ Map<String, Integer> testHost = new HashMap();
+
+ testHost.put("[3ffe:2a00:100:7031::1]:80", 80);
+ testHost.put("https://[3ffe:2a00:100:7031::1]:80/test.html", 80);
+ testHost.put("[3ffe:2a00:100:7031::1]/test.html", 80);
+ testHost.put("http://[3ffe:2a00:100:7031::1]/test.html", 80);
+ testHost.put("[3ffe:2a00:100:7031::1]:8090/test.html", 8090);
+
+ for (String host : testHost.keySet()) {
+ int port = Domains.stripToPort(host);
+ int expectedPort = testHost.get(host);
+ assertEquals(host, expectedPort, port);
+
+ }
+ }
+}