Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / resources / fetch-worker.js
1 importScripts('worker-test-harness.js');
2 importScripts('test-helpers.js');
3
4 async_test(function(t) {
5     fetch('http://')
6       .then(
7         unreached_rejection(t, 'fetch of invalid URL must fail'),
8         function(e) {
9           assert_equals(e.message, 'Invalid URL');
10           t.done();
11         })
12       .catch(unreached_rejection(t));
13   }, 'Fetch invalid URL in ServiceWorkerGlobalScope');
14
15 async_test(function(t) {
16     fetch('fetch-status.php?status=200')
17       .then(function(response) {
18           assert_equals(response.status, 200);
19           assert_equals(response.statusText, 'OK');
20           t.done();
21         })
22       .catch(unreached_rejection(t));
23   }, 'Fetch result of 200 response in ServiceWorkerGlobalScope');
24
25 async_test(function(t) {
26     fetch('fetch-status.php?status=404')
27       .then(function(response) {
28           assert_equals(response.status, 404);
29           assert_equals(response.statusText, 'Not Found');
30           t.done();
31         })
32       .catch(unreached_rejection(t));
33   }, 'Fetch result of 404 response in ServiceWorkerGlobalScope');
34
35 function evalJsonp(text) {
36   return new Promise(function(resolve) {
37       var report = resolve;
38       // text must contain report() call.
39       eval(text);
40     });
41 }
42
43 async_test(function(t) {
44     var request =
45       new Request('fetch-access-control.php',
46                   {
47                     method: 'POST',
48                     body: new Blob(['Test Blob'], {type: 'test/type'})
49                   });
50     fetch(request)
51       .then(function(response) { return response.text(); })
52       .then(evalJsonp)
53       .then(function(result) {
54           assert_equals(result.method, 'POST');
55           assert_equals(result.body, 'Test Blob');
56           t.done();
57         })
58       .catch(unreached_rejection(t));
59   }, 'Fetch with Blob body test in ServiceWorkerGlobalScope');
60
61 async_test(function(t) {
62     var request = new Request('fetch-access-control.php',
63                               {method: 'POST', body: 'Test String'});
64     fetch(request)
65       .then(function(response) { return response.text(); })
66       .then(evalJsonp)
67       .then(function(result) {
68           assert_equals(result.method, 'POST');
69           assert_equals(result.body, 'Test String');
70           t.done();
71         })
72       .catch(unreached_rejection(t));
73   }, 'Fetch with string body test in ServiceWorkerGlobalScope');
74
75 async_test(function(t) {
76     var text = "Test ArrayBuffer";
77     var array = new Uint8Array(text.length);
78     for (var i = 0; i < text.length; ++i)
79       array[i] = text.charCodeAt(i);
80     var request = new Request('fetch-access-control.php',
81                               {method: 'POST', body: array.buffer});
82     fetch(request)
83       .then(function(response) { return response.text(); })
84       .then(evalJsonp)
85       .then(function(result) {
86           assert_equals(result.method, 'POST');
87           assert_equals(result.body, 'Test ArrayBuffer');
88           t.done();
89         })
90       .catch(unreached_rejection(t));
91   }, 'Fetch with ArrayBuffer body test in ServiceWorkerGlobalScope');
92
93 async_test(function(t) {
94     var text = "Test ArrayBufferView";
95     var array = new Uint8Array(text.length);
96     for (var i = 0; i < text.length; ++i)
97       array[i] = text.charCodeAt(i);
98     var request = new Request('fetch-access-control.php',
99                               {method: 'POST', body: array});
100     fetch(request)
101       .then(function(response) { return response.text(); })
102       .then(evalJsonp)
103       .then(function(result) {
104           assert_equals(result.method, 'POST');
105           assert_equals(result.body, 'Test ArrayBufferView');
106           t.done();
107         })
108       .catch(unreached_rejection(t));
109   }, 'Fetch with ArrayBufferView body test in ServiceWorkerGlobalScope');
110
111 async_test(function(t) {
112     var formData = new FormData();
113     formData.append('StringKey1', '1234567890');
114     formData.append('StringKey2', 'ABCDEFGHIJ');
115     formData.append('BlobKey', new Blob(['blob content']));
116     formData.append('FileKey',
117                     new File(['file content'], 'file.dat'));
118     var request = new Request('fetch-access-control.php',
119                           {method: 'POST', body: formData});
120     fetch(request)
121       .then(function(response) { return response.text(); })
122       .then(evalJsonp)
123       .then(function(result) {
124           assert_equals(result.method, 'POST');
125           assert_equals(result.post['StringKey1'], '1234567890');
126           assert_equals(result.post['StringKey2'], 'ABCDEFGHIJ');
127           var files = [];
128           for (var i = 0; i < result.files.length; ++i) {
129             files[result.files[i].key] = result.files[i];
130           }
131           assert_equals(files['BlobKey'].content, 'blob content');
132           assert_equals(files['BlobKey'].name, 'blob');
133           assert_equals(files['BlobKey'].size, 12);
134           assert_equals(files['FileKey'].content, 'file content');
135           assert_equals(files['FileKey'].name, 'file.dat');
136           assert_equals(files['FileKey'].size, 12);
137           t.done();
138         })
139       .catch(unreached_rejection(t));
140   }, 'Fetch with FormData body test in ServiceWorkerGlobalScope');
141
142 test(function(t) {
143     function runInfiniteFetchLoop() {
144       fetch('dummy.html')
145         .then(function() { runInfiniteFetchLoop(); });
146     }
147     runInfiniteFetchLoop();
148   },
149   'Destroying the execution context while fetch is happening should not ' +
150       'cause a crash.');