Files
twenty/packages/twenty-shared/src/utils/url/__tests__/isValidHostname.test.ts
T
Charles Bochet 9046a9ac16 Migrate url tooling to twenty-shared (#10440)
Migrate and unify URL tooling in twenty-shared.

We now have:
- isValidHostname which follows our own business rules
- a zod schema that can be re-used in different context and leverages is
isValidHostname
- isValidUrl on top of the zod schema
- a getAbsoluteURl and getHostname on top of the zod schema

I have added a LOT of tests to cover all the cases I've found

Also fixes: https://github.com/twentyhq/twenty/issues/10147
2025-02-24 18:01:51 +01:00

72 lines
2.2 KiB
TypeScript

import { isValidHostname } from 'src/utils/url/isValidHostname';
describe('isValidHostname', () => {
it(`should return true if string google`, () => {
expect(isValidHostname('google')).toBeFalsy();
});
it(`should return true if string google.com`, () => {
expect(isValidHostname('google.com')).toBeTruthy();
});
it(`should return true if string bbc.co.uk`, () => {
expect(isValidHostname('bbc.co.uk')).toBeTruthy();
});
it(`should return true if string www.subdomain.example.com`, () => {
expect(isValidHostname('www.subdomain.example.com')).toBeTruthy();
});
it(`should return true if string web.io`, () => {
expect(isValidHostname('web.io')).toBeTruthy();
});
it(`should return true if string x.com`, () => {
expect(isValidHostname('x.com')).toBeTruthy();
});
it(`should return true if string 2.com`, () => {
expect(isValidHostname('2.com')).toBeTruthy();
});
it(`should return true if string localhost`, () => {
expect(isValidHostname('localhost')).toBeTruthy();
});
it(`should return true if string 127.0.0.1`, () => {
expect(isValidHostname('127.0.0.1')).toBeTruthy();
});
it(`should return false if string 2`, () => {
expect(isValidHostname('2')).toBeFalsy();
});
it(`should return false if string contains non-valid characters`, () => {
expect(isValidHostname('subdomain.example.com/path')).toBeFalsy();
});
it(`should return false if string is empty`, () => {
expect(isValidHostname('')).toBeFalsy();
});
it(`should return false if string is one word`, () => {
expect(isValidHostname('subdomain')).toBeFalsy();
});
it(`should return true if string is ip address`, () => {
expect(isValidHostname('192.168.2.0')).toBeTruthy();
});
it(`should return false if string is ip address but allowIp is false`, () => {
expect(isValidHostname('192.168.2.0', { allowIp: false })).toBeFalsy();
});
it(`should return true if string is localhost but allowLocalhost is false`, () => {
expect(isValidHostname('localhost', { allowLocalhost: false })).toBeFalsy();
});
it(`should return true if string is localhost but allowLocalhost is true`, () => {
expect(isValidHostname('localhost', { allowLocalhost: true })).toBeTruthy();
});
});