path.js revision 430b6b672341c7e8b5e4cfafaaae20315e68701b
1description("Canonicalization of paths."); 2 3cases = [ 4 ["/././foo", "/foo"], 5 ["/./.foo", "/.foo"], 6 ["/foo/.", "/foo/"], 7 ["/foo/./", "/foo/"], 8 // double dots followed by a slash or the end of the string count 9 ["/foo/bar/..", "/foo/"], 10 ["/foo/bar/../", "/foo/"], 11 // don't count double dots when they aren't followed by a slash 12 ["/foo/..bar", "/foo/..bar"], 13 // some in the middle 14 ["/foo/bar/../ton", "/foo/ton"], 15 ["/foo/bar/../ton/../../a", "/a"], 16 // we should not be able to go above the root 17 ["/foo/../../..", "/"], 18 ["/foo/../../../ton", "/ton"], 19 // escaped dots should be unescaped and treated the same as dots 20 ["/foo/%2e", "/foo/"], 21 ["/foo/%2e%2", "/foo/.%2"], 22 ["/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar"], 23 // Multiple slashes in a row should be preserved and treated like empty 24 // directory names. 25 ["////../..", "//"], 26 27 // ----- escaping tests ----- 28 ["/foo", "/foo"], 29 // Valid escape sequence 30 ["/%20foo", "/%20foo"], 31 // Invalid escape sequence we should pass through unchanged. 32 ["/foo%", "/foo%"], 33 ["/foo%2", "/foo%2"], 34 // Invalid escape sequence: bad characters should be treated the same as 35 // the sourrounding text, not as escaped (in this case, UTF-8). 36 ["/foo%2zbar", "/foo%2zbar"], 37 // (Disabled because requires UTF8) 38 // ["/foo%2\xc2\xa9zbar", "/foo%2%C2%A9zbar"], 39 ["/foo%2\u00c2\u00a9zbar", "/foo%2%C3%82%C2%A9zbar"], 40 // Regular characters that are escaped should be unescaped 41 ["/foo%41%7a", "/fooAz"], 42 // Funny characters that are unescaped should be escaped 43 ["/foo\u0009\u0091%91", "/foo%09%C2%91%91"], 44 // Invalid characters that are escaped should cause a failure. 45 ["/foo%00%51", "/foo%00Q"], 46 // Some characters should be passed through unchanged regardless of esc. 47 ["/(%28:%3A%29)", "/(%28:%3A%29)"], 48 // Characters that are properly escaped should not have the case changed 49 // of hex letters. 50 ["/%3A%3a%3C%3c", "/%3A%3a%3C%3c"], 51 // Funny characters that are unescaped should be escaped 52 ["/foo\tbar", "/foo%09bar"], 53 // Backslashes should get converted to forward slashes 54 ["\\\\foo\\\\bar", "/foo/bar"], 55 // Hashes found in paths (possibly only when the caller explicitly sets 56 // the path on an already-parsed URL) should be escaped. 57 // (Disabled because requires ability to set path directly.) 58 // ["/foo#bar", "/foo%23bar"], 59 // %7f should be allowed and %3D should not be unescaped (these were wrong 60 // in a previous version). 61 ["/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd"], 62 // @ should be passed through unchanged (escaped or unescaped). 63 ["/@asdf%40", "/@asdf%40"], 64 65 // ----- encoding tests ----- 66 // Basic conversions 67 ["/\u4f60\u597d\u4f60\u597d", "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD"], 68 // Invalid unicode characters should fail. We only do validation on 69 // UTF-16 input, so this doesn't happen on 8-bit. 70 ["/\ufdd0zyx", "/%EF%BF%BDzyx"], 71 72]; 73 74for (var i = 0; i < cases.length; ++i) { 75 test_vector = cases[i][0]; 76 expected_result = cases[i][1]; 77 shouldBe("canonicalize('http://example.com" + test_vector + "')", 78 "'http://example.com" + expected_result + "'"); 79} 80 81var successfullyParsed = true; 82