Node: UrlObject: omit path (#40939)

* Node: `UrlObject`: omit `path`

* Add working test

* Move tests into module

* Copy changes to v10 and v11

* Revert v11
This commit is contained in:
Oliver Joseph Ash
2019-12-26 16:56:04 +00:00
committed by Andrew Branch
parent 616768386e
commit 29a7bcd6eb
7 changed files with 161 additions and 156 deletions

View File

@@ -1,4 +1,3 @@
import assert = require("assert");
import * as fs from "fs";
import * as url from "url";
import * as util from "util";
@@ -10,157 +9,6 @@ import * as dns from "dns";
import * as inspector from "inspector";
import * as trace_events from "trace_events";
////////////////////////////////////////////////////
/// Url tests : http://nodejs.org/api/url.html
////////////////////////////////////////////////////
{
{
url.format(url.parse('http://www.example.com/xyz'));
url.format('http://www.example.com/xyz');
// https://google.com/search?q=you're%20a%20lizard%2C%20gary
url.format({
protocol: 'https',
host: "google.com",
pathname: 'search',
query: { q: "you're a lizard, gary" }
});
const myURL = new url.URL('https://a:b@你好你好?abc#foo');
url.format(myURL, { fragment: false, unicode: true, auth: false });
}
{
const helloUrl = url.parse('http://example.com/?hello=world', true);
let helloQuery = helloUrl.query['hello'];
assert.equal(helloUrl.query['hello'], 'world');
let strUrl = url.parse('http://example.com/?hello=world');
let queryStr: string = strUrl.query!;
strUrl = url.parse('http://example.com/?hello=world', false);
queryStr = strUrl.query!;
function getBoolean(): boolean { return false; }
const urlUrl = url.parse('http://example.com/?hello=world', getBoolean());
if (typeof(urlUrl.query) === 'string') {
queryStr = urlUrl.query;
} else if (urlUrl.query) {
helloQuery = urlUrl.query['hello'];
}
}
{
const ascii: string = url.domainToASCII('español.com');
const unicode: string = url.domainToUnicode('xn--espaol-zwa.com');
}
{
let myURL = new url.URL('https://theuser:thepwd@example.org:81/foo/path?query=string#bar');
assert.equal(myURL.hash, '#bar');
assert.equal(myURL.host, 'example.org:81');
assert.equal(myURL.hostname, 'example.org');
assert.equal(myURL.href, 'https://theuser:thepwd@example.org:81/foo/path?query=string#bar');
assert.equal(myURL.origin, 'https://example.org:81');
assert.equal(myURL.password, 'thepwd');
assert.equal(myURL.username, 'theuser');
assert.equal(myURL.pathname, '/foo/path');
assert.equal(myURL.port, "81");
assert.equal(myURL.protocol, "https:");
assert.equal(myURL.search, "?query=string");
assert.equal(myURL.toString(), 'https://theuser:thepwd@example.org:81/foo/path?query=string#bar');
assert(myURL.searchParams instanceof url.URLSearchParams);
myURL.host = 'example.org:82';
myURL.hostname = 'example.com';
myURL.href = 'http://other.com';
myURL.hash = 'baz';
myURL.password = "otherpwd";
myURL.username = "otheruser";
myURL.pathname = "/otherPath";
myURL.port = "82";
myURL.protocol = "http";
myURL.search = "a=b";
assert.equal(myURL.href, 'http://otheruser:otherpwd@other.com:82/otherPath?a=b#baz');
myURL = new url.URL('/foo', 'https://example.org/');
assert.equal(myURL.href, 'https://example.org/foo');
assert.equal(myURL.toJSON(), myURL.href);
}
{
const searchParams = new url.URLSearchParams('abc=123');
assert.equal(searchParams.toString(), 'abc=123');
searchParams.forEach((value: string, name: string, me: url.URLSearchParams): void => {
assert.equal(name, 'abc');
assert.equal(value, '123');
assert.equal(me, searchParams);
});
assert.equal(searchParams.get('abc'), '123');
searchParams.append('abc', 'xyz');
assert.deepEqual(searchParams.getAll('abc'), ['123', 'xyz']);
const entries = searchParams.entries();
assert.deepEqual(entries.next(), { value: ["abc", "123"], done: false });
assert.deepEqual(entries.next(), { value: ["abc", "xyz"], done: false });
assert.deepEqual(entries.next(), { value: undefined, done: true });
const keys = searchParams.keys();
assert.deepEqual(keys.next(), { value: "abc", done: false });
assert.deepEqual(keys.next(), { value: "abc", done: false });
assert.deepEqual(keys.next(), { value: undefined, done: true });
const values = searchParams.values();
assert.deepEqual(values.next(), { value: "123", done: false });
assert.deepEqual(values.next(), { value: "xyz", done: false });
assert.deepEqual(values.next(), { value: undefined, done: true });
searchParams.set('abc', 'b');
assert.deepEqual(searchParams.getAll('abc'), ['b']);
searchParams.delete('a');
assert(!searchParams.has('a'));
assert.equal(searchParams.get('a'), null);
searchParams.sort();
}
{
const searchParams = new url.URLSearchParams({
user: 'abc',
query: ['first', 'second']
});
assert.equal(searchParams.toString(), 'user=abc&query=first%2Csecond');
assert.deepEqual(searchParams.getAll('query'), ['first,second']);
}
{
// Using an array
const params = new url.URLSearchParams([
['user', 'abc'],
['query', 'first'],
['query', 'second'],
] as Array<[string, string]>);
assert.equal(params.toString(), 'user=abc&query=first&query=second');
}
{
let path: string = url.fileURLToPath('file://test');
path = url.fileURLToPath(new url.URL('file://test'));
}
{
const path: url.URL = url.pathToFileURL('file://test');
}
}
//////////////////////////////////////////////////////
/// Https tests : http://nodejs.org/api/https.html ///
//////////////////////////////////////////////////////

151
types/node/test/url.ts Normal file
View File

@@ -0,0 +1,151 @@
import assert = require("assert");
import * as url from 'url';
{
url.format(url.parse('http://www.example.com/xyz'));
url.format('http://www.example.com/xyz');
// https://google.com/search?q=you're%20a%20lizard%2C%20gary
url.format({
protocol: 'https',
host: "google.com",
pathname: 'search',
query: { q: "you're a lizard, gary" }
});
const myURL = new url.URL('https://a:b@你好你好?abc#foo');
url.format(myURL, { fragment: false, unicode: true, auth: false });
// `format doesn't work with `path`: use `pathname` and `search` instead
// $ExpectError
url.format({ path: '/foo' });
}
{
const helloUrl = url.parse('http://example.com/?hello=world', true);
let helloQuery = helloUrl.query['hello'];
assert.equal(helloUrl.query['hello'], 'world');
let strUrl = url.parse('http://example.com/?hello=world');
let queryStr: string = strUrl.query!;
strUrl = url.parse('http://example.com/?hello=world', false);
queryStr = strUrl.query!;
function getBoolean(): boolean { return false; }
const urlUrl = url.parse('http://example.com/?hello=world', getBoolean());
if (typeof(urlUrl.query) === 'string') {
queryStr = urlUrl.query;
} else if (urlUrl.query) {
helloQuery = urlUrl.query['hello'];
}
}
{
const ascii: string = url.domainToASCII('español.com');
const unicode: string = url.domainToUnicode('xn--espaol-zwa.com');
}
{
let myURL = new url.URL('https://theuser:thepwd@example.org:81/foo/path?query=string#bar');
assert.equal(myURL.hash, '#bar');
assert.equal(myURL.host, 'example.org:81');
assert.equal(myURL.hostname, 'example.org');
assert.equal(myURL.href, 'https://theuser:thepwd@example.org:81/foo/path?query=string#bar');
assert.equal(myURL.origin, 'https://example.org:81');
assert.equal(myURL.password, 'thepwd');
assert.equal(myURL.username, 'theuser');
assert.equal(myURL.pathname, '/foo/path');
assert.equal(myURL.port, "81");
assert.equal(myURL.protocol, "https:");
assert.equal(myURL.search, "?query=string");
assert.equal(myURL.toString(), 'https://theuser:thepwd@example.org:81/foo/path?query=string#bar');
assert(myURL.searchParams instanceof url.URLSearchParams);
myURL.host = 'example.org:82';
myURL.hostname = 'example.com';
myURL.href = 'http://other.com';
myURL.hash = 'baz';
myURL.password = "otherpwd";
myURL.username = "otheruser";
myURL.pathname = "/otherPath";
myURL.port = "82";
myURL.protocol = "http";
myURL.search = "a=b";
assert.equal(myURL.href, 'http://otheruser:otherpwd@other.com:82/otherPath?a=b#baz');
myURL = new url.URL('/foo', 'https://example.org/');
assert.equal(myURL.href, 'https://example.org/foo');
assert.equal(myURL.toJSON(), myURL.href);
}
{
const searchParams = new url.URLSearchParams('abc=123');
assert.equal(searchParams.toString(), 'abc=123');
searchParams.forEach((value: string, name: string, me: url.URLSearchParams): void => {
assert.equal(name, 'abc');
assert.equal(value, '123');
assert.equal(me, searchParams);
});
assert.equal(searchParams.get('abc'), '123');
searchParams.append('abc', 'xyz');
assert.deepEqual(searchParams.getAll('abc'), ['123', 'xyz']);
const entries = searchParams.entries();
assert.deepEqual(entries.next(), { value: ["abc", "123"], done: false });
assert.deepEqual(entries.next(), { value: ["abc", "xyz"], done: false });
assert.deepEqual(entries.next(), { value: undefined, done: true });
const keys = searchParams.keys();
assert.deepEqual(keys.next(), { value: "abc", done: false });
assert.deepEqual(keys.next(), { value: "abc", done: false });
assert.deepEqual(keys.next(), { value: undefined, done: true });
const values = searchParams.values();
assert.deepEqual(values.next(), { value: "123", done: false });
assert.deepEqual(values.next(), { value: "xyz", done: false });
assert.deepEqual(values.next(), { value: undefined, done: true });
searchParams.set('abc', 'b');
assert.deepEqual(searchParams.getAll('abc'), ['b']);
searchParams.delete('a');
assert(!searchParams.has('a'));
assert.equal(searchParams.get('a'), null);
searchParams.sort();
}
{
const searchParams = new url.URLSearchParams({
user: 'abc',
query: ['first', 'second']
});
assert.equal(searchParams.toString(), 'user=abc&query=first%2Csecond');
assert.deepEqual(searchParams.getAll('query'), ['first,second']);
}
{
// Using an array
const params = new url.URLSearchParams([
['user', 'abc'],
['query', 'first'],
['query', 'second'],
] as Array<[string, string]>);
assert.equal(params.toString(), 'user=abc&query=first&query=second');
}
{
let path: string = url.fileURLToPath('file://test');
path = url.fileURLToPath(new url.URL('file://test'));
}
{
const path: url.URL = url.pathToFileURL('file://test');
}

View File

@@ -32,7 +32,8 @@
"test/v8.ts",
"test/vm.ts",
"test/worker_threads.ts",
"test/zlib.ts"
"test/zlib.ts",
"test/url.ts"
],
"compilerOptions": {
"module": "commonjs",

1
types/node/url.d.ts vendored
View File

@@ -8,7 +8,6 @@ declare module "url" {
host?: string | null;
hostname?: string | null;
href?: string | null;
path?: string | null;
pathname?: string | null;
protocol?: string | null;
search?: string | null;

View File

@@ -0,0 +1,5 @@
import * as url from 'url';
// `format doesn't work with `path`: use `pathname` and `search` instead
// $ExpectError
url.format({ path: '/foo' });

View File

@@ -1,7 +1,8 @@
{
"files": [
"index.d.ts",
"node-tests.ts"
"node-tests.ts",
"test/url.ts"
],
"compilerOptions": {
"module": "commonjs",

View File

@@ -7,7 +7,6 @@ declare module "url" {
host?: string;
hostname?: string;
href?: string;
path?: string;
pathname?: string;
protocol?: string;
search?: string;
@@ -24,6 +23,7 @@ declare module "url" {
interface Url extends UrlObjectCommon {
port?: string;
query?: string | null | ParsedUrlQuery;
path?: string;
}
interface UrlWithParsedQuery extends Url {