add unit test for Domains.stripToPort,

simplify ipv6 check
This commit is contained in:
reger 2016-11-19 06:22:55 +01:00
parent f27531f5ec
commit ac6e198bd1
2 changed files with 55 additions and 3 deletions

View File

@ -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;

View File

@ -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);
}
}
}