ensureSecureProcotol

This commit is contained in:
Reinaldo Neto 2023-03-06 19:47:07 -03:00
parent fb47f8221f
commit 75db91b5b8
3 changed files with 10 additions and 10 deletions

View File

@ -1,20 +1,20 @@
import addProtocol from './addProtocol'; import ensureSecureProtocol from './ensureSecureProtocol';
describe('Add the protocol https at the begin of the URL', () => { describe('Add the protocol https at the begin of the URL', () => {
it('return the link as original when sent with https at the begin', () => { it('return the link as original when sent with https at the begin', () => {
const linkHttps = 'https://www.google.com'; const linkHttps = 'https://www.google.com';
expect(addProtocol(linkHttps)).toBe(linkHttps); expect(ensureSecureProtocol(linkHttps)).toBe(linkHttps);
}); });
it('return the link as original when sent with http at the begin', () => { it('return the link as original when sent with http at the begin', () => {
const linkHttp = 'http://www.google.com'; const linkHttp = 'http://www.google.com';
expect(addProtocol(linkHttp)).toBe(linkHttp); expect(ensureSecureProtocol(linkHttp)).toBe(linkHttp);
}); });
it("return a new link with protocol at the begin when there isn't the protocol at the begin", () => { it("return a new link with protocol at the begin when there isn't the protocol at the begin", () => {
const linkWithoutProtocol = 'www.google.com'; const linkWithoutProtocol = 'www.google.com';
expect(addProtocol(linkWithoutProtocol)).toBe('https://www.google.com'); expect(ensureSecureProtocol(linkWithoutProtocol)).toBe('https://www.google.com');
}); });
it('return the link correctly when the original starts with double slash, because the server is returning that', () => { it('return the link correctly when the original starts with double slash, because the server is returning that', () => {
const linkWithDoubleSlashAtBegin = '//www.google.com'; const linkWithDoubleSlashAtBegin = '//www.google.com';
expect(addProtocol(linkWithDoubleSlashAtBegin)).toBe('https://www.google.com'); expect(ensureSecureProtocol(linkWithDoubleSlashAtBegin)).toBe('https://www.google.com');
}); });
}); });

View File

@ -1,10 +1,10 @@
// If the link does not have the protocol at the beginning, we are inserting https as the default, // If the link does not have the protocol at the beginning, we are inserting https as the default,
// since by conversion the most used is the secure protocol, with the same behavior as the web. // since by convention the most used is the secure protocol, with the same behavior as the web.
const addProtocol = (url: string): string => { const ensureSecureProtocol = (url: string): string => {
if (!url.toLowerCase().startsWith('http')) { if (!url.toLowerCase().startsWith('http')) {
return `https://${url.replace('//', '')}`; return `https://${url.replace('//', '')}`;
} }
return url; return url;
}; };
export default addProtocol; export default ensureSecureProtocol;

View File

@ -5,7 +5,7 @@ import parse from 'url-parse';
import { themes } from '../../constants'; import { themes } from '../../constants';
import { TSupportedThemes } from '../../../theme'; import { TSupportedThemes } from '../../../theme';
import UserPreferences from '../userPreferences'; import UserPreferences from '../userPreferences';
import addProtocol from './addProtocol'; import ensureSecureProtocol from './ensureSecureProtocol';
export const DEFAULT_BROWSER_KEY = 'DEFAULT_BROWSER_KEY'; export const DEFAULT_BROWSER_KEY = 'DEFAULT_BROWSER_KEY';
@ -38,7 +38,7 @@ const appSchemeURL = (url: string, browser: string): string => {
}; };
const openLink = async (url: string, theme: TSupportedThemes = 'light'): Promise<void> => { const openLink = async (url: string, theme: TSupportedThemes = 'light'): Promise<void> => {
url = addProtocol(url); url = ensureSecureProtocol(url);
try { try {
const browser = UserPreferences.getString(DEFAULT_BROWSER_KEY); const browser = UserPreferences.getString(DEFAULT_BROWSER_KEY);
if (browser === 'inApp') { if (browser === 'inApp') {