Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

url-parser.js 3.9 KiB

8 år sedan
8 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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('stackoverflow.com', getDomainName('http://stackoverflow.com/questions/3689423/google-chrome-plugin-how-to-get-domain-from-url-tab-url'));
  9. t.is('v4-alpha.getbootstrap.com', getDomainName('http://v4-alpha.getbootstrap.com/components/buttons/'));
  10. t.is('accounts.google.com', getDomainName('https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier'));
  11. t.is('www.netflix.com', getDomainName('https://www.netflix.com/browse'));
  12. t.is('www.bbc.co.uk', getDomainName('https://www.bbc.co.uk'));
  13. t.is('192.168.1.1:10443', getDomainName('https://192.168.1.1:10443/webapp/'));
  14. });
  15. test('getDomainName v2', t => {
  16. t.is('lesspass.com', getDomainName('https://lesspass.com/#!/', 2));
  17. t.is('lesspass.com', getDomainName('https://lesspass.com/api/', 2));
  18. t.is('lesspass.com', getDomainName('https://api.lesspass.com/', 2));
  19. t.is('lesspass.com', getDomainName('http://lesspass.com', 2));
  20. t.is('stackoverflow.com', getDomainName('http://stackoverflow.com/questions/3689423/google-chrome-plugin-how-to-get-domain-from-url-tab-url', 2));
  21. t.is('getbootstrap.com', getDomainName('http://v4-alpha.getbootstrap.com/components/buttons/', 2));
  22. t.is('google.com', getDomainName('https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier', 2));
  23. t.is('netflix.com', getDomainName('https://www.netflix.com/browse', 2));
  24. t.is('bbc.co.uk', getDomainName('https://www.bbc.co.uk', 2));
  25. t.is('192.168.1.1:10443', getDomainName('https://192.168.1.1:10443/webapp/', 2));
  26. t.is('192.168.1.1', getDomainName('http://192.168.1.1', 2));
  27. t.is('192.168.1.1', getDomainName('https://192.168.1.1/', 2));
  28. t.is('', getDomainName(undefined, 2));
  29. });
  30. test('ip validator', t => {
  31. t.true(_ipIsValid('192.168.23.215'));
  32. t.true(_ipIsValid('127.0.0.1'));
  33. t.false(_ipIsValid('210.110'), 'must have 4 octets');
  34. t.false(_ipIsValid('255'), 'must have 4 octets');
  35. t.false(_ipIsValid('y.y.y.y'), 'only digits are allowed');
  36. t.false(_ipIsValid('255.0.0.y'), 'only digits are allowed');
  37. t.false(_ipIsValid('666.10.10.20'), 'octet number must be between [0-255]');
  38. t.false(_ipIsValid('4444.11.11.11'), 'octet number must be between [0-255]');
  39. t.false(_ipIsValid('33.3333.33.3'), 'octet number must be between [0-255]');
  40. });
  41. test('get web extension context', t => {
  42. global.chrome = undefined;
  43. t.false(isWebExtension())
  44. });
  45. test('get web extension context', t => {
  46. global.chrome = {
  47. tabs: {
  48. query(a, b){
  49. console.log(a, b)
  50. }
  51. }
  52. };
  53. t.true(isWebExtension())
  54. });
  55. test('get current tab', t => {
  56. const url = 'example.org';
  57. global.chrome = {
  58. tabs: {
  59. query(a, callback){
  60. callback([{url}])
  61. }
  62. }
  63. };
  64. return getCurrentUrl().then(currentUrl => {
  65. t.is(currentUrl, url)
  66. });
  67. });
  68. test('get default site', t => {
  69. global.chrome = {
  70. tabs: {
  71. query(a, callback){
  72. callback([{url: 'https://example.org'}])
  73. }
  74. }
  75. };
  76. return getSite().then(site => {
  77. t.is(site, 'example.org')
  78. });
  79. });
  80. test('get default site if not in web extension', t => {
  81. global.chrome = undefined;
  82. return getSite().then(site => {
  83. t.is(site, '')
  84. });
  85. });