url-format.any.js (2388B)
1 // META: timeout=long 2 const blob = new Blob(['test']); 3 const file = new File(['test'], 'name'); 4 5 test(t => { 6 const url_count = 5000; 7 let list = []; 8 9 t.add_cleanup(() => { 10 for (let url of list) { 11 URL.revokeObjectURL(url); 12 } 13 }); 14 15 for (let i = 0; i < url_count; ++i) 16 list.push(URL.createObjectURL(blob)); 17 18 list.sort(); 19 20 for (let i = 1; i < list.length; ++i) 21 assert_not_equals(list[i], list[i-1], 'generated Blob URLs should be unique'); 22 }, 'Generated Blob URLs are unique'); 23 24 test(() => { 25 const url = URL.createObjectURL(blob); 26 assert_equals(typeof url, 'string'); 27 assert_true(url.startsWith('blob:')); 28 }, 'Blob URL starts with "blob:"'); 29 30 test(() => { 31 const url = URL.createObjectURL(file); 32 assert_equals(typeof url, 'string'); 33 assert_true(url.startsWith('blob:')); 34 }, 'Blob URL starts with "blob:" for Files'); 35 36 test(() => { 37 const url = URL.createObjectURL(blob); 38 assert_equals(new URL(url).origin, location.origin); 39 if (location.origin !== 'null') { 40 assert_true(url.includes(location.origin)); 41 assert_true(url.startsWith('blob:' + location.protocol)); 42 } 43 }, 'Origin of Blob URL matches our origin'); 44 45 test(() => { 46 const url = URL.createObjectURL(blob); 47 const url_record = new URL(url); 48 assert_equals(url_record.protocol, 'blob:'); 49 assert_equals(url_record.origin, location.origin); 50 assert_equals(url_record.host, '', 'host should be an empty string'); 51 assert_equals(url_record.port, '', 'port should be an empty string'); 52 const uuid_path_re = /\/[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; 53 assert_true(uuid_path_re.test(url_record.pathname), 'Path must end with a valid UUID'); 54 if (location.origin !== 'null') { 55 const nested_url = new URL(url_record.pathname); 56 assert_equals(nested_url.origin, location.origin); 57 assert_equals(nested_url.pathname.search(uuid_path_re), 0, 'Path must be a valid UUID'); 58 assert_true(url.includes(location.origin)); 59 assert_true(url.startsWith('blob:' + location.protocol)); 60 } 61 }, 'Blob URL parses correctly'); 62 63 test(() => { 64 const url = URL.createObjectURL(file); 65 assert_equals(new URL(url).origin, location.origin); 66 if (location.origin !== 'null') { 67 assert_true(url.includes(location.origin)); 68 assert_true(url.startsWith('blob:' + location.protocol)); 69 } 70 }, 'Origin of Blob URL matches our origin for Files');