test_unicode-url.js (7572B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Tests utility functions contained in `unicode-url.js` 8 */ 9 10 const { 11 getUnicodeUrl, 12 getUnicodeUrlPath, 13 getUnicodeHostname, 14 } = require("resource://devtools/client/shared/unicode-url.js"); 15 16 // List of URLs used to test Unicode URL conversion 17 const TEST_URLS = [ 18 // Type: Readable ASCII URLs 19 // Expected: All of Unicode versions should equal to the raw. 20 { 21 raw: "https://example.org", 22 expectedUnicode: "https://example.org", 23 }, 24 { 25 raw: "http://example.org", 26 expectedUnicode: "http://example.org", 27 }, 28 { 29 raw: "ftp://example.org", 30 expectedUnicode: "ftp://example.org", 31 }, 32 { 33 raw: "https://example.org.", 34 expectedUnicode: "https://example.org.", 35 }, 36 { 37 raw: "https://example.org/", 38 expectedUnicode: "https://example.org/", 39 }, 40 { 41 raw: "https://example.org/test", 42 expectedUnicode: "https://example.org/test", 43 }, 44 { 45 raw: "https://example.org/test.html", 46 expectedUnicode: "https://example.org/test.html", 47 }, 48 { 49 raw: "https://example.org/test.html?one=1&two=2", 50 expectedUnicode: "https://example.org/test.html?one=1&two=2", 51 }, 52 { 53 raw: "https://example.org/test.html#here", 54 expectedUnicode: "https://example.org/test.html#here", 55 }, 56 { 57 raw: "https://example.org/test.html?one=1&two=2#here", 58 expectedUnicode: "https://example.org/test.html?one=1&two=2#here", 59 }, 60 // Type: Unreadable URLs with either Punycode domain names or URI-encoded 61 // paths 62 // Expected: Unreadable domain names and URI-encoded paths should be converted 63 // to readable Unicode. 64 { 65 raw: "https://xn--g6w.xn--8pv/test.html", 66 // Do not type Unicode characters directly, because this test file isn't 67 // specified with a known encoding. 68 expectedUnicode: "https://\u6e2c.\u672c/test.html", 69 }, 70 { 71 raw: "https://example.org/%E6%B8%AC%E8%A9%A6.html", 72 // Do not type Unicode characters directly, because this test file isn't 73 // specified with a known encoding. 74 expectedUnicode: "https://example.org/\u6e2c\u8a66.html", 75 }, 76 { 77 raw: "https://example.org/test.html?One=%E4%B8%80", 78 // Do not type Unicode characters directly, because this test file isn't 79 // specified with a known encoding. 80 expectedUnicode: "https://example.org/test.html?One=\u4e00", 81 }, 82 { 83 raw: "https://example.org/test.html?%E4%B8%80=1", 84 // Do not type Unicode characters directly, because this test file isn't 85 // specified with a known encoding. 86 expectedUnicode: "https://example.org/test.html?\u4e00=1", 87 }, 88 { 89 raw: 90 "https://xn--g6w.xn--8pv/%E6%B8%AC%E8%A9%A6.html" + 91 "?%E4%B8%80=%E4%B8%80" + 92 "#%E6%AD%A4", 93 // Do not type Unicode characters directly, because this test file isn't 94 // specified with a known encoding. 95 expectedUnicode: 96 "https://\u6e2c.\u672c/\u6e2c\u8a66.html" + "?\u4e00=\u4e00" + "#\u6b64", 97 }, 98 // Type: data: URIs 99 // Expected: All should not be converted. 100 { 101 raw: "data:text/plain;charset=UTF-8;Hello%20world", 102 expectedUnicode: "data:text/plain;charset=UTF-8;Hello%20world", 103 }, 104 { 105 raw: "data:text/plain;charset=UTF-8;%E6%B8%AC%20%E8%A9%A6", 106 expectedUnicode: "data:text/plain;charset=UTF-8;%E6%B8%AC%20%E8%A9%A6", 107 }, 108 { 109 raw: 110 "data:image/png;base64,iVBORw0KGgoAAA" + 111 "ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" + 112 "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" + 113 "5ErkJggg==", 114 expectedUnicode: 115 "data:image/png;base64,iVBORw0KGgoAAA" + 116 "ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" + 117 "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" + 118 "5ErkJggg==", 119 }, 120 // Type: Malformed URLs 121 // Expected: All should not be converted. 122 { 123 raw: "://example.org/test", 124 expectedUnicode: "://example.org/test", 125 }, 126 { 127 raw: "://xn--g6w.xn--8pv/%E6%B8%AC%E8%A9%A6.html" + "?%E4%B8%80=%E4%B8%80", 128 expectedUnicode: 129 "://xn--g6w.xn--8pv/%E6%B8%AC%E8%A9%A6.html" + "?%E4%B8%80=%E4%B8%80", 130 }, 131 { 132 // %E8%A9 isn't a valid UTF-8 code, so this URL is malformed. 133 raw: "https://xn--g6w.xn--8pv/%E6%B8%AC%E8%A9", 134 expectedUnicode: "https://xn--g6w.xn--8pv/%E6%B8%AC%E8%A9", 135 }, 136 ]; 137 138 // List of hostanmes used to test Unicode hostname conversion 139 const TEST_HOSTNAMES = [ 140 // Type: Readable ASCII hostnames 141 // Expected: All of Unicode versions should equal to the raw. 142 { 143 raw: "example", 144 expectedUnicode: "example", 145 }, 146 { 147 raw: "example.org", 148 expectedUnicode: "example.org", 149 }, 150 // Type: Unreadable Punycode hostnames 151 // Expected: Punycode should be converted to readable Unicode. 152 { 153 raw: "xn--g6w", 154 // Do not type Unicode characters directly, because this test file isn't 155 // specified with a known encoding. 156 expectedUnicode: "\u6e2c", 157 }, 158 { 159 raw: "xn--g6w.xn--8pv", 160 // Do not type Unicode characters directly, because this test file isn't 161 // specified with a known encoding. 162 expectedUnicode: "\u6e2c.\u672c", 163 }, 164 ]; 165 166 // List of URL paths used to test Unicode URL path conversion 167 const TEST_URL_PATHS = [ 168 // Type: Readable ASCII URL paths 169 // Expected: All of Unicode versions should equal to the raw. 170 { 171 raw: "test", 172 expectedUnicode: "test", 173 }, 174 { 175 raw: "/", 176 expectedUnicode: "/", 177 }, 178 { 179 raw: "/test", 180 expectedUnicode: "/test", 181 }, 182 { 183 raw: "/test.html?one=1&two=2#here", 184 expectedUnicode: "/test.html?one=1&two=2#here", 185 }, 186 // Type: Unreadable URI-encoded URL paths 187 // Expected: URL paths should be converted to readable Unicode. 188 { 189 raw: "/%E6%B8%AC%E8%A9%A6", 190 // Do not type Unicode characters directly, because this test file isn't 191 // specified with a known encoding. 192 expectedUnicode: "/\u6e2c\u8a66", 193 }, 194 { 195 raw: "/%E6%B8%AC%E8%A9%A6.html", 196 // Do not type Unicode characters directly, because this test file isn't 197 // specified with a known encoding. 198 expectedUnicode: "/\u6e2c\u8a66.html", 199 }, 200 { 201 raw: 202 "/%E6%B8%AC%E8%A9%A6.html" + 203 "?%E4%B8%80=%E4%B8%80&%E4%BA%8C=%E4%BA%8C" + 204 "#%E6%AD%A4", 205 // Do not type Unicode characters directly, because this test file isn't 206 // specified with a known encoding. 207 expectedUnicode: 208 "/\u6e2c\u8a66.html" + "?\u4e00=\u4e00&\u4e8c=\u4e8c" + "#\u6b64", 209 }, 210 // Type: Malformed URL paths 211 // Expected: All should not be converted. 212 { 213 // %E8%A9 isn't a valid UTF-8 code, so this URL is malformed. 214 raw: "/%E6%B8%AC%E8%A9", 215 expectedUnicode: "/%E6%B8%AC%E8%A9", 216 }, 217 ]; 218 219 function run_test() { 220 // Test URLs 221 for (const url of TEST_URLS) { 222 const result = getUnicodeUrl(url.raw); 223 equal( 224 result, 225 url.expectedUnicode, 226 "Test getUnicodeUrl: " + 227 url.raw + 228 " should be unicodized to " + 229 url.expectedUnicode 230 ); 231 } 232 233 // Test hostnames 234 for (const hostname of TEST_HOSTNAMES) { 235 const result = getUnicodeHostname(hostname.raw); 236 equal( 237 result, 238 hostname.expectedUnicode, 239 "Test getUnicodeHostname: " + 240 hostname.raw + 241 " should be unicodized to " + 242 hostname.expectedUnicode 243 ); 244 } 245 246 // Test URL paths 247 for (const urlPath of TEST_URL_PATHS) { 248 const result = getUnicodeUrlPath(urlPath.raw); 249 equal( 250 result, 251 urlPath.expectedUnicode, 252 "Test getUnicodeUrlPath: " + 253 urlPath.raw + 254 " should be unicodized to " + 255 urlPath.expectedUnicode 256 ); 257 } 258 }