OLD | NEW |
(Empty) | |
| 1 function ReferrerPolicyTestCase(scenario, testDescription, sanityChecker) { |
| 2 // Pass and skip rest of the test if browser does not support fetch. |
| 3 if (scenario.subresource == "fetch-request" && !window.fetch) { |
| 4 // TODO(kristijanburnik): This should be refactored. |
| 5 return { |
| 6 start: function() { |
| 7 test(function() { assert_true(true); }, |
| 8 "[ReferrerPolicyTestCase] Skipping test: Fetch is not supported."); |
| 9 } |
| 10 }; |
| 11 } |
| 12 |
| 13 // This check is A NOOP in release. |
| 14 sanityChecker.checkScenario(scenario); |
| 15 |
| 16 var subresourceInvoker = { |
| 17 "a-tag": queryLink, |
| 18 "area-tag": queryAreaLink, |
| 19 "fetch-request": queryFetch, |
| 20 "iframe-tag": queryIframe, |
| 21 "img-tag": queryImage, |
| 22 "script-tag": queryScript, |
| 23 "worker-request": queryWorker, |
| 24 "xhr-request": queryXhr |
| 25 }; |
| 26 |
| 27 var referrerUrlResolver = { |
| 28 "omitted": function() { |
| 29 return undefined; |
| 30 }, |
| 31 "origin": function() { |
| 32 return document.origin + "/"; |
| 33 }, |
| 34 "stripped-referrer": function() { |
| 35 return stripUrlForUseAsReferrer(location.toString()); |
| 36 } |
| 37 }; |
| 38 |
| 39 var t = { |
| 40 _scenario: scenario, |
| 41 _testDescription: testDescription, |
| 42 _subresourceUrl: null, |
| 43 _expectedReferrerUrl: null, |
| 44 _constructSubresourceUrl: function() { |
| 45 // TODO(kristijanburnik): We should assert that these two domains are |
| 46 // different. E.g. If someone runs the tets over www, this would fail. |
| 47 var domainForOrigin = { |
| 48 "cross-origin":"{{domains[www1]}}", |
| 49 "same-origin": location.hostname |
| 50 }; |
| 51 |
| 52 // Values obtained and replaced by the wptserve pipeline: |
| 53 // http://wptserve.readthedocs.org/en/latest/pipes.html#built-in-pipes |
| 54 var portForProtocol = { |
| 55 "http": parseInt("{{ports[http][0]}}"), |
| 56 "https": parseInt("{{ports[https][0]}}") |
| 57 } |
| 58 |
| 59 var targetPort = portForProtocol[t._scenario.target_protocol]; |
| 60 |
| 61 t._subresourceUrl = t._scenario.target_protocol + "://" + |
| 62 domainForOrigin[t._scenario.origin] + |
| 63 normalizePort(targetPort) + |
| 64 t._scenario["subresource_path"] + |
| 65 "?redirection=" + t._scenario["redirection"] + |
| 66 "&cache_destroyer=" + (new Date()).getTime(); |
| 67 }, |
| 68 |
| 69 _constructExpectedReferrerUrl: function() { |
| 70 t._expectedReferrerUrl = referrerUrlResolver[t._scenario.referrer_url](); |
| 71 }, |
| 72 |
| 73 _invokeSubresource: function(callback, test) { |
| 74 var invoker = subresourceInvoker[t._scenario.subresource]; |
| 75 |
| 76 // Depending on the delivery method, extend the subresource element with |
| 77 // these attributes. |
| 78 var elementAttributesForDeliveryMethod = { |
| 79 "attr-referrer": {referrerPolicy: t._scenario.referrer_policy}, |
| 80 "rel-noreferrer": {rel: "noreferrer"} |
| 81 }; |
| 82 |
| 83 var delivery_method = t._scenario.delivery_method; |
| 84 |
| 85 if (delivery_method in elementAttributesForDeliveryMethod) { |
| 86 invoker(t._subresourceUrl, |
| 87 callback, |
| 88 elementAttributesForDeliveryMethod[delivery_method], |
| 89 t._scenario.referrer_policy, |
| 90 test); |
| 91 } else { |
| 92 invoker(t._subresourceUrl, callback, null, t._scenario.referrer_policy,
test); |
| 93 } |
| 94 |
| 95 }, |
| 96 |
| 97 start: function() { |
| 98 t._constructSubresourceUrl(); |
| 99 t._constructExpectedReferrerUrl(); |
| 100 |
| 101 var test = async_test(t._testDescription); |
| 102 |
| 103 t._invokeSubresource(function(result) { |
| 104 // Check if the result is in valid format. NOOP in release. |
| 105 sanityChecker.checkSubresourceResult( |
| 106 test, t._scenario, t._subresourceUrl, result); |
| 107 |
| 108 // Check the reported URL. |
| 109 test.step(function() { |
| 110 assert_equals(result.referrer, |
| 111 t._expectedReferrerUrl, |
| 112 "Reported Referrer URL is '" + |
| 113 t._scenario.referrer_url + "'."); |
| 114 assert_equals(result.headers.referer, |
| 115 t._expectedReferrerUrl, |
| 116 "Reported Referrer URL from HTTP header is '" + |
| 117 t._expectedReferrerUrl + "'"); |
| 118 }, "Reported Referrer URL is as expected: " + t._scenario.referrer_url); |
| 119 |
| 120 test.done(); |
| 121 }, test); |
| 122 |
| 123 } |
| 124 } |
| 125 |
| 126 return t; |
| 127 } |
OLD | NEW |