11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/5832
41cb0ef41Sopenharmony_cirequire('../common');
51cb0ef41Sopenharmony_ciconst url = require('url');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
81cb0ef41Sopenharmony_ciconst tests = require(
91cb0ef41Sopenharmony_ci  fixtures.path('wpt', 'url', 'resources', 'urltestdata.json'),
101cb0ef41Sopenharmony_ci);
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cilet failed = 0;
131cb0ef41Sopenharmony_cilet attempted = 0;
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_citests.forEach((test) => {
161cb0ef41Sopenharmony_ci  attempted++;
171cb0ef41Sopenharmony_ci  // Skip comments
181cb0ef41Sopenharmony_ci  if (typeof test === 'string') return;
191cb0ef41Sopenharmony_ci  let parsed;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  try {
221cb0ef41Sopenharmony_ci    // Attempt to parse
231cb0ef41Sopenharmony_ci    parsed = url.parse(url.resolve(test.base, test.input));
241cb0ef41Sopenharmony_ci    if (test.failure) {
251cb0ef41Sopenharmony_ci      // If the test was supposed to fail and we didn't get an
261cb0ef41Sopenharmony_ci      // error, treat it as a failure.
271cb0ef41Sopenharmony_ci      failed++;
281cb0ef41Sopenharmony_ci    } else {
291cb0ef41Sopenharmony_ci      // Test was not supposed to fail, so we're good so far. Now
301cb0ef41Sopenharmony_ci      // check the results of the parse.
311cb0ef41Sopenharmony_ci      let username, password;
321cb0ef41Sopenharmony_ci      try {
331cb0ef41Sopenharmony_ci        assert.strictEqual(test.href, parsed.href);
341cb0ef41Sopenharmony_ci        assert.strictEqual(test.protocol, parsed.protocol);
351cb0ef41Sopenharmony_ci        username = parsed.auth ? parsed.auth.split(':', 2)[0] : '';
361cb0ef41Sopenharmony_ci        password = parsed.auth ? parsed.auth.split(':', 2)[1] : '';
371cb0ef41Sopenharmony_ci        assert.strictEqual(test.username, username);
381cb0ef41Sopenharmony_ci        assert.strictEqual(test.password, password);
391cb0ef41Sopenharmony_ci        assert.strictEqual(test.host, parsed.host);
401cb0ef41Sopenharmony_ci        assert.strictEqual(test.hostname, parsed.hostname);
411cb0ef41Sopenharmony_ci        assert.strictEqual(+test.port, +parsed.port);
421cb0ef41Sopenharmony_ci        assert.strictEqual(test.pathname, parsed.pathname || '/');
431cb0ef41Sopenharmony_ci        assert.strictEqual(test.search, parsed.search || '');
441cb0ef41Sopenharmony_ci        assert.strictEqual(test.hash, parsed.hash || '');
451cb0ef41Sopenharmony_ci      } catch {
461cb0ef41Sopenharmony_ci        // For now, we're just interested in the number of failures.
471cb0ef41Sopenharmony_ci        failed++;
481cb0ef41Sopenharmony_ci      }
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci  } catch {
511cb0ef41Sopenharmony_ci    // If Parse failed and it wasn't supposed to, treat it as a failure.
521cb0ef41Sopenharmony_ci    if (!test.failure)
531cb0ef41Sopenharmony_ci      failed++;
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci});
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciassert.ok(failed === 0, `${failed} failed tests (out of ${attempted})`);
58