这个问题在这里被问过:
但我对答案完全不满意。我需要一种方法来比较两个 URL 是否相等,理想情况下我不会手动编写它。这个库需要明白这些 url 是相等的
http://stackoverflow.com
https://stackoverflow.com/
https://stackoverflow.com/questions/ask
https://stackoverflow.com/questions/ask/
http://stackoverflow.com?paramName=
http://stackoverflow.com?paramName
http://stackoverflow.com?paramName1=value1¶mName2=value2
http://stackoverflow.com?paramName2=value2¶mName1=value1
http://stackoverflow.com?param name 1=value 1
http://stackoverflow.com?param%20name%201=value%201
这些 URL 不相等:
https://stackoverflow.com/questions/ask
https://stackoverflow.com/questionz/ask
http://stackoverflow.com?paramName1=value1¶mName2=value2
http://stackoverflow.com?paramName1=value1¶mName2=value3
像这样的其他复杂的事情。我在哪里可以找到这样的图书馆?
顺便说一句,这是一个单元测试:
import org.junit.Test;
import java.net.URI;
import java.net.URISyntaxException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
public class UriTest {
@Test
public void equality() throws URISyntaxException {
assertUrlsEqual("http://stackoverflow.com", "https://stackoverflow.com/");
assertUrlsEqual("https://stackoverflow.com/questions/ask", "https://stackoverflow.com/questions/ask/");
assertUrlsEqual("http://stackoverflow.com?paramName=", "http://stackoverflow.com?paramName");
assertUrlsEqual("http://stackoverflow.com?paramName1=value1¶mName2=value2", "http://stackoverflow.com?paramName2=value2¶mName1=value1");
assertUrlsEqual("http://stackoverflow.com?param name 1=value 1", "http://stackoverflow.com?param%20name%201=value%201");
}
@Test
public void notEqual() throws URISyntaxException {
assertUrlsNotEqual("https://stackoverflow.com/questions/ask", "https://stackoverflow.com/questionz/ask");
assertUrlsNotEqual("http://stackoverflow.com?paramName1=value1¶mName2=value2", "http://stackoverflow.com?paramName1=value1¶mName2=value3");
}
private void assertUrlsNotEqual(String u1, String u2) throws URISyntaxException {
//...?
}
private void assertUrlsEqual(String u1, String u2) throws URISyntaxException {
//...?
}
}