scheme-blob.sub.any.js (5352B)
1 // META: script=../resources/utils.js 2 3 function checkFetchResponse(url, data, mime, size, desc) { 4 promise_test(function(test) { 5 size = size.toString(); 6 return fetch(url).then(function(resp) { 7 assert_equals(resp.status, 200, "HTTP status is 200"); 8 assert_equals(resp.type, "basic", "response type is basic"); 9 assert_equals(resp.headers.get("Content-Type"), mime, "Content-Type is " + resp.headers.get("Content-Type")); 10 assert_equals(resp.headers.get("Content-Length"), size, "Content-Length is " + resp.headers.get("Content-Length")); 11 return resp.text(); 12 }).then(function(bodyAsText) { 13 assert_equals(bodyAsText, data, "Response's body is " + data); 14 }); 15 }, desc); 16 } 17 18 var blob = new Blob(["Blob's data"], { "type" : "text/plain" }); 19 checkFetchResponse(URL.createObjectURL(blob), "Blob's data", "text/plain", blob.size, 20 "Fetching [GET] URL.createObjectURL(blob) is OK"); 21 22 function checkKoUrl(url, method, desc) { 23 promise_test(function(test) { 24 var promise = fetch(url, {"method": method}); 25 return promise_rejects_js(test, TypeError, promise); 26 }, desc); 27 } 28 29 var blob2 = new Blob(["Blob's data"], { "type" : "text/plain" }); 30 checkKoUrl("blob:http://{{domains[www]}}:{{ports[http][0]}}/", "GET", 31 "Fetching [GET] blob:http://{{domains[www]}}:{{ports[http][0]}}/ is KO"); 32 33 var invalidRequestMethods = [ 34 "POST", 35 "OPTIONS", 36 "HEAD", 37 "PUT", 38 "DELETE", 39 "INVALID", 40 ]; 41 invalidRequestMethods.forEach(function(method) { 42 checkKoUrl(URL.createObjectURL(blob2), method, "Fetching [" + method + "] URL.createObjectURL(blob) is KO"); 43 }); 44 45 checkKoUrl("blob:not-backed-by-a-blob/", "GET", 46 "Fetching [GET] blob:not-backed-by-a-blob/ is KO"); 47 48 let empty_blob = new Blob([]); 49 checkFetchResponse(URL.createObjectURL(empty_blob), "", "", 0, 50 "Fetching URL.createObjectURL(empty_blob) is OK"); 51 52 let empty_type_blob = new Blob([], {type: ""}); 53 checkFetchResponse(URL.createObjectURL(empty_type_blob), "", "", 0, 54 "Fetching URL.createObjectURL(empty_type_blob) is OK"); 55 56 let empty_data_blob = new Blob([], {type: "text/plain"}); 57 checkFetchResponse(URL.createObjectURL(empty_data_blob), "", "text/plain", 0, 58 "Fetching URL.createObjectURL(empty_data_blob) is OK"); 59 60 let invalid_type_blob = new Blob([], {type: "invalid"}); 61 checkFetchResponse(URL.createObjectURL(invalid_type_blob), "", "", 0, 62 "Fetching URL.createObjectURL(invalid_type_blob) is OK"); 63 64 promise_test(function(test) { 65 return fetch("/images/blue.png").then(function(resp) { 66 return resp.arrayBuffer(); 67 }).then(function(image_buffer) { 68 let blob = new Blob([image_buffer]); 69 return fetch(URL.createObjectURL(blob)).then(function(resp) { 70 assert_equals(resp.status, 200, "HTTP status is 200"); 71 assert_equals(resp.type, "basic", "response type is basic"); 72 assert_equals(resp.headers.get("Content-Type"), "", "Content-Type is " + resp.headers.get("Content-Type")); 73 }) 74 }); 75 }, "Blob content is not sniffed for a content type [image/png]"); 76 77 let simple_xml_string = '<?xml version="1.0" encoding="UTF-8"?><x></x>'; 78 let xml_blob_no_type = new Blob([simple_xml_string]); 79 checkFetchResponse(URL.createObjectURL(xml_blob_no_type), simple_xml_string, "", 45, 80 "Blob content is not sniffed for a content type [text/xml]"); 81 82 let simple_text_string = 'Hello, World!'; 83 promise_test(function(test) { 84 let blob = new Blob([simple_text_string], {"type": "text/plain"}); 85 let slice = blob.slice(7, simple_text_string.length, "\0"); 86 return fetch(URL.createObjectURL(slice)).then(function (resp) { 87 assert_equals(resp.status, 200, "HTTP status is 200"); 88 assert_equals(resp.type, "basic", "response type is basic"); 89 assert_equals(resp.headers.get("Content-Type"), ""); 90 assert_equals(resp.headers.get("Content-Length"), "6"); 91 return resp.text(); 92 }).then(function(bodyAsText) { 93 assert_equals(bodyAsText, "World!"); 94 }); 95 }, "Set content type to the empty string for slice with invalid content type"); 96 97 promise_test(function(test) { 98 let blob = new Blob([simple_text_string], {"type": "text/plain"}); 99 let slice = blob.slice(7, simple_text_string.length, "\0"); 100 return fetch(URL.createObjectURL(slice)).then(function (resp) { 101 assert_equals(resp.status, 200, "HTTP status is 200"); 102 assert_equals(resp.type, "basic", "response type is basic"); 103 assert_equals(resp.headers.get("Content-Type"), ""); 104 assert_equals(resp.headers.get("Content-Length"), "6"); 105 return resp.text(); 106 }).then(function(bodyAsText) { 107 assert_equals(bodyAsText, "World!"); 108 }); 109 }, "Set content type to the empty string for slice with no content type "); 110 111 promise_test(function(test) { 112 let blob = new Blob([simple_xml_string]); 113 let slice = blob.slice(0, 38); 114 return fetch(URL.createObjectURL(slice)).then(function (resp) { 115 assert_equals(resp.status, 200, "HTTP status is 200"); 116 assert_equals(resp.type, "basic", "response type is basic"); 117 assert_equals(resp.headers.get("Content-Type"), ""); 118 assert_equals(resp.headers.get("Content-Length"), "38"); 119 return resp.text(); 120 }).then(function(bodyAsText) { 121 assert_equals(bodyAsText, '<?xml version="1.0" encoding="UTF-8"?>'); 122 }); 123 }, "Blob.slice should not sniff the content for a content type"); 124 125 done();