test_blob_worker_xhr_post.html (3311B)
1 <!-- 2 Any copyright is dedicated to the Public Domain. 3 http://creativecommons.org/publicdomain/zero/1.0/ 4 --> 5 <html> 6 <head> 7 <title>Indexed Database Property Test</title> 8 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 12 <script type="text/javascript"> 13 function* testSteps() 14 { 15 const BLOB_DATA = ["fun ", "times ", "all ", "around!"]; 16 const BLOB_TYPE = "text/plain"; 17 const BLOB_SIZE = BLOB_DATA.join("").length; 18 19 info("Setting up"); 20 21 let request = indexedDB.open(window.location.pathname, 1); 22 request.onerror = errorHandler; 23 request.onupgradeneeded = grabEventAndContinueHandler; 24 request.onsuccess = unexpectedSuccessHandler; 25 let event = yield undefined; 26 27 let db = event.target.result; 28 db.onerror = errorHandler; 29 30 ok(db, "Created database"); 31 32 info("Creating objectStore"); 33 34 let objectStore = db.createObjectStore("foo", { autoIncrement: true }); 35 36 request.onupgradeneeded = unexpectedSuccessHandler; 37 request.onsuccess = grabEventAndContinueHandler; 38 event = yield undefined; 39 40 ok(true, "Opened database"); 41 42 let blob = new Blob(BLOB_DATA, { type: BLOB_TYPE }); 43 44 info("Adding blob to database"); 45 46 objectStore = db.transaction("foo", "readwrite").objectStore("foo"); 47 objectStore.add(blob).onsuccess = grabEventAndContinueHandler; 48 event = yield undefined; 49 50 let blobKey = event.target.result; 51 ok(blobKey, "Got a key for the blob"); 52 53 info("Getting blob from the database"); 54 55 objectStore = db.transaction("foo").objectStore("foo"); 56 objectStore.get(blobKey).onsuccess = grabEventAndContinueHandler; 57 event = yield undefined; 58 59 blob = event.target.result; 60 61 ok(blob instanceof Blob, "Got a blob"); 62 is(blob.size, BLOB_SIZE, "Correct size"); 63 is(blob.type, BLOB_TYPE, "Correct type"); 64 65 let slice = blob.slice(0, BLOB_DATA[0].length, BLOB_TYPE); 66 67 ok(slice instanceof Blob, "Slice returned a blob"); 68 is(slice.size, BLOB_DATA[0].length, "Correct size for slice"); 69 is(slice.type, BLOB_TYPE, "Correct type for slice"); 70 71 info("Sending slice to a worker"); 72 73 function workerScript() { 74 /* eslint-env worker */ 75 onmessage = function(event) { 76 var blob = event.data; 77 var xhr = new XMLHttpRequest(); 78 // We just want to make sure the error case doesn't fire; it's fine for 79 // us to just want a 404. 80 xhr.open("POST", "http://mochi.test:8888/does-not-exist", true); 81 xhr.onload = function() { 82 postMessage({ status: xhr.status }); 83 }; 84 xhr.onerror = function() { 85 postMessage({ status: "error" }); 86 }; 87 xhr.send(blob); 88 }; 89 } 90 91 let workerScriptUrl = 92 URL.createObjectURL(new Blob(["(", workerScript.toString(), ")()"])); 93 94 let xhrWorker = new Worker(workerScriptUrl); 95 xhrWorker.postMessage(slice); 96 xhrWorker.onmessage = grabEventAndContinueHandler; 97 event = yield undefined; 98 99 is(event.data.status, 404, "XHR generated the expected 404"); 100 xhrWorker.terminate(); 101 102 URL.revokeObjectURL(workerScriptUrl); 103 104 finishTest(); 105 } 106 </script> 107 <script type="text/javascript" src="helpers.js"></script> 108 109 </head> 110 111 <body onload="runTest();"></body> 112 113 </html>