<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
// Test handling of blocked responses in CORB/ORB.
function probe() {
  // We will cross-origin load a script resource that should get blocked by all
  // versions of CORB/ORB. Three things may happen:
  //
  // 1, The script might execute. (CORB/ORB not supported. Or a bug.)
  // 2, An empty response is injected: The script loads but nothing is executed.
  //    (Expected behaviour for CORB and "ORB v0.1".)
  // 3, An error is injected and script loading aborts. (Expected for ORB.)

  // A cross-origin response labelled as text/csv, which will call
  // script_callback when executed.
  const probe = "https://{{domains[www1]}}:{{ports[https][0]}}" +
      "/fetch/corb/resources/response_block_probe.js";

  // Load the probe as a script.
  const script = document.createElement("script");
  script.src = probe;
  document.body.appendChild(script);

  // Return a promise that will return a string description corresponding to the
  // three conditions above. Not that a script_callback call is processed
  // synchronously and hence will occur before the onload event is dispatched.
  return new Promise((resolve, reject) => {
    script.onload = _ => resolve("script loaded");
    script.onerror = _ => resolve("script errored");
    window.script_callback = _ => resolve("script executed");
  });
}

promise_test(t => probe().then(
    value => assert_equals(value, "script errored")),
    "ORB: Expect error response.");

// This test ensures we're _not_ seeing CORB behaviour.
promise_test(t => probe().then(
    value => assert_not_equals(value, "script loaded")),
    "!CORB: CORB would have expected an empty response.");
</script>
