conditional-get.any.js (1431B)
1 // META: title=Request ETag 2 // META: global=window,worker 3 // META: script=/common/utils.js 4 5 promise_test(function() { 6 var cacheBuster = token(); // ensures first request is uncached 7 var url = "../resources/cache.py?v=" + cacheBuster; 8 var etag; 9 10 // make the first request 11 return fetch(url).then(function(response) { 12 // ensure we're getting the regular, uncached response 13 assert_equals(response.status, 200); 14 assert_equals(response.headers.get("X-HTTP-STATUS"), null) 15 16 return response.text(); // consuming the body, just to be safe 17 }).then(function(body) { 18 // make a second request 19 return fetch(url); 20 }).then(function(response) { 21 // while the server responds with 304 if our browser sent the correct 22 // If-None-Match request header, at the JavaScript level this surfaces 23 // as 200 24 assert_equals(response.status, 200); 25 assert_equals(response.headers.get("X-HTTP-STATUS"), "304") 26 27 etag = response.headers.get("ETag") 28 29 return response.text(); // consuming the body, just to be safe 30 }).then(function(body) { 31 // make a third request, explicitly setting If-None-Match request header 32 var headers = { "If-None-Match": etag } 33 return fetch(url, { headers: headers }) 34 }).then(function(response) { 35 // 304 now surfaces thanks to the explicit If-None-Match request header 36 assert_equals(response.status, 304); 37 }); 38 }, "Testing conditional GET with ETags");