From f3ae6da72daa7c4cd5aba56ddc7668d0224aa593 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Wed, 11 Mar 2020 14:37:06 -0400 Subject: [PATCH] URL: Use test data from web-platform-tests for isURL spec conformance (#20537) * URL: Use test data from web-platform-tests for isURL spec conformance * URL: Change fetch utility to parse JSON Early iterations included LICENSE fetching as string, but this was later changed to static file. As such, the utility is better served to include JSON parsing. * URL: Stop fetch execution when rejecting by status code Interestingly, rejecting (throwing) does not itself halt execution. * URL: Update fetchJSON function description * URL: Try using react-native-url-polyfill * URL: Clarify default exceptions as about:blank parameter specific * URL: Add Native-specific isURL exceptions * URL: Move WPT license into explanatory README.md file --- package-lock.json | 44 ++++++- packages/url/package.json | 3 +- packages/url/scripts/download-wpt-data.js | 118 +++++++++++++++++++ packages/url/src/is-url.native.js | 26 ++-- packages/url/src/test/fixtures/README.md | 25 ++++ packages/url/src/test/fixtures/wpt-data.json | 1 + packages/url/src/test/index.native.js | 23 ++++ packages/url/src/test/index.test.js | 35 ++---- tsconfig.json | 2 +- 9 files changed, 230 insertions(+), 47 deletions(-) create mode 100644 packages/url/scripts/download-wpt-data.js create mode 100644 packages/url/src/test/fixtures/README.md create mode 100644 packages/url/src/test/fixtures/wpt-data.json diff --git a/package-lock.json b/package-lock.json index 30ea302d26b49..42dabf49da7d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11073,7 +11073,8 @@ "requires": { "@babel/runtime": "^7.8.3", "lodash": "^4.17.15", - "qs": "^6.5.2" + "qs": "^6.5.2", + "react-native-url-polyfill": "^1.1.2" } }, "@wordpress/viewport": { @@ -13504,8 +13505,7 @@ "base64-js": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.0.tgz", - "integrity": "sha512-ccav/yGvoa80BQDljCxsmmQ3Xvx60/UpBIij5QN21W3wBi/hhIC9OoO+KLpu9IJTS9j4DRVJ3aDDF9cMSoa2lw==", - "dev": true + "integrity": "sha512-ccav/yGvoa80BQDljCxsmmQ3Xvx60/UpBIij5QN21W3wBi/hhIC9OoO+KLpu9IJTS9j4DRVJ3aDDF9cMSoa2lw==" }, "basic-auth": { "version": "2.0.1", @@ -21430,8 +21430,7 @@ "ieee754": { "version": "1.1.12", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.12.tgz", - "integrity": "sha512-GguP+DRY+pJ3soyIiGPTvdiVXjZ+DbXOxGpXn3eMvNW4x4irjqXm4wHKscC+TfxSJ0yw/S1F24tqdMNsMZTiLA==", - "dev": true + "integrity": "sha512-GguP+DRY+pJ3soyIiGPTvdiVXjZ+DbXOxGpXn3eMvNW4x4irjqXm4wHKscC+TfxSJ0yw/S1F24tqdMNsMZTiLA==" }, "iferr": { "version": "0.1.5", @@ -33830,6 +33829,26 @@ } } }, + "react-native-url-polyfill": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/react-native-url-polyfill/-/react-native-url-polyfill-1.1.2.tgz", + "integrity": "sha512-RPYwjW+4udnAf26xUCQP2dn4t2tnRFo3Ii4s/hy7Ivpe7xYtXp7CMVX505CR8X3p0f8NKmOJ4MQEFMMnbd/Y/Q==", + "requires": { + "buffer": "^5.4.3", + "whatwg-url-without-unicode": "8.0.0-1" + }, + "dependencies": { + "buffer": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.5.0.tgz", + "integrity": "sha512-9FTEDjLjwoAkEwyMGDjYJQN2gfRgOKBKRfiglhvibGbpeeU/pQn1bJxQqm32OD/AIeEuHxU9roxXxg34Byp/Ww==", + "requires": { + "base64-js": "^1.0.2", + "ieee754": "^1.1.4" + } + } + } + }, "react-outside-click-handler": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/react-outside-click-handler/-/react-outside-click-handler-1.2.2.tgz", @@ -40368,6 +40387,21 @@ "webidl-conversions": "^4.0.2" } }, + "whatwg-url-without-unicode": { + "version": "8.0.0-1", + "resolved": "https://registry.npmjs.org/whatwg-url-without-unicode/-/whatwg-url-without-unicode-8.0.0-1.tgz", + "integrity": "sha512-0Uy8mjsG5O8Y53327XL+ZqsrMdxO1CL/6m840SmW5iyRWFvU2zlxS2RzpD3pFFVKYOKCmsKn5JKzWxQ+bImnWA==", + "requires": { + "webidl-conversions": "^5.0.0" + }, + "dependencies": { + "webidl-conversions": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-5.0.0.tgz", + "integrity": "sha512-VlZwKPCkYKxQgeSbH5EyngOmRp7Ww7I9rQLERETtf5ofd9pGeswWiOtogpEO850jziPRarreGxn5QIiTqpb2wA==" + } + } + }, "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", diff --git a/packages/url/package.json b/packages/url/package.json index ab6b8f6cfcb1a..c564a35d316e1 100644 --- a/packages/url/package.json +++ b/packages/url/package.json @@ -24,7 +24,8 @@ "dependencies": { "@babel/runtime": "^7.8.3", "lodash": "^4.17.15", - "qs": "^6.5.2" + "qs": "^6.5.2", + "react-native-url-polyfill": "^1.1.2" }, "publishConfig": { "access": "public" diff --git a/packages/url/scripts/download-wpt-data.js b/packages/url/scripts/download-wpt-data.js new file mode 100644 index 0000000000000..f087d6202368e --- /dev/null +++ b/packages/url/scripts/download-wpt-data.js @@ -0,0 +1,118 @@ +/** + * External dependencies + */ +const { get } = require( 'https' ); +const path = require( 'path' ); +const fs = require( 'fs' ); + +/** + * Default file output destination. + */ +const DEFAULT_OUT_FILE = path.resolve( + __dirname, + '../src/test/fixtures/wpt-data.json' +); + +/** + * Source test data URL. + */ +const DATA_URL = + 'https://raw.githubusercontent.com/web-platform-tests/wpt/master/url/resources/urltestdata.json'; + +/** + * Items to exclude from the default test data, where the test case relies on + * an explicit `'about:blank'` base parameter provided to the constructor. The + * test data as given does not otherwise allow for distinction between a null + * base and base of `'about:blank'`. + * + * @type {string[]} + */ +const INPUT_EXCEPTIONS_ACTUAL_ABOUT_BLANK_BASE = [ '#x' ]; + +/** + * Given a URL, returns promise resolving to the downloaded URL contents parsed + * as JSON. + * + * @param {string} url URL to download. + * + * @return {Promise<*>} Promise resolving to result of parsed JSON. + */ +const fetchJSON = ( url ) => + new Promise( ( resolve, reject ) => { + get( url, async ( response ) => { + if ( response.statusCode !== 200 ) { + return reject(); + } + + let string = ''; + + for await ( const chunk of response ) { + string += chunk.toString(); + } + + resolve( JSON.parse( string ) ); + } ); + } ); + +/** + * Returns true if the given value is a test data item. + * + * @param {*} item Candidate to test. + * + * @return {boolean} Whether candidate is test data item. + */ +const isDataItem = ( item ) => item && item.input; + +/** + * Returns true if the given data item is expected to be used as the base + * parameter of a URL constructor. + * + * @param {Object} item Data item to test. + * + * @return {boolean} Whether data item has non-default base. + */ +const hasBase = ( item ) => item.base !== 'about:blank'; + +/** + * Returns true if the given data item is included in the exception set. + * + * @param {Object} item Data item to test. + * + * @return {boolean} Whether data item is exception. + */ +const isException = ( item ) => + INPUT_EXCEPTIONS_ACTUAL_ABOUT_BLANK_BASE.includes( item.input ); + +/** + * Downloads data and writes output file. + * + * @param {string} [outFile] Optional output file. + */ +async function download( outFile = DEFAULT_OUT_FILE ) { + const data = await fetchJSON( DATA_URL ); + + const transformedData = data + .filter( + ( item ) => + isDataItem( item ) && ! hasBase( item ) && ! isException( item ) + ) + .map( ( item ) => ( { + input: item.input, + failure: item.failure, + } ) ); + + const file = fs.createWriteStream( outFile ); + file.write( JSON.stringify( transformedData ) ); + file.close(); +} + +module.exports = download; + +if ( ! module.parent ) { + try { + download(); + } catch ( error ) { + process.stderr.write( error ); + process.statusCode = 1; + } +} diff --git a/packages/url/src/is-url.native.js b/packages/url/src/is-url.native.js index 71222cf7e74f3..9a8048a00fa56 100644 --- a/packages/url/src/is-url.native.js +++ b/packages/url/src/is-url.native.js @@ -1,16 +1,18 @@ -const URL_REGEXP = /^((([A-Za-z]{3,9}:(?:\/[\/]*)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)(?::\d{2,5})?((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)$/i; /** - * Determines whether the given string looks like a URL. - * - * @param {string} url The string to scrutinise. - * - * @example - * ```js - * const isURL = isURL( 'https://wordpress.org' ); // true - * ``` - * - * @return {boolean} Whether or not it looks like a URL. + * External dependencies + */ +import { URL } from 'react-native-url-polyfill'; + +/** + * @type {typeof import('./is-url').isURL} */ export function isURL( url ) { - return URL_REGEXP.test( url ); + // A URL can be considered value if the `URL` constructor is able to parse + // it. The constructor throws an error for an invalid URL. + try { + new URL( url ); + return true; + } catch ( error ) { + return false; + } } diff --git a/packages/url/src/test/fixtures/README.md b/packages/url/src/test/fixtures/README.md new file mode 100644 index 0000000000000..7914f5f0866a9 --- /dev/null +++ b/packages/url/src/test/fixtures/README.md @@ -0,0 +1,25 @@ +# URL Fixtures + +The `@wordpress/url` module uses data from the [Web Platform Tests project](https://github.com/web-platform-tests/wpt) to verify expected behavior of its functionality as conforming to the [URL Living Standard](https://url.spec.whatwg.org/). + +This data is updated manually. To bring in the latest data, run the download script: + +``` +node packages/url/scripts/download-wpt-data.js +``` + +The Web Platform Tests URL data is made available under the 3-Clause BSD License: + +``` +# The 3-Clause BSD License + +Copyright 2019 web-platform-tests contributors + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +``` diff --git a/packages/url/src/test/fixtures/wpt-data.json b/packages/url/src/test/fixtures/wpt-data.json new file mode 100644 index 0000000000000..173c93c3bfbec --- /dev/null +++ b/packages/url/src/test/fixtures/wpt-data.json @@ -0,0 +1 @@ +[{"input":"https://test:@test"},{"input":"https://:@test"},{"input":"non-special://test:@test/x"},{"input":"non-special://:@test/x"},{"input":"lolscheme:x x#x x"},{"input":"file://example:1/","failure":true},{"input":"file://example:test/","failure":true},{"input":"file://example%/","failure":true},{"input":"file://[example]/","failure":true},{"input":"http://example.com/././foo"},{"input":"http://example.com/./.foo"},{"input":"http://example.com/foo/."},{"input":"http://example.com/foo/./"},{"input":"http://example.com/foo/bar/.."},{"input":"http://example.com/foo/bar/../"},{"input":"http://example.com/foo/..bar"},{"input":"http://example.com/foo/bar/../ton"},{"input":"http://example.com/foo/bar/../ton/../../a"},{"input":"http://example.com/foo/../../.."},{"input":"http://example.com/foo/../../../ton"},{"input":"http://example.com/foo/%2e"},{"input":"http://example.com/foo/%2e%2"},{"input":"http://example.com/foo/%2e./%2e%2e/.%2e/%2e.bar"},{"input":"http://example.com////../.."},{"input":"http://example.com/foo/bar//../.."},{"input":"http://example.com/foo/bar//.."},{"input":"http://example.com/foo"},{"input":"http://example.com/%20foo"},{"input":"http://example.com/foo%"},{"input":"http://example.com/foo%2"},{"input":"http://example.com/foo%2zbar"},{"input":"http://example.com/foo%2©zbar"},{"input":"http://example.com/foo%41%7a"},{"input":"http://example.com/foo\t‘%91"},{"input":"http://example.com/foo%00%51"},{"input":"http://example.com/(%28:%3A%29)"},{"input":"http://example.com/%3A%3a%3C%3c"},{"input":"http://example.com/foo\tbar"},{"input":"http://example.com\\\\foo\\\\bar"},{"input":"http://example.com/%7Ffp3%3Eju%3Dduvgw%3Dd"},{"input":"http://example.com/@asdf%40"},{"input":"http://example.com/你好你好"},{"input":"http://example.com/‥/foo"},{"input":"http://example.com//foo"},{"input":"http://example.com/‮/foo/‭/bar"},{"input":"http://www.google.com/foo?bar=baz#"},{"input":"http://www.google.com/foo?bar=baz# »"},{"input":"data:test# »"},{"input":"http://www.google.com"},{"input":"http://192.0x00A80001"},{"input":"http://www/foo%2Ehtml"},{"input":"http://www/foo/%2E/html"},{"input":"http://user:pass@/","failure":true},{"input":"http://%25DOMAIN:foobar@foodomain.com/"},{"input":"http:\\\\www.google.com\\foo"},{"input":"http://foo:80/"},{"input":"http://foo:81/"},{"input":"httpa://foo:80/"},{"input":"http://foo:-80/","failure":true},{"input":"https://foo:443/"},{"input":"https://foo:80/"},{"input":"ftp://foo:21/"},{"input":"ftp://foo:80/"},{"input":"gopher://foo:70/"},{"input":"gopher://foo:443/"},{"input":"ws://foo:80/"},{"input":"ws://foo:81/"},{"input":"ws://foo:443/"},{"input":"ws://foo:815/"},{"input":"wss://foo:80/"},{"input":"wss://foo:81/"},{"input":"wss://foo:443/"},{"input":"wss://foo:815/"},{"input":"http:/example.com/"},{"input":"ftp:/example.com/"},{"input":"https:/example.com/"},{"input":"madeupscheme:/example.com/"},{"input":"file:/example.com/"},{"input":"ftps:/example.com/"},{"input":"gopher:/example.com/"},{"input":"ws:/example.com/"},{"input":"wss:/example.com/"},{"input":"data:/example.com/"},{"input":"javascript:/example.com/"},{"input":"mailto:/example.com/"},{"input":"http:example.com/"},{"input":"ftp:example.com/"},{"input":"https:example.com/"},{"input":"madeupscheme:example.com/"},{"input":"ftps:example.com/"},{"input":"gopher:example.com/"},{"input":"ws:example.com/"},{"input":"wss:example.com/"},{"input":"data:example.com/"},{"input":"javascript:example.com/"},{"input":"mailto:example.com/"},{"input":"http:@www.example.com"},{"input":"http:/@www.example.com"},{"input":"http://@www.example.com"},{"input":"http:a:b@www.example.com"},{"input":"http:/a:b@www.example.com"},{"input":"http://a:b@www.example.com"},{"input":"http://@pple.com"},{"input":"http::b@www.example.com"},{"input":"http:/:b@www.example.com"},{"input":"http://:b@www.example.com"},{"input":"http:/:@/www.example.com","failure":true},{"input":"http://user@/www.example.com","failure":true},{"input":"http:@/www.example.com","failure":true},{"input":"http:/@/www.example.com","failure":true},{"input":"http://@/www.example.com","failure":true},{"input":"https:@/www.example.com","failure":true},{"input":"http:a:b@/www.example.com","failure":true},{"input":"http:/a:b@/www.example.com","failure":true},{"input":"http://a:b@/www.example.com","failure":true},{"input":"http::@/www.example.com","failure":true},{"input":"http:a:@www.example.com"},{"input":"http:/a:@www.example.com"},{"input":"http://a:@www.example.com"},{"input":"http://www.@pple.com"},{"input":"http:@:www.example.com","failure":true},{"input":"http:/@:www.example.com","failure":true},{"input":"http://@:www.example.com","failure":true},{"input":"http://:@www.example.com"},{"input":"\u0000\u001b\u0004\u0012 http://example.com/\u001f \r "},{"input":"https://�","failure":true},{"input":"https://%EF%BF%BD","failure":true},{"input":"https://x/�?�#�"},{"input":"https://faß.ExAmPlE/"},{"input":"sc://faß.ExAmPlE/"},{"input":"https://x x:12","failure":true},{"input":"http://./"},{"input":"http://../"},{"input":"http://0..0x300/"},{"input":"http://[www.google.com]/","failure":true},{"input":"http://host/?'"},{"input":"notspecial://host/?'"},{"input":"about:/../"},{"input":"data:/../"},{"input":"javascript:/../"},{"input":"mailto:/../"},{"input":"sc://ñ.test/"},{"input":"sc://\u001f!\"$&'()*+,-.;<=>^_`{|}~/"},{"input":"sc://\u0000/","failure":true},{"input":"sc:// /","failure":true},{"input":"sc://%/"},{"input":"sc://@/","failure":true},{"input":"sc://te@s:t@/","failure":true},{"input":"sc://:/","failure":true},{"input":"sc://:12/","failure":true},{"input":"sc://[/","failure":true},{"input":"sc://\\/","failure":true},{"input":"sc://]/","failure":true},{"input":"sc:\\../"},{"input":"sc::a@example.net"},{"input":"wow:%NBD"},{"input":"wow:%1G"},{"input":"wow:￿"},{"input":"ftp://example.com%80/","failure":true},{"input":"ftp://example.com%A0/","failure":true},{"input":"https://example.com%80/","failure":true},{"input":"https://example.com%A0/","failure":true},{"input":"ftp://%e2%98%83"},{"input":"https://%e2%98%83"},{"input":"http://127.0.0.1:10100/relative_import.html"},{"input":"http://facebook.com/?foo=%7B%22abc%22"},{"input":"https://localhost:3000/jqueryui@1.2.3"},{"input":"h\tt\nt\rp://h\to\ns\rt:9\t0\n0\r0/p\ta\nt\rh?q\tu\ne\rry#f\tr\na\rg"},{"input":"http://foo.bar/baz?qux#foo\bbar"},{"input":"http://foo.bar/baz?qux#foo\"bar"},{"input":"http://foo.bar/baz?qux#foobar"},{"input":"http://foo.bar/baz?qux#foo`bar"},{"input":"https://0x.0x.0"},{"input":"https://0x100000000/test","failure":true},{"input":"https://256.0.0.1/test","failure":true},{"input":"file:///C%3A/"},{"input":"file:///C%7C/"},{"input":"file:\\\\//"},{"input":"file:\\\\\\\\"},{"input":"file:\\\\\\\\?fox"},{"input":"file:\\\\\\\\#guppy"},{"input":"file://spider///"},{"input":"file:\\\\localhost//"},{"input":"file:///localhost//cat"},{"input":"file://\\/localhost//cat"},{"input":"file://localhost//a//../..//"},{"input":"file://example.net/C:/"},{"input":"file://1.2.3.4/C:/"},{"input":"file://[1::8]/C:/"},{"input":"file:/C|/"},{"input":"file://C|/"},{"input":"file:"},{"input":"file:?q=v"},{"input":"file:#frag"},{"input":"https://[0::0::0]","failure":true},{"input":"https://[0:.0]","failure":true},{"input":"https://[0:0:]","failure":true},{"input":"https://[0:1:2:3:4:5:6:7.0.0.0.1]","failure":true},{"input":"https://[0:1.00.0.0.0]","failure":true},{"input":"https://[0:1.290.0.0.0]","failure":true},{"input":"https://[0:1.23.23]","failure":true},{"input":"http://?","failure":true},{"input":"http://#","failure":true},{"input":"sc://ñ"},{"input":"sc://ñ?x"},{"input":"sc://ñ#x"},{"input":"sc://?"},{"input":"sc://#"},{"input":"tftp://foobar.com/someconfig;mode=netascii"},{"input":"telnet://user:pass@foobar.com:23/"},{"input":"ut2004://10.10.10.10:7777/Index.ut2"},{"input":"redis://foo:bar@somehost:6379/0?baz=bam&qux=baz"},{"input":"rsync://foo@host:911/sup"},{"input":"git://github.com/foo/bar.git"},{"input":"irc://myserver.com:6999/channel?passwd"},{"input":"dns://fw.example.org:9999/foo.bar.org?type=TXT"},{"input":"ldap://localhost:389/ou=People,o=JNDITutorial"},{"input":"git+https://github.com/foo/bar"},{"input":"urn:ietf:rfc:2648"},{"input":"tag:joe@example.org,2001:foo/bar"},{"input":"non-special://%E2%80%A0/"},{"input":"non-special://H%4fSt/path"},{"input":"non-special://[1:2:0:0:5:0:0:0]/"},{"input":"non-special://[1:2:0:0:0:0:0:3]/"},{"input":"non-special://[1:2::3]:80/"},{"input":"non-special://[:80/","failure":true},{"input":"blob:https://example.com:443/"},{"input":"blob:d3958f5c-0777-0845-9dcf-2cb28783acaf"},{"input":"http://0177.0.0.0189"},{"input":"http://0x7f.0.0.0x7g"},{"input":"http://0X7F.0.0.0X7G"},{"input":"http://[::127.0.0.0.1]","failure":true},{"input":"http://[0:1:0:1:0:1:0:1]"},{"input":"http://[1:0:1:0:1:0:1:0]"},{"input":"http://example.org/test?\""},{"input":"http://example.org/test?#"},{"input":"http://example.org/test?<"},{"input":"http://example.org/test?>"},{"input":"http://example.org/test?⌣"},{"input":"http://example.org/test?%23%23"},{"input":"http://example.org/test?%GH"},{"input":"http://example.org/test?a#%EF"},{"input":"http://example.org/test?a#%GH"},{"input":"a","failure":true},{"input":"a/","failure":true},{"input":"a//","failure":true},{"input":"http://example.org/test?a#b\u0000c"}] \ No newline at end of file diff --git a/packages/url/src/test/index.native.js b/packages/url/src/test/index.native.js index 75247a7b9223c..b6286be7e5c3b 100644 --- a/packages/url/src/test/index.native.js +++ b/packages/url/src/test/index.native.js @@ -2,3 +2,26 @@ * Internal dependencies */ import './index.test'; + +jest.mock( './fixtures/wpt-data.json', () => { + const data = require.requireActual( './fixtures/wpt-data.json' ); + + /** + * Test items to exclude by input. Ideally this should be empty, but are + * necessary by non-spec-conformance of the Native implementations. + * Specifically, the React Native implementation uses an implementation of + * WHATWG URL without full Unicode support. + * + * @type {string[]} + */ + const URL_EXCEPTIONS = [ + 'https://�', + 'https://%EF%BF%BD', + 'ftp://example.com%80/', + 'ftp://example.com%A0/', + 'https://example.com%80/', + 'https://example.com%A0/', + ]; + + return data.filter( ( { input } ) => ! URL_EXCEPTIONS.includes( input ) ); +} ); diff --git a/packages/url/src/test/index.test.js b/packages/url/src/test/index.test.js index d14572627de14..8b25963dadcfd 100644 --- a/packages/url/src/test/index.test.js +++ b/packages/url/src/test/index.test.js @@ -28,36 +28,15 @@ import { filterURLForDisplay, cleanForSlug, } from '../'; +import wptData from './fixtures/wpt-data'; describe( 'isURL', () => { - it.each( [ - [ 'http://wordpress.org' ], - [ 'https://wordpress.org' ], - [ 'HTTPS://WORDPRESS.ORG' ], - [ 'https://wordpress.org/./foo' ], - [ 'https://wordpress.org/path?query#fragment' ], - [ 'https://localhost/foo#bar' ], - [ 'https:///localhost/foo#bar' ], - [ 'mailto:example@example.com' ], - [ 'ssh://user:password@127.0.0.1:8080' ], - [ 'file:///localfolder/file.mov' ], - [ 'file:/localfolder/file.mov' ], - ] )( 'valid (true): %s', ( url ) => { - expect( isURL( url ) ).toBe( true ); - } ); - - it.each( [ - [ 'http://word press.org' ], - [ 'http://wordpress.org:port' ], - [ 'http://[wordpress.org]/' ], - [ 'HTTP: HyperText Transfer Protocol' ], - [ 'URLs begin with a http:// prefix' ], - [ 'Go here: http://wordpress.org' ], - [ 'http://' ], - [ '' ], - ] )( 'invalid (false): %s', ( url ) => { - expect( isURL( url ) ).toBe( false ); - } ); + it.each( wptData.map( ( { input, failure } ) => [ input, !! failure ] ) )( + '%s', + ( input, isFailure ) => { + expect( isURL( input ) ).toBe( ! isFailure ); + } + ); } ); describe( 'isEmail', () => { diff --git a/tsconfig.json b/tsconfig.json index 6e74bf4506643..3e94a1db275d2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -38,7 +38,7 @@ "./packages/is-shallow-equal/**/*.js", "./packages/priority-queue/**/*.js", "./packages/token-list/**/*.js", - "./packages/url/**/*.js", + "./packages/url/src/**/*.js", "./packages/warning/**/*.js", ], "exclude": [