{"size": 3260, "ext": "js", "max_stars_repo_path": "third_party/blink/web_tests/inspector-protocol/network/resource-type.js", "max_stars_repo_name": "zealoussnow/chromium", "max_stars_repo_stars_event_min_datetime": "2015-01-01T01:57:10.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T23:33:32.000Z", "max_issues_repo_path": "third_party/blink/web_tests/inspector-protocol/network/resource-type.js", "max_issues_repo_name": "zealoussnow/chromium", "max_issues_repo_issues_event_min_datetime": "2015-05-04T09:58:14.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-31T19:35:03.000Z", "max_forks_repo_path": "third_party/blink/web_tests/inspector-protocol/network/resource-type.js", "max_forks_repo_name": "zealoussnow/chromium", "max_forks_repo_forks_event_min_datetime": "2015-01-02T11:32:21.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T16:35:46.000Z"}
{"max_stars_count": 14668, "max_issues_count": 113, "max_forks_count": 5941, "avg_line_length": 31.6504854369, "max_line_length": 127, "alphanum_fraction": 0.6288343558}
(async function(testRunner) {
  var {page, session, dp} = await testRunner.startBlank(`Tests that responseReceived contains the documented resource types.`);

  var resources = [
    { url: '/resources-page.html', responseAvailable: false },
    { url: '/stylesheet.css'},
    { url: '/script.js'},
    { url: '/abe.png'},
    { url: '/test.wav'},
    { url: '/test.ogv'},
    { url: '/simple-captions.vtt'},
    { url: '/greenbox.png'}
  ];
  var resourcesLeft = resources.length;

  testRunner.log('Test started');
  var messageObject = await dp.Network.enable();
  if (messageObject.error) {
    testRunner.log(`FAIL: Couldn't enable network agent: ${messageObject.error.message}`);
    testRunner.completeTest();
    return;
  }
  testRunner.log('Network agent enabled');

  dp.Network.onRequestWillBeSent(event => {
    var url = event.params.request.url;
    if (url.startsWith('blob'))
      return;

    var type = event.params.type;
    for (var resource of resources) {
      if (!url.endsWith(resource.url))
        continue;
      if (resource.gotRequestType)
        testRunner.fail('FAIL: Requested resource ' + url + ' twice.');
      resource.gotRequestType = type;
      return;
    }
    testRunner.fail('FAIL: Requested unexpected resource ' + url);
  });

  dp.Network.onResponseReceived(event => {
    var url = event.params.response.url;
    if (url.indexOf('blob') === 0)
      return;

    var type = event.params.type;
    var requestId = event.params.requestId;
    for (var resource of resources) {
      if (url.substring(url.length - resource.url.length) !== resource.url)
        continue;
      if (resource.gotType)
        testRunner.fail('FAIL: Received resource ' + url + ' twice.');
      resource.gotType = type;
      resource.requestId = requestId;
      return;
    }
    testRunner.fail('FAIL: received unexpected resource ' + url);
  });

  dp.Network.onLoadingFinished(async event => {
    var requestId = event.params.requestId;
    for (var resource of resources) {
      if (resource.requestId !== requestId)
        continue;
      if (!('responseAvailable' in resource)) {
        var protocolResponse = await dp.Network.getResponseBody({requestId});
        if (protocolResponse.error) {
          resource.responseAvailable = false;
        } else {
          resource.responseAvailable = true;
          resource.responseEncoded = protocolResponse.result.base64Encoded;
        }
      }
      resourcesLeft -= 1;
      if (resourcesLeft === 0)
        receivedAllResources();
    }
  });

  function receivedAllResources() {
    for (var resource of resources) {
      var description = [
        'Resource ' + resource.url + ':',
        '  - request type: ' + resource.gotRequestType,
        '  - response type: ' + resource.gotType,
        '  - responseAvailable: ' + resource.responseAvailable
      ];
      if (resource.responseAvailable)
        description.push('  - responseEncoded: ' + resource.responseEncoded);

      testRunner.log(description.join('\n'));
    }
    testRunner.completeTest();
  }

  session.evaluate(`
    var iframe = document.createElement('iframe');
    iframe.src = '${testRunner.url('../resources/resources-page.html')}';
    document.body.appendChild(iframe);
  `);
})
