Upstream version 10.39.225.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, 0, 'Default Response should not have any header.');
10
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');
16
17 test(function() {
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');
24
25     var responses =
26         [new Response(new Blob(),
27                       {status: 303, statusText: 'See Other', headers: headers}),
28          new Response(new Blob(),
29                       {
30                           status: 303,
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'}
37                       }),
38          new Response(new Blob(),
39                       {
40                           status: 303,
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']]
47                       })];
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.');
75     });
76     // Note: detailed behavioral tests for Headers are in another test,
77     // http/tests/serviceworker/headers.html.
78 }, 'Response constructor test in ServiceWorkerGlobalScope');
79
80 test(function() {
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');
85
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');
92
93 test(function() {
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');
98     });
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');
102     });
103
104     var invalidNames = ['', '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"',
105                         '/', '[', ']', '?', '=', '{', '}', '\u3042', 'a(b'];
106     invalidNames.forEach(function(name) {
107         assert_throws({name:'TypeError'},
108                       function() {
109                           var obj = {};
110                           obj[name] = 'a';
111                           new Response(new Blob(), {headers: obj});
112                       },
113                       'new Response with headers with an invalid name (' + name +') should throw');
114         assert_throws({name:'TypeError'},
115                       function() {
116                           new Response(new Blob(), {headers: [[name, 'a']]});
117                       },
118                       'new Response with headers with an invalid name (' + name +') should throw');
119     });
120     var invalidValues = ['test \r data', 'test \n data'];
121     invalidValues.forEach(function(value) {
122         assert_throws({name:'TypeError'},
123                       function() {
124                           new Response(new Blob(),
125                                        {headers: {'X-ServiceWorker-Test': value}});
126                       },
127                       'new Response with headers with an invalid value should throw');
128         assert_throws({name:'TypeError'},
129                       function() {
130                           new Response(new Blob(),
131                                        {headers: [['X-ServiceWorker-Test', value]]});
132                       },
133                       'new Response with headers with an invalid value should throw');
134     });
135 }, 'Response throw error test in ServiceWorkerGlobalScope');