1
0
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:
Oscar Hinton
2017-11-17 22:33:58 +01:00
committed by Kyle Spearrin
parent f9b00c6871
commit 4531846ff8
6 changed files with 164 additions and 22 deletions

View 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');
});
});
});

View File

@@ -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) { }
}