test_cache_put.js (2108B)
1 /* global context testDone:true */ 2 3 var url = "test_cache.js"; 4 var cache; 5 var fetchResponse; 6 Promise.all([fetch(url), caches.open("putter" + context)]) 7 .then(function (results) { 8 fetchResponse = results[0]; 9 cache = results[1]; 10 return cache.put(url, fetchResponse.clone()); 11 }) 12 .then(function (result) { 13 is(undefined, result, "Successful put() should resolve undefined"); 14 return cache.match(url); 15 }) 16 .then(function (response) { 17 ok(response, "match() should find resppnse that was previously put()"); 18 ok( 19 response.url.endsWith(url), 20 "matched response should match original url" 21 ); 22 return Promise.all([fetchResponse.text(), response.text()]); 23 }) 24 .then(function (results) { 25 // suppress large assert spam unless it's relevent 26 if (results[0] !== results[1]) { 27 is(results[0], results[1], "stored response body should match original"); 28 } 29 30 // Now, try to overwrite the request with a different response object. 31 return cache.put(url, new Response("overwritten")); 32 }) 33 .then(function () { 34 return cache.matchAll(url); 35 }) 36 .then(function (result) { 37 is(result.length, 1, "Only one entry should exist"); 38 return result[0].text(); 39 }) 40 .then(function (body) { 41 is( 42 body, 43 "overwritten", 44 "The cache entry should be successfully overwritten" 45 ); 46 47 // Now, try to write a URL with a fragment 48 return cache.put(url + "#fragment", new Response("more overwritten")); 49 }) 50 .then(function () { 51 return cache.matchAll(url + "#differentFragment"); 52 }) 53 .then(function (result) { 54 is(result.length, 1, "Only one entry should exist"); 55 return result[0].text(); 56 }) 57 .then(function (body) { 58 is( 59 body, 60 "more overwritten", 61 "The cache entry should be successfully overwritten" 62 ); 63 64 // TODO: Verify that trying to store a response with an error raises a TypeError 65 // when bug 1147178 is fixed. 66 67 return caches.delete("putter" + context); 68 }) 69 .then(function (deleted) { 70 ok(deleted, "The cache should be deleted successfully"); 71 testDone(); 72 });