test_background.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const assert = require('assert');
  2. const tester = require('../src/background/utils/tester');
  3. describe('tester', () => {
  4. describe('scheme', () => {
  5. it('should match all', () => {
  6. const script = {
  7. custom: {},
  8. meta: {
  9. match: [
  10. '*://*/*',
  11. ],
  12. }
  13. };
  14. assert.ok(tester.testScript('https://www.google.com/', script), 'should match `http | https`');
  15. assert.ok(!tester.testScript('file:///Users/Gerald/file', script), 'should not match `file`');
  16. });
  17. it('should match exact', () => {
  18. const script = {
  19. custom: {},
  20. meta: {
  21. match: [
  22. 'http://*/*',
  23. 'ftp://*/*',
  24. 'file:///*',
  25. ],
  26. },
  27. };
  28. assert.ok(tester.testScript('http://www.google.com/', script), 'should match `http`');
  29. assert.ok(!tester.testScript('https://www.google.com/', script), 'should not match `https`');
  30. assert.ok(tester.testScript('file:///Users/Gerald/file', script), 'should match `file`');
  31. assert.ok(tester.testScript('ftp://example.com/file', script), 'should match `ftp`');
  32. });
  33. });
  34. describe('host', () => {
  35. it('should match domain', () => {
  36. const script = {
  37. custom: {},
  38. meta: {
  39. match: [
  40. '*://docs.google.com/',
  41. ],
  42. },
  43. };
  44. assert.ok(tester.testScript('https://docs.google.com/', script), 'should match exact domain name');
  45. assert.ok(!tester.testScript('https://sub.docs.google.com/', script), 'should not match subdomains');
  46. assert.ok(!tester.testScript('https://docs.google.com.cn/', script), 'should not match suffixed domains');
  47. });
  48. it('should match subdomains', () => {
  49. const script = {
  50. custom: {},
  51. meta: {
  52. match: [
  53. '*://*.google.com/',
  54. ],
  55. },
  56. };
  57. assert.ok(tester.testScript('https://www.google.com/', script), 'should match subdomains');
  58. assert.ok(tester.testScript('https://a.b.google.com/', script), 'should match subdomains');
  59. assert.ok(tester.testScript('https://google.com/', script), 'should match specified domain');
  60. assert.ok(!tester.testScript('https://www.google.com.hk/', script), 'should not match suffixed domains');
  61. });
  62. });
  63. describe('path', () => {
  64. it('should match any', () => {
  65. const script = {
  66. custom: {},
  67. meta: {
  68. match: [
  69. 'https://www.google.com/*',
  70. ],
  71. },
  72. };
  73. assert.ok(tester.testScript('https://www.google.com/', script), 'should match `/`');
  74. assert.ok(tester.testScript('https://www.google.com/hello/world', script), 'should match any');
  75. });
  76. it('should match exact', () => {
  77. const script = {
  78. custom: {},
  79. meta: {
  80. match: [
  81. 'https://www.google.com/a/b/c',
  82. ],
  83. },
  84. };
  85. assert.ok(tester.testScript('https://www.google.com/a/b/c', script), 'should match exact');
  86. assert.ok(!tester.testScript('https://www.google.com/a/b/c/d', script), 'should match exact');
  87. });
  88. });
  89. });