summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/fetch-request-xhr-iframe.https.html
blob: 26c6b7344677a9a6792db342b108d2bbc68d3ec7 (plain)
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
<script src="../resources/get-host-info.sub.js"></script>
<script src="test-helpers.sub.js?pipe=sub"></script>
<script>
var port;
var host_info = get_host_info();

function assert_equals(a, b) {
  port.postMessage({results: 'equals', got: a, expected: b});
}

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 create_file_system_file(file_name, data) {
  return new Promise(function(resolve, reject) {
      webkitRequestFileSystem(TEMPORARY, 1024, function(fs) {
          fs.root.getFile(
            file_name, {create: true, exclusive: true},
            function(fileEntry) {
              fileEntry.createWriter(function(fileWriter) {
                  fileWriter.onwriteend = function(e) {
                    fileEntry.file(function(file) { resolve(file); });
                  };
                  var blob = new Blob([data], {type: 'text/plain'});
                  fileWriter.write(blob);
                });
            }, function(e) { reject(e); });
        }, function(e) { reject(e); });
    });
}

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 + '/dummy?test', true);
      xhr.send(data);
    });
}

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);
      });
}

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');
      });
}

window.addEventListener('message', function(evt) {
    port = evt.ports[0];
    string_test()
      .then(blob_test)
      .then(custom_method_test)
      .then(options_method_test)
      .then(form_data_test)
      .then(mode_credentials_test)
      .then(data_url_test)
      .then(function() { port.postMessage({results: 'finish'}); })
      .catch(function(e) { port.postMessage({results: 'failure:' + e}); });
  });
</script>