|
| 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 | +promise_test(function(test) { |
| 61 | + return fetch("/images/blue.png").then(function(resp) { |
| 62 | + return resp.arrayBuffer(); |
| 63 | + }).then(function(image_buffer) { |
| 64 | + let blob = new Blob([image_buffer]); |
| 65 | + return fetch(URL.createObjectURL(blob)).then(function(resp) { |
| 66 | + assert_equals(resp.status, 200, "HTTP status is 200"); |
| 67 | + assert_equals(resp.type, "basic", "response type is basic"); |
| 68 | + assert_equals(resp.headers.get("Content-Type"), "", "Content-Type is " + resp.headers.get("Content-Type")); |
| 69 | + }) |
| 70 | + }); |
| 71 | +}, "Blob content is not sniffed for a content type [image/png]"); |
| 72 | + |
| 73 | +let simple_xml_string = '<?xml version="1.0" encoding="UTF-8"?><x></x>'; |
| 74 | +let xml_blob_no_type = new Blob([simple_xml_string]); |
| 75 | +checkFetchResponse(URL.createObjectURL(xml_blob_no_type), simple_xml_string, "", 45, |
| 76 | + "Blob content is not sniffed for a content type [text/xml]"); |
| 77 | + |
| 78 | +promise_test(function(test) { |
| 79 | + let blob = new Blob([], {"type": "text/plain"}); |
| 80 | + let slice = blob.slice(8, 25, "\0"); |
| 81 | + return fetch(URL.createObjectURL(slice)).then(function (resp) { |
| 82 | + assert_equals(resp.status, 200, "HTTP status is 200"); |
| 83 | + assert_equals(resp.type, "basic", "response type is basic"); |
| 84 | + assert_equals(resp.headers.get("Content-Type"), ""); |
| 85 | + assert_equals(resp.headers.get("Content-Length"), "17"); |
| 86 | + return resp.text(); |
| 87 | + }).then(function(bodyAsText) { |
| 88 | + assert_equals(bodyAsText, "type with invalid"); |
| 89 | + }); |
| 90 | +}, "Set content type to the empty string for slice with invalid content type"); |
| 91 | + |
| 92 | +promise_test(function(test) { |
| 93 | + let blob = new Blob([], {"type": "text/plain"}); |
| 94 | + let slice = blob.slice(8, 20); |
| 95 | + return fetch(URL.createObjectURL(slice)).then(function (resp) { |
| 96 | + assert_equals(resp.status, 200, "HTTP status is 200"); |
| 97 | + assert_equals(resp.type, "basic", "response type is basic"); |
| 98 | + assert_equals(resp.headers.get("Content-Type"), ""); |
| 99 | + assert_equals(resp.headers.get("Content-Length"), "12"); |
| 100 | + return resp.text(); |
| 101 | + }).then(function(bodyAsText) { |
| 102 | + assert_equals(bodyAsText, "type that is"); |
| 103 | + }); |
| 104 | +}, "Set content type to the empty string for slice with no content type "); |
| 105 | + |
| 106 | +promise_test(function(test) { |
| 107 | + let blob = new Blob([simple_xml_string]); |
| 108 | + let slice = blob.slice(0, 38); |
| 109 | + return fetch(URL.createObjectURL(slice)).then(function (resp) { |
| 110 | + assert_equals(resp.status, 200, "HTTP status is 200"); |
| 111 | + assert_equals(resp.type, "basic", "response type is basic"); |
| 112 | + assert_equals(resp.headers.get("Content-Type"), ""); |
| 113 | + assert_equals(resp.headers.get("Content-Length"), "38"); |
| 114 | + return resp.text(); |
| 115 | + }).then(function(bodyAsText) { |
| 116 | + assert_equals(bodyAsText, '<?xml version="1.0" encoding="UTF-8"?>'); |
| 117 | + }); |
| 118 | +}, "Blob.slice should not sniff the content for a content type"); |
| 119 | + |
| 120 | +done(); |
0 commit comments