OLD | NEW |
1 (function() { | 1 (function() { |
2 var next_cache_index = 1; | 2 var next_cache_index = 1; |
3 | 3 |
4 // Returns a promise that resolves to a newly created Cache object. The | 4 // Returns a promise that resolves to a newly created Cache object. The |
5 // returned Cache will be destroyed when |test| completes. | 5 // returned Cache will be destroyed when |test| completes. |
6 function create_temporary_cache(test) { | 6 function create_temporary_cache(test) { |
7 var uniquifier = String(++next_cache_index); | 7 var uniquifier = String(++next_cache_index); |
8 var cache_name = self.location.pathname + '/' + uniquifier; | 8 var cache_name = self.location.pathname + '/' + uniquifier; |
9 | 9 |
10 test.add_cleanup(function() { | 10 test.add_cleanup(function() { |
(...skipping 17 matching lines...) Expand all Loading... |
28 // E.g.: | 28 // E.g.: |
29 // cache_test(function(cache) { | 29 // cache_test(function(cache) { |
30 // // Do something with |cache|, which is a Cache object. | 30 // // Do something with |cache|, which is a Cache object. |
31 // }, "Some Cache test"); | 31 // }, "Some Cache test"); |
32 function cache_test(test_function, description) { | 32 function cache_test(test_function, description) { |
33 promise_test(function(test) { | 33 promise_test(function(test) { |
34 return create_temporary_cache(test) | 34 return create_temporary_cache(test) |
35 .then(test_function); | 35 .then(test_function); |
36 }, description); | 36 }, description); |
37 } | 37 } |
| 38 |
| 39 // A set of Request/Response pairs to be used with prepopulated_cache_test(). |
| 40 var simple_entries = [ |
| 41 { |
| 42 name: 'a', |
| 43 request: new Request('http://example.com/a'), |
| 44 response: new Response('') |
| 45 }, |
| 46 |
| 47 { |
| 48 name: 'b', |
| 49 request: new Request('http://example.com/b'), |
| 50 response: new Response('') |
| 51 }, |
| 52 |
| 53 { |
| 54 name: 'a_with_query', |
| 55 request: new Request('http://example.com/a?q=r'), |
| 56 response: new Response('') |
| 57 }, |
| 58 |
| 59 { |
| 60 name: 'A', |
| 61 request: new Request('http://example.com/A'), |
| 62 response: new Response('') |
| 63 }, |
| 64 |
| 65 { |
| 66 name: 'a_https', |
| 67 request: new Request('https://example.com/a'), |
| 68 response: new Response('') |
| 69 }, |
| 70 |
| 71 { |
| 72 name: 'a_org', |
| 73 request: new Request('http://example.org/a'), |
| 74 response: new Response('') |
| 75 }, |
| 76 |
| 77 { |
| 78 name: 'cat', |
| 79 request: new Request('http://example.com/cat'), |
| 80 response: new Response('') |
| 81 }, |
| 82 |
| 83 { |
| 84 name: 'catmandu', |
| 85 request: new Request('http://example.com/catmandu'), |
| 86 response: new Response('') |
| 87 }, |
| 88 |
| 89 { |
| 90 name: 'cat_num_lives', |
| 91 request: new Request('http://example.com/cat?lives=9'), |
| 92 response: new Response('') |
| 93 }, |
| 94 |
| 95 { |
| 96 name: 'cat_in_the_hat', |
| 97 request: new Request('http://example.com/cat/in/the/hat'), |
| 98 response: new Response('') |
| 99 }, |
| 100 |
| 101 { |
| 102 name: 'secret_cat', |
| 103 request: new Request('http://tom:jerry@example.com/cat'), |
| 104 response: new Response('') |
| 105 }, |
| 106 |
| 107 { |
| 108 name: 'top_secret_cat', |
| 109 request: new Request('http://tom:j3rry@example.com/cat'), |
| 110 response: new Response('') |
| 111 }, |
| 112 { |
| 113 name: 'non_2xx_response', |
| 114 request: new Request('http://example.com/non2xx'), |
| 115 response: new Response('', {status: 404, statusText: 'nope'}) |
| 116 }, |
| 117 |
| 118 { |
| 119 name: 'error_response', |
| 120 request: new Request('http://example.com/error'), |
| 121 response: Response.error() |
| 122 }, |
| 123 ]; |
| 124 |
| 125 // A set of Request/Response pairs to be used with prepopulated_cache_test(). |
| 126 // These contain a mix of test cases that use Vary headers. |
| 127 var vary_entries = [ |
| 128 { |
| 129 name: 'vary_cookie_is_cookie', |
| 130 request: new Request('http://example.com/c', |
| 131 {headers: {'Cookies': 'is-for-cookie'}}), |
| 132 response: new Response('', |
| 133 {headers: {'Vary': 'Cookies'}}) |
| 134 }, |
| 135 |
| 136 { |
| 137 name: 'vary_cookie_is_good', |
| 138 request: new Request('http://example.com/c', |
| 139 {headers: {'Cookies': 'is-good-enough-for-me'}}), |
| 140 response: new Response('', |
| 141 {headers: {'Vary': 'Cookies'}}) |
| 142 }, |
| 143 |
| 144 { |
| 145 name: 'vary_cookie_absent', |
| 146 request: new Request('http://example.com/c'), |
| 147 response: new Response('', |
| 148 {headers: {'Vary': 'Cookies'}}) |
| 149 }, |
| 150 |
| 151 { |
| 152 name: 'vary_wildcard', |
| 153 request: new Request('http://example.com/c', |
| 154 {headers: {'Cookies': 'x', 'X-Key': '1'}}), |
| 155 response: new Response('', |
| 156 {headers: {'Vary': '*'}}) |
| 157 } |
| 158 ]; |
| 159 |
| 160 // Run |test_function| with a Cache object and a map of entries. Prior to the |
| 161 // call, the Cache is populated by cache entries from |entries|. The latter is |
| 162 // expected to be an Object mapping arbitrary keys to objects of the form |
| 163 // {request: <Request object>, response: <Response object>}. There's no |
| 164 // guarantee on the order in which entries will be added to the cache. |
| 165 // |
| 166 // |test_function| should return a Promise that can be used with promise_test. |
| 167 function prepopulated_cache_test(entries, test_function, description) { |
| 168 cache_test(function(cache) { |
| 169 var p = Promise.resolve(); |
| 170 var hash = {}; |
| 171 return Promise.all(entries.map(function(entry) { |
| 172 hash[entry.name] = entry; |
| 173 return cache.put(entry.request.clone(), |
| 174 entry.response.clone()) |
| 175 .catch(function(e) { |
| 176 assert_unreached( |
| 177 'Test setup failed for entry ' + entry.name + ': ' + e); |
| 178 }); |
| 179 })) |
| 180 .then(function() { |
| 181 assert_equals(Object.keys(hash).length, entries.length); |
| 182 }) |
| 183 .then(function() { |
| 184 return test_function(cache, hash); |
| 185 }); |
| 186 }, description); |
| 187 } |
OLD | NEW |