From 29a7bcd6eb0184a1d15bc128600f7ccfbfcf21fc Mon Sep 17 00:00:00 2001 From: Oliver Joseph Ash Date: Thu, 26 Dec 2019 16:56:04 +0000 Subject: [PATCH] Node: `UrlObject`: omit `path` (#40939) * Node: `UrlObject`: omit `path` * Add working test * Move tests into module * Copy changes to v10 and v11 * Revert v11 --- types/node/node-tests.ts | 152 ----------------------------------- types/node/test/url.ts | 151 ++++++++++++++++++++++++++++++++++ types/node/tsconfig.json | 3 +- types/node/url.d.ts | 1 - types/node/v10/test/url.ts | 5 ++ types/node/v10/tsconfig.json | 3 +- types/node/v10/url.d.ts | 2 +- 7 files changed, 161 insertions(+), 156 deletions(-) create mode 100644 types/node/test/url.ts create mode 100644 types/node/v10/test/url.ts diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index ad91504a85..a63d7aee5b 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -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 /// ////////////////////////////////////////////////////// diff --git a/types/node/test/url.ts b/types/node/test/url.ts new file mode 100644 index 0000000000..b45c829e7d --- /dev/null +++ b/types/node/test/url.ts @@ -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'); +} diff --git a/types/node/tsconfig.json b/types/node/tsconfig.json index 6f97f05513..53ace26dc8 100644 --- a/types/node/tsconfig.json +++ b/types/node/tsconfig.json @@ -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", diff --git a/types/node/url.d.ts b/types/node/url.d.ts index e5b7e28566..d3a395b0c4 100644 --- a/types/node/url.d.ts +++ b/types/node/url.d.ts @@ -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; diff --git a/types/node/v10/test/url.ts b/types/node/v10/test/url.ts new file mode 100644 index 0000000000..9a8bc8b8fe --- /dev/null +++ b/types/node/v10/test/url.ts @@ -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' }); diff --git a/types/node/v10/tsconfig.json b/types/node/v10/tsconfig.json index aa3d2812f1..67bcb08130 100644 --- a/types/node/v10/tsconfig.json +++ b/types/node/v10/tsconfig.json @@ -1,7 +1,8 @@ { "files": [ "index.d.ts", - "node-tests.ts" + "node-tests.ts", + "test/url.ts" ], "compilerOptions": { "module": "commonjs", diff --git a/types/node/v10/url.d.ts b/types/node/v10/url.d.ts index 1e1d154e8e..b44220f9cb 100644 --- a/types/node/v10/url.d.ts +++ b/types/node/v10/url.d.ts @@ -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 {