connection-reuse-test.js (2670B)
1 // This script is loaded in HTTP and HTTPS contexts to validate 2 // PerformanceResourceTiming entries' attributes when reusing connections. 3 // 4 // Note: to ensure that we reuse the connection to fetch multiple resources, we 5 // use the same XMLHttpRequest object throughout an individual test. Although 6 // it doesn't seem to be specified, each browser tested by WPT will reuse the 7 // underlying TCP connection with this approach. Pre-establishing the XHR's 8 // connection helps us to test connection reuse also in browsers that may key 9 // their connections on the related request's credentials mode. 10 11 const connection_reuse_test = (path, follow_on_assertions, test_label) => { 12 const {on_200, on_304} = follow_on_assertions; 13 14 // Make the first request before calling 'attribute_test' so that only the 15 // second request's PerformanceResourceTiming entry will be interrogated. We 16 // don't check the first request's PerformanceResourceTiming entry because 17 // that's not what this test is trying to validate. 18 const client = new XMLHttpRequest(); 19 const identifier = Math.random(); 20 path = `${path}?tag=${identifier}`; 21 client.open("GET", path, false); 22 client.send(); 23 24 attribute_test( 25 async () => { 26 client.open("GET", path + "&same_resource=false", false); 27 client.send(); 28 29 // We expect to get a 200 Ok response because we've requested a different 30 // resource than previous requests. 31 if (client.status != 200) { 32 throw new Error(`Got something other than a 200 response. ` + 33 `client.status: ${client.status}`); 34 } 35 }, path, entry => { 36 invariants.assert_connection_reused(entry); 37 on_200(entry); 38 }, 39 `PerformanceResrouceTiming entries need to conform to the spec when a ` + 40 `distinct resource is fetched over a persistent connection ` + 41 `(${test_label})`); 42 43 attribute_test( 44 async () => { 45 client.open("GET", path, false); 46 client.setRequestHeader("If-None-Match", identifier); 47 client.send(); 48 49 // We expect to get a 304 Not Modified response because we've used a 50 // matching 'identifier' for the If-None-Match header. 51 if (client.status != 304) { 52 throw new Error(`Got something other than a 304 response. ` + 53 `client.status: ${client.status}, response: ` + 54 `'${client.responseText}'`); 55 } 56 }, path, entry => { 57 invariants.assert_connection_reused(entry); 58 on_304(entry); 59 }, 60 `PerformanceResrouceTiming entries need to conform to the spec when the ` + 61 `resource is cache-revalidated over a persistent connection ` + 62 `(${test_label})`); 63 }