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 3.8 KiB

8 年之前
8 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. });
  29. test('ip validator', t => {
  30. t.true(_ipIsValid('192.168.23.215'));
  31. t.true(_ipIsValid('127.0.0.1'));
  32. t.false(_ipIsValid('210.110'), 'must have 4 octets');
  33. t.false(_ipIsValid('255'), 'must have 4 octets');
  34. t.false(_ipIsValid('y.y.y.y'), 'only digits are allowed');
  35. t.false(_ipIsValid('255.0.0.y'), 'only digits are allowed');
  36. t.false(_ipIsValid('666.10.10.20'), 'octet number must be between [0-255]');
  37. t.false(_ipIsValid('4444.11.11.11'), 'octet number must be between [0-255]');
  38. t.false(_ipIsValid('33.3333.33.3'), 'octet number must be between [0-255]');
  39. });
  40. test('get web extension context', t => {
  41. global.chrome = undefined;
  42. t.false(isWebExtension())
  43. });
  44. test('get web extension context', t => {
  45. global.chrome = {
  46. tabs: {
  47. query(a, b){
  48. console.log(a, b)
  49. }
  50. }
  51. };
  52. t.true(isWebExtension())
  53. });
  54. test('get current tab', t => {
  55. const url = 'example.org';
  56. global.chrome = {
  57. tabs: {
  58. query(a, callback){
  59. callback([{url}])
  60. }
  61. }
  62. };
  63. return getCurrentUrl().then(currentUrl => {
  64. t.is(currentUrl, url)
  65. });
  66. });
  67. test('get default site', t => {
  68. global.chrome = {
  69. tabs: {
  70. query(a, callback){
  71. callback([{url: 'https://example.org'}])
  72. }
  73. }
  74. };
  75. return getSite().then(site => {
  76. t.is(site, 'example.org')
  77. });
  78. });
  79. test('get default site if not in web extension', t => {
  80. global.chrome = undefined;
  81. return getSite().then(site => {
  82. t.is(site, '')
  83. });
  84. });