You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

url-parser.js 2.1 KiB

8 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import test from 'ava';
  2. import {getDomainName, _ipIsValid, isWebExtension, getCurrentUrl, getSite} from '../src/domain/url-parser';
  3. test(t => {
  4. t.is('lesspass.com', getDomainName('https://lesspass.com/#!/'));
  5. t.is('lesspass.com', getDomainName('https://lesspass.com/api/'));
  6. t.is('api.lesspass.com', getDomainName('https://api.lesspass.com/'));
  7. t.is('lesspass.com', getDomainName('http://lesspass.com'));
  8. t.is('192.168.1.1:10443', getDomainName('https://192.168.1.1:10443/webapp/'));
  9. });
  10. test('ip validator', t => {
  11. t.true(_ipIsValid('192.168.23.215'));
  12. t.true(_ipIsValid('127.0.0.1'));
  13. t.false(_ipIsValid('210.110'), 'must have 4 octets');
  14. t.false(_ipIsValid('255'), 'must have 4 octets');
  15. t.false(_ipIsValid('y.y.y.y'), 'only digits are allowed');
  16. t.false(_ipIsValid('255.0.0.y'), 'only digits are allowed');
  17. t.false(_ipIsValid('666.10.10.20'), 'octet number must be between [0-255]');
  18. t.false(_ipIsValid('4444.11.11.11'), 'octet number must be between [0-255]');
  19. t.false(_ipIsValid('33.3333.33.3'), 'octet number must be between [0-255]');
  20. });
  21. test('get web extension context', t => {
  22. global.chrome = undefined;
  23. t.false(isWebExtension())
  24. });
  25. test('get web extension context', t => {
  26. global.chrome = {
  27. tabs: {
  28. query(a, b){
  29. console.log(a, b)
  30. }
  31. }
  32. };
  33. t.true(isWebExtension())
  34. });
  35. test('get current tab', t => {
  36. const url = 'example.org';
  37. global.chrome = {
  38. tabs: {
  39. query(a, callback){
  40. callback([{url}])
  41. }
  42. }
  43. };
  44. return getCurrentUrl().then(currentUrl => {
  45. t.is(currentUrl, url)
  46. });
  47. });
  48. test('get default site', t => {
  49. global.chrome = {
  50. tabs: {
  51. query(a, callback){
  52. callback([{url: 'https://example.org'}])
  53. }
  54. }
  55. };
  56. return getSite().then(site => {
  57. t.is(site, 'example.org')
  58. });
  59. });
  60. test('get default site if not in web extension', t => {
  61. global.chrome = undefined;
  62. return getSite().then(site => {
  63. t.is(site, '')
  64. });
  65. });