Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / resources / response-worker.js
1 importScripts('worker-test-harness.js');
2
3 test(function() {
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, 1, 'Default Response should have one header.');
10     assert_equals(response.headers.get('content-type'), '', 'Default Response should have one header which name is \'content-type\' and value is an empty string.');
11
12     response.status = 394;
13     response.statusText = 'Sesame Street';
14     assert_equals(response.status, 200, 'Response.status should be readonly');
15     assert_equals(response.statusText, 'OK', 'Response.statusText should be readonly');
16 }, 'Response default value test in ServiceWorkerGlobalScope');
17
18 test(function() {
19     var headers = new Headers;
20     headers.set('Content-Language', 'ja');
21     headers.set('Content-Type', 'text/html; charset=UTF-8');
22     headers.set('X-ServiceWorker-Test', 'response test field');
23     headers.set('Set-Cookie', 'response test set-cookie');
24     headers.set('Set-Cookie2', 'response test set-cookie2');
25
26     var responses =
27         [new Response(new Blob(),
28                       {status: 303, statusText: 'See Other', headers: headers}),
29          new Response(new Blob(),
30                       {
31                           status: 303,
32                           statusText: 'See Other',
33                           headers: {'Content-Language': 'ja',
34                                     'Content-Type': 'text/html; charset=UTF-8',
35                                     'X-ServiceWorker-Test': 'response test field',
36                                     'Set-Cookie': 'response test set-cookie',
37                                     'Set-Cookie2': 'response test set-cookie2'}
38                       }),
39          new Response(new Blob(),
40                       {
41                           status: 303,
42                           statusText: 'See Other',
43                           headers: [['Content-Language', 'ja'],
44                                     ['Content-Type', 'text/html; charset=UTF-8'],
45                                     ['X-ServiceWorker-Test', 'response test field'],
46                                     ['Set-Cookie', 'response test set-cookie'],
47                                     ['Set-Cookie2', 'response test set-cookie2']]
48                       })];
49     responses.forEach(function(response) {
50         assert_equals(response.status, 303, 'Response.status should match');
51         assert_equals(response.statusText, 'See Other', 'Response.statusText should match');
52         assert_true(response.headers instanceof Headers, 'Response.headers should be Headers');
53         assert_equals(response.headers.size, 3, 'Response.headers.size should match');
54         assert_equals(response.headers.get('Content-Language'), 'ja',
55                       'Content-Language of Response.headers should match');
56         assert_equals(response.headers.get('Content-Type'), 'text/html; charset=UTF-8',
57                       'Content-Type of Response.headers should match');
58         assert_equals(response.headers.get('X-ServiceWorker-Test'), 'response test field',
59                       'X-ServiceWorker-Test of Response.headers should match');
60         response.headers.set('X-ServiceWorker-Test2', 'response test field2');
61         assert_equals(response.headers.size, 4, 'Response.headers.size should increase by 1.');
62         assert_equals(response.headers.get('X-ServiceWorker-Test2'), 'response test field2',
63                       'Response.headers should be added');
64         response.headers.set('set-cookie', 'dummy');
65         response.headers.set('sEt-cookie', 'dummy');
66         response.headers.set('set-cookie2', 'dummy');
67         response.headers.set('set-cOokie2', 'dummy');
68         response.headers.append('set-cookie', 'dummy');
69         response.headers.append('sEt-cookie', 'dummy');
70         response.headers.append('set-cookie2', 'dummy');
71         response.headers.append('set-cOokie2', 'dummy');
72         assert_equals(response.headers.size, 4,
73                       'Response.headers should not accept Set-Cookie nor Set-Cookie2');
74         response.headers.delete('X-ServiceWorker-Test');
75         assert_equals(response.headers.size, 3, 'Response.headers.size should decrease by 1.');
76     });
77     // Note: detailed behavioral tests for Headers are in another test,
78     // http/tests/serviceworker/headers.html.
79 }, 'Response constructor test in ServiceWorkerGlobalScope');
80
81 test(function() {
82     var response = new Response(new Blob(['dummy'], {type :'audio/wav'}));
83     assert_equals(response.headers.size, 1, 'Response.headers should have Content-Type');
84     assert_equals(response.headers.get('Content-Type'), 'audio/wav',
85                   'Content-Type of Response.headers should be set');
86
87     response = new Response(new Blob(['dummy'], {type :'audio/wav'}),
88                             {headers:{'Content-Type': 'text/html; charset=UTF-8'}});
89     assert_equals(response.headers.size, 1, 'Response.headers should have Content-Type');
90     assert_equals(response.headers.get('Content-Type'), 'text/html; charset=UTF-8',
91                   'Content-Type of Response.headers should be overridden');
92 }, 'Response content type test in ServiceWorkerGlobalScope');
93
94 test(function() {
95     [0, 1, 100, 199, 600, 700].forEach(function(status) {
96         assert_throws({name:'RangeError'},
97                       function() { new Response(new Blob(), {status: status}); },
98                       'new Response with status = ' + status + ' should throw');
99     });
100     [200, 300, 400, 500, 599].forEach(function(status) {
101         var response = new Response(new Blob(), {status: status});
102         assert_equals(response.status, status, 'Response.status should match');
103     });
104
105     var invalidNames = ['', '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"',
106                         '/', '[', ']', '?', '=', '{', '}', '\u3042', 'a(b'];
107     invalidNames.forEach(function(name) {
108         assert_throws({name:'TypeError'},
109                       function() {
110                           var obj = {};
111                           obj[name] = 'a';
112                           new Response(new Blob(), {headers: obj});
113                       },
114                       'new Response with headers with an invalid name (' + name +') should throw');
115         assert_throws({name:'TypeError'},
116                       function() {
117                           new Response(new Blob(), {headers: [[name, 'a']]});
118                       },
119                       'new Response with headers with an invalid name (' + name +') should throw');
120     });
121     var invalidValues = ['test \r data', 'test \n data'];
122     invalidValues.forEach(function(value) {
123         assert_throws({name:'TypeError'},
124                       function() {
125                           new Response(new Blob(),
126                                        {headers: {'X-ServiceWorker-Test': value}});
127                       },
128                       'new Response with headers with an invalid value should throw');
129         assert_throws({name:'TypeError'},
130                       function() {
131                           new Response(new Blob(),
132                                        {headers: [['X-ServiceWorker-Test', value]]});
133                       },
134                       'new Response with headers with an invalid value should throw');
135     });
136 }, 'Response throw error test in ServiceWorkerGlobalScope');