11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst url = require('url');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_cifunction createWithNoPrototype(properties = []) {
71cb0ef41Sopenharmony_ci  const noProto = Object.create(null);
81cb0ef41Sopenharmony_ci  properties.forEach((property) => {
91cb0ef41Sopenharmony_ci    noProto[property.key] = property.value;
101cb0ef41Sopenharmony_ci  });
111cb0ef41Sopenharmony_ci  return noProto;
121cb0ef41Sopenharmony_ci}
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction check(actual, expected) {
151cb0ef41Sopenharmony_ci  assert.notStrictEqual(Object.getPrototypeOf(actual), Object.prototype);
161cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Object.keys(actual).sort(),
171cb0ef41Sopenharmony_ci                         Object.keys(expected).sort());
181cb0ef41Sopenharmony_ci  Object.keys(expected).forEach(function(key) {
191cb0ef41Sopenharmony_ci    assert.deepStrictEqual(actual[key], expected[key]);
201cb0ef41Sopenharmony_ci  });
211cb0ef41Sopenharmony_ci}
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst parseTestsWithQueryString = {
241cb0ef41Sopenharmony_ci  '/foo/bar?baz=quux#frag': {
251cb0ef41Sopenharmony_ci    href: '/foo/bar?baz=quux#frag',
261cb0ef41Sopenharmony_ci    hash: '#frag',
271cb0ef41Sopenharmony_ci    search: '?baz=quux',
281cb0ef41Sopenharmony_ci    query: createWithNoPrototype([{ key: 'baz', value: 'quux' }]),
291cb0ef41Sopenharmony_ci    pathname: '/foo/bar',
301cb0ef41Sopenharmony_ci    path: '/foo/bar?baz=quux'
311cb0ef41Sopenharmony_ci  },
321cb0ef41Sopenharmony_ci  'http://example.com': {
331cb0ef41Sopenharmony_ci    href: 'http://example.com/',
341cb0ef41Sopenharmony_ci    protocol: 'http:',
351cb0ef41Sopenharmony_ci    slashes: true,
361cb0ef41Sopenharmony_ci    host: 'example.com',
371cb0ef41Sopenharmony_ci    hostname: 'example.com',
381cb0ef41Sopenharmony_ci    query: createWithNoPrototype(),
391cb0ef41Sopenharmony_ci    search: null,
401cb0ef41Sopenharmony_ci    pathname: '/',
411cb0ef41Sopenharmony_ci    path: '/'
421cb0ef41Sopenharmony_ci  },
431cb0ef41Sopenharmony_ci  '/example': {
441cb0ef41Sopenharmony_ci    protocol: null,
451cb0ef41Sopenharmony_ci    slashes: null,
461cb0ef41Sopenharmony_ci    auth: undefined,
471cb0ef41Sopenharmony_ci    host: null,
481cb0ef41Sopenharmony_ci    port: null,
491cb0ef41Sopenharmony_ci    hostname: null,
501cb0ef41Sopenharmony_ci    hash: null,
511cb0ef41Sopenharmony_ci    search: null,
521cb0ef41Sopenharmony_ci    query: createWithNoPrototype(),
531cb0ef41Sopenharmony_ci    pathname: '/example',
541cb0ef41Sopenharmony_ci    path: '/example',
551cb0ef41Sopenharmony_ci    href: '/example'
561cb0ef41Sopenharmony_ci  },
571cb0ef41Sopenharmony_ci  '/example?query=value': {
581cb0ef41Sopenharmony_ci    protocol: null,
591cb0ef41Sopenharmony_ci    slashes: null,
601cb0ef41Sopenharmony_ci    auth: undefined,
611cb0ef41Sopenharmony_ci    host: null,
621cb0ef41Sopenharmony_ci    port: null,
631cb0ef41Sopenharmony_ci    hostname: null,
641cb0ef41Sopenharmony_ci    hash: null,
651cb0ef41Sopenharmony_ci    search: '?query=value',
661cb0ef41Sopenharmony_ci    query: createWithNoPrototype([{ key: 'query', value: 'value' }]),
671cb0ef41Sopenharmony_ci    pathname: '/example',
681cb0ef41Sopenharmony_ci    path: '/example?query=value',
691cb0ef41Sopenharmony_ci    href: '/example?query=value'
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci};
721cb0ef41Sopenharmony_cifor (const u in parseTestsWithQueryString) {
731cb0ef41Sopenharmony_ci  const actual = url.parse(u, true);
741cb0ef41Sopenharmony_ci  const expected = Object.assign(new url.Url(), parseTestsWithQueryString[u]);
751cb0ef41Sopenharmony_ci  for (const i in actual) {
761cb0ef41Sopenharmony_ci    if (actual[i] === null && expected[i] === undefined) {
771cb0ef41Sopenharmony_ci      expected[i] = null;
781cb0ef41Sopenharmony_ci    }
791cb0ef41Sopenharmony_ci  }
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  const properties = Object.keys(actual).sort();
821cb0ef41Sopenharmony_ci  assert.deepStrictEqual(properties, Object.keys(expected).sort());
831cb0ef41Sopenharmony_ci  properties.forEach((property) => {
841cb0ef41Sopenharmony_ci    if (property === 'query') {
851cb0ef41Sopenharmony_ci      check(actual[property], expected[property]);
861cb0ef41Sopenharmony_ci    } else {
871cb0ef41Sopenharmony_ci      assert.deepStrictEqual(actual[property], expected[property]);
881cb0ef41Sopenharmony_ci    }
891cb0ef41Sopenharmony_ci  });
901cb0ef41Sopenharmony_ci}
91