ef77d59033f4a0d0a545b5dad05e3f33fd27922d
[WebKit-https.git] / LayoutTests / http / tests / websocket / tests / hybi / send-file-blob-fail.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../../../js-test-resources/js-test-pre.js"></script>
5 </head>
6 <body>
7 <div id="description"></div>
8 <div id="console"></div>
9 <script type="text/javascript">
10 description("WebSocket should fail the connection if it has failed to read a Blob.");
11
12 window.jsTestIsAsync = true;
13 if (window.testRunner)
14     testRunner.overridePreference("WebKitHixie76WebSocketProtocolEnabled", 0);
15
16 function startsWith(target, prefix)
17 {
18     return target.indexOf(prefix) === 0;
19 }
20
21 var fileSystemSize = 1024;
22 var fileName = "websocket-send-file-blob-fail.txt";
23 var messageToWrite = "This message shouldn't be sent.";
24
25 function runTest()
26 {
27     if (!window.webkitRequestFileSystem) {
28         testFailed("window.webkitRequestFileSystem is not available.");
29         finishJSTest();
30         return;
31     }
32
33     webkitRequestFileSystem(TEMPORARY, fileSystemSize, didGetFileSystem, didFail);
34 }
35
36 function didGetFileSystem(fileSystem)
37 {
38     debug("Got FileSystem object.");
39     fileSystem.root.getFile(fileName, {create: true}, didCreateFile, didFail);
40 }
41
42 var fileEntry;
43
44 function didCreateFile(entry)
45 {
46     debug("File created.");
47     fileEntry = entry;
48     shouldBeTrue("fileEntry.isFile");
49     fileEntry.createWriter(didGetFileWriter, didFail);
50 }
51
52 function didGetFileWriter(writer)
53 {
54     writer.truncate(0);
55     writer.onerror = function()
56     {
57         testFailed("FileWriter operation failed.");
58         endTest();
59     };
60     writer.onwrite = function()
61     {
62         writer.write(new Blob([messageToWrite]));
63         writer.onwrite = didWriteFile;
64     };
65 }
66
67 function didWriteFile()
68 {
69     debug("Wrote to file.");
70     fileEntry.file(didGetFile, didFail);
71 }
72
73 var fileObject;
74
75 function didGetFile(file)
76 {
77     debug("Got File object.");
78     fileObject = file;
79
80     // Delete the file object before it is read. This should cause the subsequent read operation
81     // to fail reliably.
82     fileEntry.remove(didRemoveFile, didFail);
83 }
84
85 function didRemoveFile()
86 {
87     debug("File deleted.");
88     fileEntry = null; // To prevent the file from getting deleted again.
89     var ws = new WebSocket("ws://127.0.0.1:8880/websocket/tests/hybi/echo");
90     ws.onopen = function()
91     {
92         ws.send(fileObject); // This operation should fail and the connection should be aborted.
93     };
94     ws.onclose = function(event)
95     {
96         closeEvent = event;
97         shouldBeFalse("closeEvent.wasClean");
98         endTest();
99     };
100 }
101
102 function didFail(fileError)
103 {
104     testFailed("FileSystem API operation failed: error code = " + fileError.code);
105     endTest();
106 }
107
108 function endTest()
109 {
110     if (fileEntry) {
111         debug("Deleting the file.");
112         fileEntry.remove(finishJSTest, finishJSTest);
113     } else
114         finishJSTest();
115 }
116
117 runTest();
118
119 </script>
120 <script src="../../../../js-test-resources/js-test-post.js"></script>
121 </body>
122 </html>