mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 15:53:27 +00:00
Add support for running unit tests (#381)
* Add test runner. * Fix tests. Add tests for UtilsService.getDomain * Test getHostname. * Add two missing test cases and fix getDomain.
This commit is contained in:
committed by
Kyle Spearrin
parent
f9b00c6871
commit
4531846ff8
54
src/services/utils.service.spec.ts
Normal file
54
src/services/utils.service.spec.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import UtilsService from './utils.service';
|
||||
|
||||
describe('Utils Service', () => {
|
||||
describe('getDomain', () => {
|
||||
it('should fail for invalid urls', () => {
|
||||
expect(UtilsService.getDomain(null)).toBeNull();
|
||||
expect(UtilsService.getDomain(undefined)).toBeNull();
|
||||
expect(UtilsService.getDomain(' ')).toBeNull();
|
||||
expect(UtilsService.getDomain('https://bit!:"_&ward.com')).toBeNull();
|
||||
expect(UtilsService.getDomain('bitwarden')).toBeNull();
|
||||
});
|
||||
|
||||
it('should handle urls without protocol', () => {
|
||||
expect(UtilsService.getDomain('bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getDomain('wrong://bitwarden.com')).toBe('bitwarden.com');
|
||||
});
|
||||
|
||||
it('should handle valid urls', () => {
|
||||
expect(UtilsService.getDomain('https://bitwarden')).toBe('bitwarden');
|
||||
expect(UtilsService.getDomain('https://bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getDomain('http://bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getDomain('http://vault.bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getDomain('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getDomain('https://bitwarden.unknown')).toBe('bitwarden.unknown');
|
||||
});
|
||||
|
||||
it('should support localhost and IP', () => {
|
||||
expect(UtilsService.getDomain('https://localhost')).toBe('localhost');
|
||||
expect(UtilsService.getDomain('https://192.168.1.1')).toBe('192.168.1.1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getHostname', () => {
|
||||
it('should fail for invalid urls', () => {
|
||||
expect(UtilsService.getHostname(null)).toBeNull();
|
||||
expect(UtilsService.getHostname(undefined)).toBeNull();
|
||||
expect(UtilsService.getHostname(' ')).toBeNull();
|
||||
expect(UtilsService.getHostname('https://bit!:"_&ward.com')).toBeNull();
|
||||
expect(UtilsService.getHostname('bitwarden')).toBeNull();
|
||||
});
|
||||
|
||||
it('should handle valid urls', () => {
|
||||
expect(UtilsService.getHostname('https://bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getHostname('http://bitwarden.com')).toBe('bitwarden.com');
|
||||
expect(UtilsService.getHostname('http://vault.bitwarden.com')).toBe('vault.bitwarden.com');
|
||||
expect(UtilsService.getHostname('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com');
|
||||
});
|
||||
|
||||
it('should support localhost and IP', () => {
|
||||
expect(UtilsService.getHostname('https://localhost')).toBe('localhost');
|
||||
expect(UtilsService.getHostname('https://192.168.1.1')).toBe('192.168.1.1');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import * as tldjs from 'tldjs';
|
||||
import { BrowserType } from '../enums/browserType.enum';
|
||||
import { UtilsService as UtilsServiceInterface } from './abstractions/utils.service';
|
||||
|
||||
@@ -186,28 +187,19 @@ export default class UtilsService implements UtilsServiceInterface {
|
||||
if (uriString.startsWith('http://') || uriString.startsWith('https://')) {
|
||||
try {
|
||||
const url = new URL(uriString);
|
||||
if (!url.hostname) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (url.hostname === 'localhost' || UtilsService.validIpAddress(url.hostname)) {
|
||||
return url.hostname;
|
||||
}
|
||||
|
||||
if (typeof tldjs !== 'undefined' && tldjs) {
|
||||
const domain = tldjs.getDomain(url.hostname);
|
||||
if (domain != null) {
|
||||
return domain;
|
||||
}
|
||||
}
|
||||
|
||||
return url.hostname;
|
||||
const domain = tldjs.getDomain(url.hostname);
|
||||
return domain ? domain : url.hostname;
|
||||
} catch (e) { }
|
||||
} else if (typeof tldjs !== 'undefined' && tldjs) {
|
||||
const domain = tldjs.getDomain(uriString);
|
||||
if (domain != null) {
|
||||
return domain;
|
||||
}
|
||||
}
|
||||
|
||||
const domain = tldjs.getDomain(uriString);
|
||||
if (domain != null) {
|
||||
return domain;
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -226,10 +218,6 @@ export default class UtilsService implements UtilsServiceInterface {
|
||||
if (uriString.startsWith('http://') || uriString.startsWith('https://')) {
|
||||
try {
|
||||
const url = new URL(uriString);
|
||||
if (!url.hostname) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return url.hostname;
|
||||
} catch (e) { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user