1 importScripts('worker-test-harness.js');
2 importScripts('test-helpers.js');
4 async_test(function(t) {
7 unreached_rejection(t, 'fetch of invalid URL must fail'),
9 assert_equals(e.message, 'Invalid URL');
12 .catch(unreached_rejection(t));
13 }, 'Fetch invalid URL in ServiceWorkerGlobalScope');
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');
22 .catch(unreached_rejection(t));
23 }, 'Fetch result of 200 response in ServiceWorkerGlobalScope');
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');
32 .catch(unreached_rejection(t));
33 }, 'Fetch result of 404 response in ServiceWorkerGlobalScope');
35 function evalJsonp(text) {
36 return new Promise(function(resolve) {
38 // text must contain report() call.
43 async_test(function(t) {
45 new Request('fetch-access-control.php',
48 body: new Blob(['Test Blob'], {type: 'test/type'})
51 .then(function(response) { return response.text(); })
53 .then(function(result) {
54 assert_equals(result.method, 'POST');
55 assert_equals(result.body, 'Test Blob');
58 .catch(unreached_rejection(t));
59 }, 'Fetch with Blob body test in ServiceWorkerGlobalScope');
61 async_test(function(t) {
62 var request = new Request('fetch-access-control.php',
63 {method: 'POST', body: 'Test String'});
65 .then(function(response) { return response.text(); })
67 .then(function(result) {
68 assert_equals(result.method, 'POST');
69 assert_equals(result.body, 'Test String');
72 .catch(unreached_rejection(t));
73 }, 'Fetch with string body test in ServiceWorkerGlobalScope');
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});
83 .then(function(response) { return response.text(); })
85 .then(function(result) {
86 assert_equals(result.method, 'POST');
87 assert_equals(result.body, 'Test ArrayBuffer');
90 .catch(unreached_rejection(t));
91 }, 'Fetch with ArrayBuffer body test in ServiceWorkerGlobalScope');
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});
101 .then(function(response) { return response.text(); })
103 .then(function(result) {
104 assert_equals(result.method, 'POST');
105 assert_equals(result.body, 'Test ArrayBufferView');
108 .catch(unreached_rejection(t));
109 }, 'Fetch with ArrayBufferView body test in ServiceWorkerGlobalScope');
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});
121 .then(function(response) { return response.text(); })
123 .then(function(result) {
124 assert_equals(result.method, 'POST');
125 assert_equals(result.post['StringKey1'], '1234567890');
126 assert_equals(result.post['StringKey2'], 'ABCDEFGHIJ');
128 for (var i = 0; i < result.files.length; ++i) {
129 files[result.files[i].key] = result.files[i];
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);
139 .catch(unreached_rejection(t));
140 }, 'Fetch with FormData body test in ServiceWorkerGlobalScope');
143 function runInfiniteFetchLoop() {
145 .then(function() { runInfiniteFetchLoop(); });
147 runInfiniteFetchLoop();
149 'Destroying the execution context while fetch is happening should not ' +