summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/fetch-cors-xhr-iframe.html
blob: 48f618397c5cd8cf3a5d561c8e371818367cb890 (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
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
209
210
<script src="../resources/get-host-info.sub.js"></script>
<script src="test-helpers.sub.js?pipe=sub"></script>
<script>
var path = base_path() + 'fetch-access-control.py';
var host_info = get_host_info();
var SUCCESS = 'SUCCESS';
var FAIL = 'FAIL';

function create_test_case_promise(url, with_credentials) {
  return new Promise(function(resolve) {
      var xhr = new XMLHttpRequest();
      xhr.onload = function() {
        if (xhr.status == 200) {
          resolve(SUCCESS);
        } else {
          resolve("STATUS" + xhr.status);
        }
      }
      xhr.onerror = function() {
        resolve(FAIL);
      }
      xhr.responseType = 'text';
      xhr.withCredentials = with_credentials;
      xhr.open('GET', url, true);
      xhr.send();
    });
}


function create_test_promise(url, with_credentials, expected_result) {
  return new Promise(function(resolve, reject) {
      create_test_case_promise(url, with_credentials)
        .then(function(result) {
          if (result == expected_result) {
            resolve();
          } else {
            reject('Result of url:' + url + ' ' +
                   ' with_credentials: ' + with_credentials + ' must be ' +
                   expected_result + ' but ' + result);
          }
        })
    });
}

function create_serial_promise(test_cases) {
  var promise = Promise.resolve();
  test_cases.forEach(function(test_case) {
      promise = promise.then(function() {
          return create_test_promise(test_case[0], test_case[1], test_case[2]);
        });
    });
    return promise;
}

window.addEventListener('message', function(evt) {
    var port = evt.ports[0];
    var url = host_info['HTTPS_ORIGIN'] + path;
    var remote_url = host_info['HTTPS_REMOTE_ORIGIN'] + path;
    // If the 4th value of the item of TEST_CASES is true, the test case outputs
    // warning messages. So such tests must be executed in serial to match the
    // expected output text.
    var TEST_CASES = [
      // Reject tests
      [url + '?reject', false, FAIL],
      [url + '?reject', true, FAIL],
      [remote_url + '?reject', false, FAIL],
      [remote_url + '?reject', true, FAIL],
      // Event handler exception tests
      [url + '?throw', false, FAIL],
      [url + '?throw', true, FAIL],
      [remote_url + '?throw', false, FAIL],
      [remote_url + '?throw', true, FAIL],
      // Reject(resolve-null) tests
      [url + '?resolve-null', false, FAIL],
      [url + '?resolve-null', true, FAIL],
      [remote_url + '?resolve-null', false, FAIL],
      [remote_url + '?resolve-null', true, FAIL],
      // Fallback tests
      [url + '?ignore', false, SUCCESS],
      [url + '?ignore', true, SUCCESS],
      [remote_url + '?ignore', false, FAIL, true],  // Executed in serial.
      [remote_url + '?ignore', true, FAIL, true],  // Executed in serial.
      [
        remote_url + '?ACAOrigin=' + host_info['HTTPS_ORIGIN'] + '&ignore',
        false, SUCCESS
      ],
      [
        remote_url + '?ACAOrigin=' + host_info['HTTPS_ORIGIN'] + '&ignore',
        true, FAIL, true  // Executed in serial.
      ],
      [
        remote_url + '?ACAOrigin=' + host_info['HTTPS_ORIGIN'] +
        '&ACACredentials=true&ignore',
        true, SUCCESS
      ],
      // Credential test (fallback)
      [url + '?Auth&ignore', false, SUCCESS],
      [url + '?Auth&ignore', true, SUCCESS],
      [remote_url + '?Auth&ignore', false, FAIL, true],  // Executed in serial.
      [remote_url + '?Auth&ignore', true, FAIL, true],  // Executed in serial.
      [
        remote_url + '?Auth&ACAOrigin=' + host_info['HTTPS_ORIGIN'] + '&ignore',
        false, 'STATUS401'
      ],
      [
        remote_url + '?Auth&ACAOrigin=' + host_info['HTTPS_ORIGIN'] + '&ignore',
        true, FAIL, true  // Executed in serial.
      ],
      [
        remote_url + '?Auth&ACAOrigin=' + host_info['HTTPS_ORIGIN'] +
        '&ACACredentials=true&ignore',
        true, SUCCESS
      ],
      // Basic response
      [
        url + '?mode=same-origin&url=' + encodeURIComponent(url),
        false, SUCCESS
      ],
      [
        url + '?mode=same-origin&url=' + encodeURIComponent(url),
        false, SUCCESS
      ],
      [
        remote_url + '?mode=same-origin&url=' + encodeURIComponent(url),
        false, SUCCESS
      ],
      [
        remote_url + '?mode=same-origin&url=' + encodeURIComponent(url),
        false, SUCCESS
      ],
      // Opaque response
      [
        url + '?mode=no-cors&url=' + encodeURIComponent(remote_url),
        false, FAIL
      ],
      [
        url + '?mode=no-cors&url=' + encodeURIComponent(remote_url),
        false, FAIL
      ],
      [
        remote_url + '?mode=no-cors&url=' + encodeURIComponent(remote_url),
        false, FAIL
      ],
      [
        remote_url + '?mode=no-cors&url=' + encodeURIComponent(remote_url),
        false, FAIL
      ],
      // CORS response
      [
        url + '?mode=cors&url=' +
        encodeURIComponent(remote_url + '?ACAOrigin=' +
                           host_info['HTTPS_ORIGIN']),
        false, SUCCESS
      ],
      [
        url + '?mode=cors&url=' +
        encodeURIComponent(remote_url + '?ACAOrigin=' +
                           host_info['HTTPS_ORIGIN']),
        true, FAIL
      ],
      [
        url + '?mode=cors&url=' +
        encodeURIComponent(remote_url + '?ACAOrigin=' +
                           host_info['HTTPS_ORIGIN'] +
                           '&ACACredentials=true'),
        true, SUCCESS
      ],
      [
        remote_url + '?mode=cors&url=' +
        encodeURIComponent(remote_url + '?ACAOrigin=' +
                           host_info['HTTPS_ORIGIN']),
        false, SUCCESS
      ],
      [
        remote_url +
        '?mode=cors&url=' +
        encodeURIComponent(remote_url + '?ACAOrigin=' +
                           host_info['HTTPS_ORIGIN']),
        true, FAIL
      ],
      [
        remote_url +
        '?mode=cors&url=' +
        encodeURIComponent(remote_url + '?ACAOrigin=' +
                           host_info['HTTPS_ORIGIN'] +
                           '&ACACredentials=true'),
        true, SUCCESS
      ]
    ];
    var promises = [];
    var serial_tests = [];
    for (var i = 0; i < TEST_CASES.length ; ++i) {
      if (!TEST_CASES[i][3]) {
        promises.push(create_test_promise(TEST_CASES[i][0],
                                          TEST_CASES[i][1],
                                          TEST_CASES[i][2]));
      } else {
        serial_tests.push(TEST_CASES[i]);
      }
    }
    promises.push(create_serial_promise(serial_tests));
    Promise.all(promises)
      .then(function() {
          port.postMessage({results: 'finish'});
        })
      .catch(function(e) {
          port.postMessage({results: 'failure:' + e});
        });
  }, false);
</script>