1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-03 18:50:05 +00:00

test(isUrl): covered isUrl utility with unit tests

This commit is contained in:
Yury Shkoda
2021-04-22 14:45:56 +03:00
parent 48442f7769
commit 9d6882799d
2 changed files with 46 additions and 3 deletions

View File

@@ -0,0 +1,39 @@
import { isUrl } from '../../utils/isUrl'
describe('urlValidator', () => {
it('should return true with an HTTP URL', () => {
const url = 'http://google.com'
expect(isUrl(url)).toEqual(true)
})
it('should return true with an HTTPS URL', () => {
const url = 'https://google.com'
expect(isUrl(url)).toEqual(true)
})
it('should return true when the URL is blank', () => {
const url = ''
expect(isUrl(url)).toEqual(false)
})
it('should return false when the URL has not supported protocol', () => {
const url = 'htpps://google.com'
expect(isUrl(url)).toEqual(false)
})
it('should return false when the URL is null', () => {
const url = null
expect(isUrl((url as unknown) as string)).toEqual(false)
})
it('should return false when the URL is undefined', () => {
const url = undefined
expect(isUrl((url as unknown) as string)).toEqual(false)
})
})

View File

@@ -1,10 +1,14 @@
/**
* Checks if string is in URL format.
* @param url - string to check.
* @param str - string to check.
*/
export const isUrl = (url: string): boolean => {
export const isUrl = (str: string): boolean => {
const supportedProtocols = ['http:', 'https:']
try {
const validUrl = new URL(url)
const url = new URL(str)
if (!supportedProtocols.includes(url.protocol)) return false
} catch (_) {
return false
}