tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

cache-storage-match.https.any.js (9077B)


      1 // META: title=CacheStorage.match
      2 // META: global=window,worker
      3 // META: script=./resources/test-helpers.js
      4 // META: timeout=long
      5 
      6 (function() {
      7  var next_index = 1;
      8 
      9  // Returns a transaction (request, response, and url) for a unique URL.
     10  function create_unique_transaction(test) {
     11    var uniquifier = String(next_index++);
     12    var url = 'http://example.com/' + uniquifier;
     13 
     14    return {
     15      request: new Request(url),
     16      response: new Response('hello'),
     17      url: url
     18    };
     19  }
     20 
     21  self.create_unique_transaction = create_unique_transaction;
     22 })();
     23 
     24 cache_test(function(cache) {
     25    var transaction = create_unique_transaction();
     26 
     27    return cache.put(transaction.request.clone(), transaction.response.clone())
     28      .then(function() {
     29          return self.caches.match(transaction.request);
     30        })
     31      .then(function(response) {
     32          assert_response_equals(response, transaction.response,
     33                                 'The response should not have changed.');
     34        });
     35 }, 'CacheStorageMatch with no cache name provided');
     36 
     37 cache_test(function(cache) {
     38    var transaction = create_unique_transaction();
     39 
     40    var test_cache_list = ['a', 'b', 'c'];
     41    return cache.put(transaction.request.clone(), transaction.response.clone())
     42      .then(function() {
     43          return Promise.all(test_cache_list.map(function(key) {
     44              return self.caches.open(key);
     45            }));
     46        })
     47      .then(function() {
     48          return self.caches.match(transaction.request);
     49        })
     50      .then(function(response) {
     51          assert_response_equals(response, transaction.response,
     52                                 'The response should not have changed.');
     53        });
     54 }, 'CacheStorageMatch from one of many caches');
     55 
     56 promise_test(function(test) {
     57    var transaction = create_unique_transaction();
     58 
     59    var test_cache_list = ['x', 'y', 'z'];
     60    return Promise.all(test_cache_list.map(function(key) {
     61        return self.caches.open(key);
     62      }))
     63      .then(function() { return self.caches.open('x'); })
     64      .then(function(cache) {
     65          return cache.put(transaction.request.clone(),
     66                           transaction.response.clone());
     67        })
     68      .then(function() {
     69          return self.caches.match(transaction.request, {cacheName: 'x'});
     70        })
     71      .then(function(response) {
     72          assert_response_equals(response, transaction.response,
     73                                 'The response should not have changed.');
     74        })
     75      .then(function() {
     76          return self.caches.match(transaction.request, {cacheName: 'y'});
     77        })
     78      .then(function(response) {
     79          assert_equals(response, undefined,
     80                        'Cache y should not have a response for the request.');
     81        });
     82 }, 'CacheStorageMatch from one of many caches by name');
     83 
     84 cache_test(function(cache) {
     85    var transaction = create_unique_transaction();
     86    return cache.put(transaction.url, transaction.response.clone())
     87      .then(function() {
     88          return self.caches.match(transaction.request);
     89        })
     90      .then(function(response) {
     91          assert_response_equals(response, transaction.response,
     92                                 'The response should not have changed.');
     93        });
     94 }, 'CacheStorageMatch a string request');
     95 
     96 cache_test(function(cache) {
     97    var transaction = create_unique_transaction();
     98    return cache.put(transaction.request.clone(), transaction.response.clone())
     99      .then(function() {
    100          return self.caches.match(new Request(transaction.request.url,
    101                                              {method: 'HEAD'}));
    102        })
    103      .then(function(response) {
    104          assert_equals(response, undefined,
    105                        'A HEAD request should not be matched');
    106        });
    107 }, 'CacheStorageMatch a HEAD request');
    108 
    109 promise_test(function(test) {
    110    var transaction = create_unique_transaction();
    111    return self.caches.match(transaction.request)
    112      .then(function(response) {
    113          assert_equals(response, undefined,
    114                        'The response should not be found.');
    115        });
    116 }, 'CacheStorageMatch with no cached entry');
    117 
    118 promise_test(function(test) {
    119    var transaction = create_unique_transaction();
    120    return self.caches.delete('foo')
    121      .then(function() {
    122          return self.caches.has('foo');
    123        })
    124      .then(function(has_foo) {
    125          assert_false(has_foo, "The cache should not exist.");
    126          return self.caches.match(transaction.request, {cacheName: 'foo'});
    127        })
    128      .then(function(response) {
    129          assert_equals(response, undefined,
    130                        'The match with bad cache name should resolve to ' +
    131                        'undefined.');
    132          return self.caches.has('foo');
    133        })
    134      .then(function(has_foo) {
    135          assert_false(has_foo, "The cache should still not exist.");
    136        });
    137 }, 'CacheStorageMatch with no caches available but name provided');
    138 
    139 cache_test(function(cache) {
    140    var transaction = create_unique_transaction();
    141 
    142    return self.caches.delete('')
    143      .then(function() {
    144          return self.caches.has('');
    145        })
    146      .then(function(has_cache) {
    147          assert_false(has_cache, "The cache should not exist.");
    148          return cache.put(transaction.request, transaction.response.clone());
    149        })
    150      .then(function() {
    151          return self.caches.match(transaction.request, {cacheName: ''});
    152        })
    153      .then(function(response) {
    154          assert_equals(response, undefined,
    155                        'The response should not be found.');
    156          return self.caches.open('');
    157        })
    158      .then(function(cache) {
    159          return cache.put(transaction.request, transaction.response);
    160        })
    161      .then(function() {
    162          return self.caches.match(transaction.request, {cacheName: ''});
    163        })
    164      .then(function(response) {
    165          assert_response_equals(response, transaction.response,
    166                                 'The response should be matched.');
    167          return self.caches.delete('');
    168        });
    169 }, 'CacheStorageMatch with empty cache name provided');
    170 
    171 cache_test(function(cache) {
    172    var request = new Request('http://example.com/?foo');
    173    var no_query_request = new Request('http://example.com/');
    174    var response = new Response('foo');
    175    return cache.put(request.clone(), response.clone())
    176      .then(function() {
    177          return self.caches.match(no_query_request.clone());
    178        })
    179      .then(function(result) {
    180          assert_equals(
    181            result, undefined,
    182            'CacheStorageMatch should resolve as undefined with a ' +
    183            'mismatched query.');
    184          return self.caches.match(no_query_request.clone(),
    185                                   {ignoreSearch: true});
    186        })
    187      .then(function(result) {
    188          assert_response_equals(
    189            result, response,
    190            'CacheStorageMatch with ignoreSearch should ignore the ' +
    191            'query of the request.');
    192        });
    193  }, 'CacheStorageMatch supports ignoreSearch');
    194 
    195 cache_test(function(cache) {
    196    var request = new Request('http://example.com/');
    197    var head_request = new Request('http://example.com/', {method: 'HEAD'});
    198    var response = new Response('foo');
    199    return cache.put(request.clone(), response.clone())
    200      .then(function() {
    201          return self.caches.match(head_request.clone());
    202        })
    203      .then(function(result) {
    204          assert_equals(
    205            result, undefined,
    206            'CacheStorageMatch should resolve as undefined with a ' +
    207            'mismatched method.');
    208          return self.caches.match(head_request.clone(),
    209                                   {ignoreMethod: true});
    210        })
    211      .then(function(result) {
    212          assert_response_equals(
    213            result, response,
    214            'CacheStorageMatch with ignoreMethod should ignore the ' +
    215            'method of request.');
    216        });
    217  }, 'Cache.match supports ignoreMethod');
    218 
    219 cache_test(function(cache) {
    220    var vary_request = new Request('http://example.com/c',
    221                                   {headers: {'Cookies': 'is-for-cookie'}});
    222    var vary_response = new Response('', {headers: {'Vary': 'Cookies'}});
    223    var mismatched_vary_request = new Request('http://example.com/c');
    224 
    225    return cache.put(vary_request.clone(), vary_response.clone())
    226      .then(function() {
    227          return self.caches.match(mismatched_vary_request.clone());
    228        })
    229      .then(function(result) {
    230          assert_equals(
    231            result, undefined,
    232            'CacheStorageMatch should resolve as undefined with a ' +
    233            ' mismatched vary.');
    234          return self.caches.match(mismatched_vary_request.clone(),
    235                                   {ignoreVary: true});
    236        })
    237      .then(function(result) {
    238          assert_response_equals(
    239            result, vary_response,
    240            'CacheStorageMatch with ignoreVary should ignore the ' +
    241            'vary of request.');
    242        });
    243  }, 'CacheStorageMatch supports ignoreVary');
    244 
    245 done();