📄 uritest.java
字号:
checkEmpty(uri.getHost(), HOST); if (((checked & PORT) == 0) && (uri.getPort() != -1)) failed |= PORT; checkEmpty(uri.getPath(), PATH); checkEmpty(uri.getQuery(), QUERY); checkEmpty(uri.getFragment(), FRAGMENT); // Report failures report(); return this; } // Summarization and reporting static void header(String s) { out.println(); out.println(); out.println("-- " + s + " --"); } static void show(String prefix, URISyntaxException x) { out.println(uquote(x.getInput())); if (x.getIndex() >= 0) { for (int i = 0; i < x.getIndex(); i++) { if (x.getInput().charAt(i) >= '\u0080') out.print(" "); // Skip over \u1234 else out.print(" "); } out.println("^"); } out.println(prefix + ": " + x.getReason()); } private void summarize() { out.println(); StringBuffer sb = new StringBuffer(); if (input.length() == 0) sb.append("\"\""); else sb.append(input); if (base != null) { sb.append(" "); sb.append(base); } if (!parsed()) { String s = (((checked & PARSEFAIL) != 0) ? "Correct exception" : "UNEXPECTED EXCEPTION"); if (exc instanceof URISyntaxException) show(s, (URISyntaxException)exc); else { out.println(uquote(sb.toString())); out.print(s + ": "); exc.printStackTrace(out); } } else { if (uri != originalURI) { sb.append(" "); sb.append(op); sb.append(" --> "); sb.append(uri); } out.println(uquote(sb.toString())); } } public static String uquote(String str) { if (str == null) return str; StringBuffer sb = new StringBuffer(); int n = str.length(); for (int i = 0; i < n; i++) { char c = str.charAt(i); if ((c >= ' ') && (c < 0x7f)) { sb.append(c); continue; } sb.append("\\u"); String s = Integer.toHexString(c).toUpperCase(); while (s.length() < 4) s = "0" + s; sb.append(s); } return sb.toString(); } static void show(String n, String v) { out.println(" " + n + " = ".substring(n.length()) + uquote(v)); } static void show(String n, String v, String vd) { if ((v == null) || v.equals(vd)) show(n, v); else { out.println(" " + n + " = ".substring(n.length()) + uquote(v) + " = " + uquote(vd)); } } public static void show(URI u) { show("opaque", "" + u.isOpaque()); show("scheme", u.getScheme()); show("ssp", u.getRawSchemeSpecificPart(), u.getSchemeSpecificPart()); show("authority", u.getRawAuthority(), u.getAuthority()); show("userinfo", u.getRawUserInfo(), u.getUserInfo()); show("host", u.getHost()); show("port", "" + u.getPort()); show("path", u.getRawPath(), u.getPath()); show("query", u.getRawQuery(), u.getQuery()); show("fragment", u.getRawFragment(), u.getFragment()); if (!u.toString().equals(u.toASCIIString())) show("toascii", u.toASCIIString()); } private void report() { summarize(); if (failed == 0) return; StringBuffer sb = new StringBuffer(); sb.append("FAIL:"); if ((failed & PARSEFAIL) != 0) sb.append(" parsefail"); if ((failed & SCHEME) != 0) sb.append(" scheme"); if ((failed & SSP) != 0) sb.append(" ssp"); if ((failed & OPAQUEPART) != 0) sb.append(" opaquepart"); if ((failed & USERINFO) != 0) sb.append(" userinfo"); if ((failed & USERINFO_D) != 0) sb.append(" userinfod"); if ((failed & HOST) != 0) sb.append(" host"); if ((failed & PORT) != 0) sb.append(" port"); if ((failed & REGISTRY) != 0) sb.append(" registry"); if ((failed & PATH) != 0) sb.append(" path"); if ((failed & PATH_D) != 0) sb.append(" pathd"); if ((failed & QUERY) != 0) sb.append(" query"); if ((failed & QUERY_D) != 0) sb.append(" queryd"); if ((failed & FRAGMENT) != 0) sb.append(" fragment"); if ((failed & FRAGMENT_D) != 0) sb.append(" fragmentd"); if ((failed & TOASCII) != 0) sb.append(" toascii"); if ((failed & IDENT_STR) != 0) sb.append(" ident-str"); if ((failed & IDENT_URI1) != 0) sb.append(" ident-uri1"); if ((failed & IDENT_URI3) != 0) sb.append(" ident-uri3"); if ((failed & IDENT_URI5) != 0) sb.append(" ident-uri5"); if ((failed & IDENT_URI7) != 0) sb.append(" ident-uri7"); if ((failed & TOSTRING) != 0) sb.append(" tostring"); out.println(sb.toString()); if (uri != null) show(uri); throw new RuntimeException("URITest failed"); } // -- URITests -- static void rfc2396() { header("RFC2396: Basic examples"); test("ftp://ftp.is.co.za/rfc/rfc1808.txt") .s("ftp").h("ftp.is.co.za").p("/rfc/rfc1808.txt").z(); test("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles") .s("gopher").h("spinaltap.micro.umn.edu") .p("/00/Weather/California/Los%20Angeles").z(); test("http://www.math.uio.no/faq/compression-faq/part1.html") .s("http").h("www.math.uio.no").p("/faq/compression-faq/part1.html").z(); test("mailto:mduerst@ifi.unizh.ch") .s("mailto").o("mduerst@ifi.unizh.ch").z(); test("news:comp.infosystems.www.servers.unix") .s("news").o("comp.infosystems.www.servers.unix").z(); test("telnet://melvyl.ucop.edu/") .s("telnet").h("melvyl.ucop.edu").p("/").z(); test("http://www.w3.org/Addressing/") .s("http").h("www.w3.org").p("/Addressing/").z(); test("ftp://ds.internic.net/rfc/") .s("ftp").h("ds.internic.net").p("/rfc/").z(); test("http://www.ics.uci.edu/pub/ietf/uri/historical.html#WARNING") .s("http").h("www.ics.uci.edu").p("/pub/ietf/uri/historical.html") .f("WARNING").z(); test("http://www.ics.uci.edu/pub/ietf/uri/#Related") .s("http").h("www.ics.uci.edu").p("/pub/ietf/uri/") .f("Related").z(); header("RFC2396: Normal relative-URI examples (appendix C)"); URI base = (test("http://a/b/c/d;p?q") .s("http").h("a").p("/b/c/d;p").q("q").z().uri()); // g:h g:h test("g:h") .s("g").o("h").z() .rslv(base).s("g").o("h").z(); // g http://a/b/c/g test("g") .p("g").z() .rslv(base).s("http").h("a").p("/b/c/g").z(); // ./g http://a/b/c/g test("./g") .p("./g").z() .rslv(base).s("http").h("a").p("/b/c/g").z(); // g/ http://a/b/c/g/ test("g/") .p("g/").z() .rslv(base).s("http").h("a").p("/b/c/g/").z(); // /g http://a/g test("/g") .p("/g").z() .rslv(base).s("http").h("a").p("/g").z(); // //g http://g test("//g") .h("g").p("").z() .rslv(base).s("http").h("g").p("").z(); // ?y http://a/b/c/?y test("?y") .p("").q("y").z() .rslv(base).s("http").h("a").p("/b/c/").q("y").z(); // g?y http://a/b/c/g?y test("g?y") .p("g").q("y").z() .rslv(base).s("http").h("a").p("/b/c/g").q("y").z(); // #s (current document)#s // DEVIATION: Lone fragment parses as relative URI with empty path test("#s") .p("").f("s").z() .rslv(base).s("http").h("a").p("/b/c/d;p").f("s").q("q").z(); // g#s http://a/b/c/g#s test("g#s") .p("g").f("s").z() .rslv(base).s("http").h("a").p("/b/c/g").f("s").z(); // g?y#s http://a/b/c/g?y#s test("g?y#s") .p("g").q("y").f("s").z() .rslv(base).s("http").h("a").p("/b/c/g").q("y").f("s").z(); // ;x http://a/b/c/;x test(";x") .p(";x").z() .rslv(base).s("http").h("a").p("/b/c/;x").z(); // g;x http://a/b/c/g;x test("g;x") .p("g;x").z() .rslv(base).s("http").h("a").p("/b/c/g;x").z(); // g;x?y#s http://a/b/c/g;x?y#s test("g;x?y#s") .p("g;x").q("y").f("s").z() .rslv(base).s("http").h("a").p("/b/c/g;x").q("y").f("s").z(); // . http://a/b/c/ test(".") .p(".").z() .rslv(base).s("http").h("a").p("/b/c/").z(); // ./ http://a/b/c/ test("./") .p("./").z() .rslv(base).s("http").h("a").p("/b/c/").z(); // .. http://a/b/ test("..") .p("..").z() .rslv(base).s("http").h("a").p("/b/").z(); // ../ http://a/b/ test("../") .p("../").z() .rslv(base).s("http").h("a").p("/b/").z(); // ../g http://a/b/g test("../g") .p("../g").z() .rslv(base).s("http").h("a").p("/b/g").z(); // ../.. http://a/ test("../..") .p("../..").z() .rslv(base).s("http").h("a").p("/").z(); // ../../ http://a/ test("../../") .p("../../").z() .rslv(base).s("http").h("a").p("/").z(); // ../../g http://a/g test("../../g") .p("../../g").z() .rslv(base).s("http").h("a").p("/g").z(); header("RFC2396: Abnormal relative-URI examples (appendix C)"); // ../../../g = http://a/../g test("../../../g") .p("../../../g").z() .rslv(base).s("http").h("a").p("/../g").z(); // ../../../../g = http://a/../../g test("../../../../g") .p("../../../../g").z() .rslv(base).s("http").h("a").p("/../../g").z(); // /./g = http://a/./g test("/./g") .p("/./g").z() .rslv(base).s("http").h("a").p("/./g").z(); // /../g = http://a/../g test("/../g") .p("/../g").z() .rslv(base).s("http").h("a").p("/../g").z(); // g. = http://a/b/c/g. test("g.") .p("g.").z() .rslv(base).s("http").h("a").p("/b/c/g.").z(); // .g = http://a/b/c/.g test(".g") .p(".g").z() .rslv(base).s("http").h("a").p("/b/c/.g").z(); // g.. = http://a/b/c/g.. test("g..") .p("g..").z() .rslv(base).s("http").h("a").p("/b/c/g..").z(); // ..g = http://a/b/c/..g test("..g") .p("..g").z() .rslv(base).s("http").h("a").p("/b/c/..g").z(); // ./../g = http://a/b/g test("./../g") .p("./../g").z() .rslv(base).s("http").h("a").p("/b/g").z(); // ./g/. = http://a/b/c/g/ test("./g/.") .p("./g/.").z() .rslv(base).s("http").h("a").p("/b/c/g/").z(); // g/./h = http://a/b/c/g/h test("g/./h") .p("g/./h").z() .rslv(base).s("http").h("a").p("/b/c/g/h").z(); // g/../h = http://a/b/c/h test("g/../h") .p("g/../h").z() .rslv(base).s("http").h("a").p("/b/c/h").z(); // g;x=1/./y = http://a/b/c/g;x=1/y test("g;x=1/./y") .p("g;x=1/./y").z() .rslv(base).s("http").h("a").p("/b/c/g;x=1/y").z(); // g;x=1/../y = http://a/b/c/y test("g;x=1/../y") .p("g;x=1/../y").z() .rslv(base).s("http").h("a").p("/b/c/y").z(); // g?y/./x = http://a/b/c/g?y/./x test("g?y/./x") .p("g").q("y/./x").z() .rslv(base).s("http").h("a").p("/b/c/g").q("y/./x").z(); // g?y/../x = http://a/b/c/g?y/../x test("g?y/../x") .p("g").q("y/../x").z() .rslv(base).s("http").h("a").p("/b/c/g").q("y/../x").z(); // g#s/./x = http://a/b/c/g#s/./x test("g#s/./x") .p("g").f("s/./x").z() .rslv(base).s("http").h("a").p("/b/c/g").f("s/./x").z(); // g#s/../x = http://a/b/c/g#s/../x test("g#s/../x") .p("g").f("s/../x").z() .rslv(base).s("http").h("a").p("/b/c/g").f("s/../x").z(); // http:g = http:g test("http:g") .s("http").o("g").z() .rslv(base).s("http").o("g").z(); } static void ip() { header("IP addresses"); test("http://1.2.3.4:5") .s("http").h("1.2.3.4").n(5).p("").z(); // From RFC2732 test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html") .s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]") .n(80).p("/index.html").z(); test("http://[1080:0:0:0:8:800:200C:417A]/index.html") .s("http").h("[1080:0:0:0:8:800:200C:417A]").p("/index.html").z(); test("http://[3ffe:2a00:100:7031::1]") .s("http").h("[3ffe:2a00:100:7031::1]").p("").z(); test("http://[1080::8:800:200C:417A]/foo") .s("http").h("[1080::8:800:200C:417A]").p("/foo").z(); test("http://[::192.9.5.5]/ipng") .s("http").h("[::192.9.5.5]").p("/ipng").z(); test("http://[::FFFF:129.144.52.38]:80/index.html") .s("http").h("[::FFFF:129.144.52.38]").n(80).p("/index.html").z(); test("http://[2010:836B:4179::836B:4179]") .s("http").h("[2010:836B:4179::836B:4179]").p("").z(); // From RFC2373 test("http://[FF01::101]") .s("http").h("[FF01::101]").p("").z(); test("http://[::1]") .s("http").h("[::1]").p("").z(); test("http://[::]") .s("http").h("[::]").p("").z(); test("http://[0:0:0:0:0:0:13.1.68.3]") .s("http").h("[0:0:0:0:0:0:13.1.68.3]").p("").z(); test("http://[0:0:0:0:0:FFFF:129.144.52.38]") .s("http").h("[0:0:0:0:0:FFFF:129.144.52.38]").p("").z(); test("http://[0:0:0:0:0:ffff:1.2.3.4]") .s("http").h("[0:0:0:0:0:ffff:1.2.3.4]").p("").z(); test("http://[::13.1.68.3]") .s("http").h("[::13.1.68.3]").p("").z(); // Optional IPv6 brackets in constructors test("s", null, "1:2:3:4:5:6:7:8", -1, null, null, null) .s("s").h("[1:2:3:4:5:6:7:8]").p("").z(); test("s", null, "[1:2:3:4:5:6:7:8]", -1, null, null, null) .s("s").h("[1:2:3:4:5:6:7:8]").p("").z(); test("s", null, "[1:2:3:4:5:6:7:8]", -1, null, null, null) .s("s").h("[1:2:3:4:5:6:7:8]").p("").z(); test("s", "1:2:3:4:5:6:7:8", null, null) .s("s").h("[1:2:3:4:5:6:7:8]").p("").z(); test("s", "[1:2:3:4:5:6:7:8]", null, null) .s("s").h("[1:2:3:4:5:6:7:8]").p("").z(); test("s", "[1:2:3:4:5:6:7:8]", null, null, null) .s("s").h("[1:2:3:4:5:6:7:8]").p("").z(); test("s", "1:2:3:4:5:6:7:8", null, null, null) .s("s").g("1:2:3:4:5:6:7:8").p("").z(); // Error cases test("http://[ff01:234/foo").x().z(); test("http://[ff01:234:zzz]/foo").x().z(); test("http://[foo]").x().z(); test("http://[]").x().z(); test("http://[129.33.44.55]").x().z(); test("http://[ff:ee:dd:cc:bb::aa:9:8]").x().z(); test("http://[fffff::1]").x().z(); test("http://[ff::ee::8]").x().z(); test("http://[1:2:3:4::5:6:7:8]").x().z(); test("http://[1:2]").x().z(); test("http://[1:2:3:4:5:6:7:8:9]").x().z(); test("http://[::1.2.3.300]").x().z(); test("http://1.2.3").psa().x().z(); test("http://1.2.3.300").psa().x().z(); test("http://1.2.3.4.5").psa().x().z(); test("http://[1.2.3.4:5]").x().z(); test("http://1:2:3:4:5:6:7:8").psa().x().z(); // URITest hostnames that might initially look like IPv4 addresses test("s://1.2.3.com").psa().s("s").h("1.2.3.com").p("").z(); test("s://1.2.3.4me.com").psa().s("s").h("1.2.3.4me.com").p("").z(); test("s://7up.com").psa().s("s").h("7up.com").p("").z(); test("s://7up.com/p").psa().s("s").h("7up.com").p("/p").z(); test("s://7up").psa().s("s").h("7up").p("").z(); test("s://7up/p").psa().s("s").h("7up").p("/p").z(); test("s://7up.").psa().s("s").h("7up.").p("").z(); test("s://7up./p").psa().s("s").h("7up.").p("/p").z(); } static void misc() throws URISyntaxException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -