Skip to content

Commit

Permalink
Introduce prependHTTPS url util (#47648)
Browse files Browse the repository at this point in the history
* Duplicate existing but for HTTPS

* Reuse existing implementation

* Rename and add docs for clarity

* Add additional test cases

Co-authored-by: Marin Atanasov <8436925+tyxla@users.noreply.github.com>

* Alphabetise imports

---------

Co-authored-by: Marin Atanasov <8436925+tyxla@users.noreply.github.com>
  • Loading branch information
getdave and tyxla authored Mar 3, 2023
1 parent 943f369 commit 888aecb
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 18 deletions.
20 changes: 20 additions & 0 deletions packages/url/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,26 @@ _Returns_

- `string`: The updated URL.

### prependHTTPS

Prepends "https\://" to a url, if it looks like something that is meant to be a TLD.

Note: this will not replace "http\://" with "<https://">.

_Usage_

```js
const actualURL = prependHTTPS( 'wordpress.org' ); // https://wordpress.org
```

_Parameters_

- _url_ `string`: The URL to test.

_Returns_

- `string`: The updated URL.

### removeQueryArgs

Removes arguments from the query string of the url
Expand Down
1 change: 1 addition & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export { filterURLForDisplay } from './filter-url-for-display';
export { cleanForSlug } from './clean-for-slug';
export { getFilename } from './get-filename';
export { normalizePath } from './normalize-path';
export { prependHTTPS } from './prepend-https';
33 changes: 33 additions & 0 deletions packages/url/src/prepend-https.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Internal dependencies
*/
import { prependHTTP } from './prepend-http';

/**
* Prepends "https://" to a url, if it looks like something that is meant to be a TLD.
*
* Note: this will not replace "http://" with "https://".
*
* @param {string} url The URL to test.
*
* @example
* ```js
* const actualURL = prependHTTPS( 'wordpress.org' ); // https://wordpress.org
* ```
*
* @return {string} The updated URL.
*/
export function prependHTTPS( url ) {
if ( ! url ) {
return url;
}

// If url starts with http://, return it as is.
if ( url.startsWith( 'http://' ) ) {
return url;
}

url = prependHTTP( url );

return url.replace( /^http:/, 'https:' );
}
123 changes: 105 additions & 18 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@
* Internal dependencies
*/
import {
isURL,
isEmail,
getProtocol,
isValidProtocol,
getAuthority,
isValidAuthority,
getPath,
isValidPath,
getQueryString,
addQueryArgs,
buildQueryString,
isValidQueryString,
cleanForSlug,
filterURLForDisplay,
getAuthority,
getFilename,
getFragment,
isValidFragment,
addQueryArgs,
getPath,
getProtocol,
getQueryArg,
getQueryArgs,
getQueryString,
hasQueryArg,
removeQueryArgs,
isEmail,
isURL,
isValidAuthority,
isValidFragment,
isValidPath,
isValidProtocol,
isValidQueryString,
normalizePath,
prependHTTP,
prependHTTPS,
removeQueryArgs,
safeDecodeURI,
filterURLForDisplay,
cleanForSlug,
getQueryArgs,
getFilename,
normalizePath,
} from '../';
import wptData from './fixtures/wpt-data';

Expand Down Expand Up @@ -890,6 +891,92 @@ describe( 'prependHTTP', () => {
} );
} );

describe( 'prependHTTPS', () => {
it( 'should prepend https to a domain', () => {
const url = 'wordpress.org';

expect( prependHTTPS( url ) ).toBe( 'https://' + url );
} );

it( 'should not prepend https to an email', () => {
const url = 'foo@wordpress.org';

expect( prependHTTPS( url ) ).toBe( url );
} );

it( 'should not prepend https to an absolute URL', () => {
const url = '/wordpress';

expect( prependHTTPS( url ) ).toBe( url );
} );

it( 'should not prepend https to a relative URL', () => {
const url = './wordpress';

expect( prependHTTPS( url ) ).toBe( url );
} );

it( 'should not prepend https to an anchor URL', () => {
const url = '#wordpress';

expect( prependHTTPS( url ) ).toBe( url );
} );

it( 'should not prepend https to a URL that already has https', () => {
const url = 'https://wordpress.org';

expect( prependHTTPS( url ) ).toBe( url );
} );

it( 'should not prepend https to a URL that already has http', () => {
const url = 'http://wordpress.org';

expect( prependHTTPS( url ) ).toBe( url );
} );

it( 'should not prepend https to a URL that already has ftp', () => {
const url = 'ftp://wordpress.org';

expect( prependHTTPS( url ) ).toBe( url );
} );

it( 'should not prepend https to a URL that already has mailto', () => {
const url = 'mailto:foo@wordpress.org';

expect( prependHTTPS( url ) ).toBe( url );
} );

it( 'should remove leading whitespace before prepending HTTPs', () => {
const url = ' wordpress.org';

expect( prependHTTPS( url ) ).toBe( 'https://wordpress.org' );
} );

it( 'should not have trailing whitespaces', () => {
const url = 'wordpress.org ';

expect( prependHTTPS( url ) ).toBe( 'https://wordpress.org' );
} );
} );

it( 'should prepend https to a domain with an anchor', () => {
const url = 'wordpress.org#something';

expect( prependHTTPS( url ) ).toBe( 'https://' + url );
} );

it( 'should prepend https to a domain with path', () => {
const url = 'wordpress.org/some/thing';

expect( prependHTTPS( url ) ).toBe( 'https://' + url );
} );

it( 'should prepend https to a domain with query arguments', () => {
const url = 'wordpress.org?foo=bar';

expect( prependHTTPS( url ) ).toBe( 'https://' + url );
} );

describe( 'safeDecodeURI', () => {
it( 'should decode URI if formed well', () => {
const encoded = 'https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B';
Expand Down

1 comment on commit 888aecb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 888aecb.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4326003564
📝 Reported issues:

Please sign in to comment.