function redirectLocationTest(toUrlDesc, {
  toUrl = undefined,
  status,
  expectStatus = undefined,
  mode,
  shouldPass = true
} = {}) {
  toUrlDesc = toUrl ? `with ${toUrlDesc}` : `without`;
  const desc = `Redirect ${status} in "${mode}" mode ${toUrlDesc} location`;
  const url = `${RESOURCES_DIR}redirect.py?redirect_status=${status}` +
      (toUrl ? `&location=${encodeURIComponent(toUrl)}` : '');
  const requestInit = {'redirect': mode};
  if (!expectStatus)
    expectStatus = status;

  promise_test((test) => {
    if (mode === 'error' || !shouldPass)
      return promise_rejects_js(test, TypeError, fetch(url, requestInit));
    if (mode === 'manual')
      return fetch(url, requestInit).then((resp) => {
        assert_equals(resp.status, 0, "Response's status is 0");
        assert_equals(resp.type, "opaqueredirect", "Response's type is opaqueredirect");
        assert_equals(resp.statusText, '', `Response's statusText is ""`);
        assert_true(resp.headers.entries().next().done, "Headers should be empty");
      });

    if (mode === 'follow')
      return fetch(url, requestInit).then((resp) => {
        assert_equals(
            resp.status, expectStatus, `Response's status is ${expectStatus}`);
      });
    assert_unreached(`${mode} is not a valid redirect mode`);
  }, desc);
}
A test to fetch a URL that returns response redirecting to `toUrl` with
`status` as its HTTP status code. `expectStatus` can be set to test the
status code in fetch's Promise response.