1 importScripts('worker-test-harness.js');
4 var response = new Response(new Blob());
5 assert_equals(response.type, 'default', 'Default Response.type should be \'default\'');
6 assert_equals(response.url, '', 'Response.url should be the empty string');
7 assert_equals(response.status, 200, 'Default Response.status should be 200');
8 assert_equals(response.statusText, 'OK', 'Default Response.statusText should be \'OK\'');
9 assert_equals(response.headers.size, 0, 'Default Response should not have any header.');
11 response.status = 394;
12 response.statusText = 'Sesame Street';
13 assert_equals(response.status, 200, 'Response.status should be readonly');
14 assert_equals(response.statusText, 'OK', 'Response.statusText should be readonly');
15 }, 'Response default value test in ServiceWorkerGlobalScope');
18 var headers = new Headers;
19 headers.set('Content-Language', 'ja');
20 headers.set('Content-Type', 'text/html; charset=UTF-8');
21 headers.set('X-ServiceWorker-Test', 'response test field');
22 headers.set('Set-Cookie', 'response test set-cookie');
23 headers.set('Set-Cookie2', 'response test set-cookie2');
26 [new Response(new Blob(),
27 {status: 303, statusText: 'See Other', headers: headers}),
28 new Response(new Blob(),
31 statusText: 'See Other',
32 headers: {'Content-Language': 'ja',
33 'Content-Type': 'text/html; charset=UTF-8',
34 'X-ServiceWorker-Test': 'response test field',
35 'Set-Cookie': 'response test set-cookie',
36 'Set-Cookie2': 'response test set-cookie2'}
38 new Response(new Blob(),
41 statusText: 'See Other',
42 headers: [['Content-Language', 'ja'],
43 ['Content-Type', 'text/html; charset=UTF-8'],
44 ['X-ServiceWorker-Test', 'response test field'],
45 ['Set-Cookie', 'response test set-cookie'],
46 ['Set-Cookie2', 'response test set-cookie2']]
48 responses.forEach(function(response) {
49 assert_equals(response.status, 303, 'Response.status should match');
50 assert_equals(response.statusText, 'See Other', 'Response.statusText should match');
51 assert_true(response.headers instanceof Headers, 'Response.headers should be Headers');
52 assert_equals(response.headers.size, 3, 'Response.headers.size should match');
53 assert_equals(response.headers.get('Content-Language'), 'ja',
54 'Content-Language of Response.headers should match');
55 assert_equals(response.headers.get('Content-Type'), 'text/html; charset=UTF-8',
56 'Content-Type of Response.headers should match');
57 assert_equals(response.headers.get('X-ServiceWorker-Test'), 'response test field',
58 'X-ServiceWorker-Test of Response.headers should match');
59 response.headers.set('X-ServiceWorker-Test2', 'response test field2');
60 assert_equals(response.headers.size, 4, 'Response.headers.size should increase by 1.');
61 assert_equals(response.headers.get('X-ServiceWorker-Test2'), 'response test field2',
62 'Response.headers should be added');
63 response.headers.set('set-cookie', 'dummy');
64 response.headers.set('sEt-cookie', 'dummy');
65 response.headers.set('set-cookie2', 'dummy');
66 response.headers.set('set-cOokie2', 'dummy');
67 response.headers.append('set-cookie', 'dummy');
68 response.headers.append('sEt-cookie', 'dummy');
69 response.headers.append('set-cookie2', 'dummy');
70 response.headers.append('set-cOokie2', 'dummy');
71 assert_equals(response.headers.size, 4,
72 'Response.headers should not accept Set-Cookie nor Set-Cookie2');
73 response.headers.delete('X-ServiceWorker-Test');
74 assert_equals(response.headers.size, 3, 'Response.headers.size should decrease by 1.');
76 // Note: detailed behavioral tests for Headers are in another test,
77 // http/tests/serviceworker/headers.html.
78 }, 'Response constructor test in ServiceWorkerGlobalScope');
81 var response = new Response(new Blob(['dummy'], {type :'audio/wav'}));
82 assert_equals(response.headers.size, 1, 'Response.headers should have Content-Type');
83 assert_equals(response.headers.get('Content-Type'), 'audio/wav',
84 'Content-Type of Response.headers should be set');
86 response = new Response(new Blob(['dummy'], {type :'audio/wav'}),
87 {headers:{'Content-Type': 'text/html; charset=UTF-8'}});
88 assert_equals(response.headers.size, 1, 'Response.headers should have Content-Type');
89 assert_equals(response.headers.get('Content-Type'), 'text/html; charset=UTF-8',
90 'Content-Type of Response.headers should be overridden');
91 }, 'Response content type test in ServiceWorkerGlobalScope');
94 [0, 1, 100, 199, 600, 700].forEach(function(status) {
95 assert_throws({name:'RangeError'},
96 function() { new Response(new Blob(), {status: status}); },
97 'new Response with status = ' + status + ' should throw');
99 [200, 300, 400, 500, 599].forEach(function(status) {
100 var response = new Response(new Blob(), {status: status});
101 assert_equals(response.status, status, 'Response.status should match');
104 var invalidNames = ['', '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"',
105 '/', '[', ']', '?', '=', '{', '}', '\u3042', 'a(b'];
106 invalidNames.forEach(function(name) {
107 assert_throws({name:'TypeError'},
111 new Response(new Blob(), {headers: obj});
113 'new Response with headers with an invalid name (' + name +') should throw');
114 assert_throws({name:'TypeError'},
116 new Response(new Blob(), {headers: [[name, 'a']]});
118 'new Response with headers with an invalid name (' + name +') should throw');
120 var invalidValues = ['test \r data', 'test \n data'];
121 invalidValues.forEach(function(value) {
122 assert_throws({name:'TypeError'},
124 new Response(new Blob(),
125 {headers: {'X-ServiceWorker-Test': value}});
127 'new Response with headers with an invalid value should throw');
128 assert_throws({name:'TypeError'},
130 new Response(new Blob(),
131 {headers: [['X-ServiceWorker-Test', value]]});
133 'new Response with headers with an invalid value should throw');
135 }, 'Response throw error test in ServiceWorkerGlobalScope');