1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
<script src="/common/get-host-info.sub.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/test-helpers.sub.js?pipe=sub"></script>
<script>
var host_info = get_host_info();
function get_boundary(headers) {
var reg = new RegExp('multipart\/form-data; boundary=(.*)');
for (var i = 0; i < headers.length; ++i) {
if (headers[i][0] != 'content-type') {
continue;
}
var regResult = reg.exec(headers[i][1]);
if (!regResult) {
continue;
}
return regResult[1];
}
return '';
}
function xhr_send(url_base, method, data, with_credentials) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(JSON.parse(xhr.response));
};
xhr.onerror = function() {
reject('XHR should succeed.');
};
xhr.responseType = 'text';
if (with_credentials) {
xhr.withCredentials = true;
}
xhr.open(method, url_base + '/sample?test', true);
xhr.send(data);
});
}
function get_sorted_header_name_list(headers) {
var header_names = [];
var idx, name;
for (idx = 0; idx < headers.length; ++idx) {
name = headers[idx][0];
// The `Accept-Language` header is optional; its presence should not
// influence test results.
//
// > 4. If request’s header list does not contain `Accept-Language`, user
// > agents should append `Accept-Language`/an appropriate value to
// > request's header list.
//
// https://fetch.spec.whatwg.org/#fetching
if (name === 'accept-language') {
continue;
}
header_names.push(name);
}
header_names.sort();
return header_names;
}
function get_header_test() {
return xhr_send(host_info['HTTPS_ORIGIN'], 'GET', '', false)
.then(function(response) {
assert_array_equals(
get_sorted_header_name_list(response.headers),
["accept"],
'event.request has the expected headers for same-origin GET.');
});
}
function post_header_test() {
return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', '', false)
.then(function(response) {
assert_array_equals(
get_sorted_header_name_list(response.headers),
["accept", "content-type"],
'event.request has the expected headers for same-origin POST.');
});
}
function cross_origin_get_header_test() {
return xhr_send(host_info['HTTPS_REMOTE_ORIGIN'], 'GET', '', false)
.then(function(response) {
assert_array_equals(
get_sorted_header_name_list(response.headers),
["accept"],
'event.request has the expected headers for cross-origin GET.');
});
}
function cross_origin_post_header_test() {
return xhr_send(host_info['HTTPS_REMOTE_ORIGIN'], 'POST', '', false)
.then(function(response) {
assert_array_equals(
get_sorted_header_name_list(response.headers),
["accept", "content-type"],
'event.request has the expected headers for cross-origin POST.');
});
}
function string_test() {
return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', 'test string', false)
.then(function(response) {
assert_equals(response.method, 'POST');
assert_equals(response.body, 'test string');
});
}
function blob_test() {
return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', new Blob(['test blob']),
false)
.then(function(response) {
assert_equals(response.method, 'POST');
assert_equals(response.body, 'test blob');
});
}
function custom_method_test() {
return xhr_send(host_info['HTTPS_ORIGIN'], 'XXX', 'test string xxx', false)
.then(function(response) {
assert_equals(response.method, 'XXX');
assert_equals(response.body, 'test string xxx');
});
}
function options_method_test() {
return xhr_send(host_info['HTTPS_ORIGIN'], 'OPTIONS', 'test string xxx', false)
.then(function(response) {
assert_equals(response.method, 'OPTIONS');
assert_equals(response.body, 'test string xxx');
});
}
function form_data_test() {
var formData = new FormData();
formData.append('sample string', '1234567890');
formData.append('sample blob', new Blob(['blob content']));
formData.append('sample file', new File(['file content'], 'file.dat'));
return xhr_send(host_info['HTTPS_ORIGIN'], 'POST', formData, false)
.then(function(response) {
assert_equals(response.method, 'POST');
var boundary = get_boundary(response.headers);
var expected_body =
'--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="sample string"\r\n' +
'\r\n' +
'1234567890\r\n' +
'--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="sample blob"; ' +
'filename="blob"\r\n' +
'Content-Type: application/octet-stream\r\n' +
'\r\n' +
'blob content\r\n' +
'--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="sample file"; ' +
'filename="file.dat"\r\n' +
'Content-Type: application/octet-stream\r\n' +
'\r\n' +
'file content\r\n' +
'--' + boundary + '--\r\n';
assert_equals(response.body, expected_body, "form data response content is as expected");
});
}
function mode_credentials_test() {
return xhr_send(host_info['HTTPS_ORIGIN'], 'GET', '', false)
.then(function(response){
assert_equals(response.mode, 'cors');
assert_equals(response.credentials, 'same-origin');
return xhr_send(host_info['HTTPS_ORIGIN'], 'GET', '', true);
})
.then(function(response){
assert_equals(response.mode, 'cors');
assert_equals(response.credentials, 'include');
return xhr_send(host_info['HTTPS_REMOTE_ORIGIN'], 'GET', '', false);
})
.then(function(response){
assert_equals(response.mode, 'cors');
assert_equals(response.credentials, 'same-origin');
return xhr_send(host_info['HTTPS_REMOTE_ORIGIN'], 'GET', '', true);
})
.then(function(response){
assert_equals(response.mode, 'cors');
assert_equals(response.credentials, 'include');
});
}
function data_url_test() {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(xhr.response);
};
xhr.onerror = function() {
reject('XHR should succeed.');
};
xhr.responseType = 'text';
xhr.open('GET', 'data:text/html,Foobar', true);
xhr.send();
})
.then(function(data) {
assert_equals(data, 'Foobar');
});
}
</script>
|