Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / resources / fetch-body-mixin-worker.js
1 self.onmessage = function(e) {
2     var message = e.data;
3     if ('port' in message) {
4         port = message.port;
5         doTextTest(port);
6     }
7 };
8
9 function quit(port) {
10     port.postMessage('quit');
11 }
12
13 function doFetchTwiceTest(port) {
14   var p1Out = p2Out = null;
15
16   fetch('doctype.html')
17   .then(function(response) {
18       var p1 = response.text();
19       var p2 = response.text();
20
21       p1.then(function(obj) {
22           p1Out = obj;
23           if (p2Out) {
24               complete();
25           }
26       });
27       p2.catch(function(e) {
28           p2Out = e;
29           if (p1Out) {
30               complete();
31           }
32       });
33   });
34
35   function complete() {
36       port.postMessage(p1Out + ' : ' + p2Out.name);
37       quit(port);
38   }
39 }
40
41 function doArrayBufferTest(port) {
42     fetch('doctype.html')
43     .then(function(response) {
44         response.arrayBuffer()
45         .then(function(b) {
46             port.postMessage('ArrayBuffer: ' + b.byteLength);
47             doFetchTwiceTest(port);
48         });
49     });
50 }
51
52 function doBlobTest(port) {
53     fetch('doctype.html')
54     .then(function(response) {
55         response.blob()
56         .then(function(blob) {
57             port.postMessage('Blob: ' + blob.size + " : " + blob.type);
58             doArrayBufferTest(port);
59         });
60     });
61 }
62
63 function doJSONFailedTest(port) {
64   fetch('doctype.html')
65   .then(function(response) {
66       response.json()
67       .catch(function(e) {
68           port.postMessage('JSON: ' + e.name);
69           doBlobTest(port);
70       });
71   });
72 }
73
74 function doJSONTest(port) {
75     fetch('simple.json')
76     .then(function(response) {
77         response.json()
78         .then(function(json) {
79             port.postMessage('JSON: ' + json['a'] + ' ' + json['b']);
80             doJSONFailedTest(port);
81         });
82     });
83 }
84
85 function doTextTest(port) {
86   fetch('doctype.html')
87   .then(function(response) {
88       response.text()
89       .then(function(txt) {
90           port.postMessage('Text: ' + txt);
91           doJSONTest(port);
92       });
93   });
94 }