tester.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import test from 'tape';
  2. import { testScript, testBlacklist, resetBlacklist } from 'src/background/utils/tester';
  3. import cache from 'src/background/utils/cache';
  4. test.onFinish(cache.destroy);
  5. function buildScript(props) {
  6. return Object.assign({
  7. custom: {
  8. origInclude: true,
  9. origExclude: true,
  10. origMatch: true,
  11. origExcludeMatch: true,
  12. },
  13. meta: {},
  14. }, props);
  15. }
  16. test('scheme', t => {
  17. t.test('should match all', q => {
  18. const script = buildScript({
  19. meta: {
  20. match: [
  21. '*://*/*',
  22. ],
  23. },
  24. });
  25. q.ok(testScript('https://www.google.com/', script), 'should match `http | https`');
  26. q.notOk(testScript('file:///Users/Gerald/file', script), 'should not match `file`');
  27. q.end();
  28. });
  29. t.test('should match exact', q => {
  30. const script = buildScript({
  31. meta: {
  32. match: [
  33. 'http://*/*',
  34. 'ftp://*/*',
  35. 'file:///*',
  36. ],
  37. },
  38. });
  39. q.ok(testScript('http://www.google.com/', script), 'should match `http`');
  40. q.notOk(testScript('https://www.google.com/', script), 'should not match `https`');
  41. q.ok(testScript('file:///Users/Gerald/file', script), 'should match `file`');
  42. q.ok(testScript('ftp://example.com/file', script), 'should match `ftp`');
  43. q.end();
  44. });
  45. t.end();
  46. });
  47. test('host', t => {
  48. t.test('should match domain', q => {
  49. const script = buildScript({
  50. meta: {
  51. match: [
  52. '*://docs.google.com/',
  53. ],
  54. },
  55. });
  56. q.ok(testScript('https://docs.google.com/', script), 'should match exact domain name');
  57. q.notOk(testScript('https://sub.docs.google.com/', script), 'should not match subdomains');
  58. q.notOk(testScript('https://docs.google.com.cn/', script), 'should not match suffixed domains');
  59. q.end();
  60. });
  61. t.test('should match subdomains', q => {
  62. const script = buildScript({
  63. meta: {
  64. match: [
  65. '*://*.google.com/',
  66. ],
  67. },
  68. });
  69. q.ok(testScript('https://www.google.com/', script), 'should match subdomains');
  70. q.ok(testScript('https://a.b.google.com/', script), 'should match subdomains');
  71. q.ok(testScript('https://google.com/', script), 'should match specified domain');
  72. q.notOk(testScript('https://www.google.com.hk/', script), 'should not match suffixed domains');
  73. q.end();
  74. });
  75. t.end();
  76. });
  77. test('path', t => {
  78. t.test('should match any', q => {
  79. const script = buildScript({
  80. meta: {
  81. match: [
  82. 'https://www.google.com/*',
  83. ],
  84. },
  85. });
  86. q.ok(testScript('https://www.google.com/', script), 'should match `/`');
  87. q.ok(testScript('https://www.google.com/hello/world', script), 'should match any');
  88. q.end();
  89. });
  90. t.test('should match exact', q => {
  91. const script = buildScript({
  92. meta: {
  93. match: [
  94. 'https://www.google.com/a/b/c',
  95. ],
  96. },
  97. });
  98. q.ok(testScript('https://www.google.com/a/b/c', script), 'should match exact');
  99. q.notOk(testScript('https://www.google.com/a/b/c/d', script), 'should match exact');
  100. q.end();
  101. });
  102. t.end();
  103. });
  104. test('include', t => {
  105. t.test('should include any', q => {
  106. const script = buildScript({
  107. meta: {
  108. include: [
  109. '*',
  110. ],
  111. },
  112. });
  113. q.ok(testScript('https://www.google.com/', script), 'should match `http | https`');
  114. q.ok(testScript('file:///Users/Gerald/file', script), 'should match `file`');
  115. q.end();
  116. });
  117. t.test('should include by regexp', q => {
  118. const script = buildScript({
  119. meta: {
  120. include: [
  121. 'https://www.google.com/*',
  122. 'https://twitter.com/*',
  123. ],
  124. },
  125. });
  126. q.ok(testScript('https://www.google.com/', script), 'should match `/`');
  127. q.ok(testScript('https://www.google.com/hello/world', script), 'include by prefix');
  128. q.notOk(testScript('https://www.hello.com/', script), 'not include by prefix');
  129. q.end();
  130. });
  131. });
  132. test('exclude', t => {
  133. t.test('should exclude any', q => {
  134. const script = buildScript({
  135. meta: {
  136. match: [
  137. '*://*/*',
  138. ],
  139. exclude: [
  140. '*',
  141. ],
  142. },
  143. });
  144. q.notOk(testScript('https://www.google.com/', script), 'should exclude `http | https`');
  145. q.end();
  146. });
  147. t.test('should include by regexp', q => {
  148. const script = buildScript({
  149. meta: {
  150. match: [
  151. '*://*/*',
  152. ],
  153. excludeMatch: [
  154. 'https://www.google.com/*',
  155. 'https://twitter.com/*',
  156. ],
  157. },
  158. });
  159. q.notOk(testScript('https://www.google.com/', script), 'should exclude `/`');
  160. q.notOk(testScript('https://www.google.com/hello/world', script), 'exclude by prefix');
  161. q.ok(testScript('https://www.hello.com/', script), 'not exclude by prefix');
  162. q.end();
  163. });
  164. });
  165. test('exclude-match', t => {
  166. t.test('should exclude any', q => {
  167. const script = buildScript({
  168. meta: {
  169. match: [
  170. '*://*/*',
  171. ],
  172. excludeMatch: [
  173. '*://*/*',
  174. ],
  175. },
  176. });
  177. q.notOk(testScript('https://www.google.com/', script), 'should exclude `http | https`');
  178. q.end();
  179. });
  180. t.test('should include by regexp', q => {
  181. const script = buildScript({
  182. meta: {
  183. match: [
  184. '*://*/*',
  185. ],
  186. excludeMatch: [
  187. 'https://www.google.com/*',
  188. 'https://twitter.com/*',
  189. ],
  190. },
  191. });
  192. q.notOk(testScript('https://www.google.com/', script), 'should exclude `/`');
  193. q.notOk(testScript('https://www.google.com/hello/world', script), 'exclude by prefix');
  194. q.ok(testScript('https://www.hello.com/', script), 'not exclude by prefix');
  195. q.end();
  196. });
  197. });
  198. test('custom', t => {
  199. t.test('should ignore original rules', q => {
  200. const script = buildScript({
  201. custom: {
  202. match: [
  203. 'https://twitter.com/*',
  204. ],
  205. },
  206. meta: {
  207. match: [
  208. 'https://www.google.com/*',
  209. ],
  210. },
  211. });
  212. q.ok(testScript('https://twitter.com/', script), 'should match custom rules');
  213. q.notOk(testScript('https://www.google.com/', script), 'should not match original rules');
  214. q.end();
  215. });
  216. });
  217. test('blacklist', t => {
  218. t.test('should exclude match rules', q => {
  219. resetBlacklist(`\
  220. # match rules
  221. *://www.google.com/*
  222. `);
  223. q.ok(testBlacklist('http://www.google.com/'));
  224. q.ok(testBlacklist('https://www.google.com/'));
  225. q.notOk(testBlacklist('https://twitter.com/'));
  226. q.end();
  227. });
  228. t.test('should exclude domains', q => {
  229. resetBlacklist(`\
  230. # domains
  231. www.google.com
  232. `);
  233. q.ok(testBlacklist('http://www.google.com/'));
  234. q.ok(testBlacklist('https://www.google.com/'));
  235. q.notOk(testBlacklist('https://twitter.com/'));
  236. q.end();
  237. });
  238. t.test('should support @exclude rules', q => {
  239. resetBlacklist(`\
  240. # @exclude rules
  241. @exclude https://www.google.com/*
  242. `);
  243. q.ok(testBlacklist('https://www.google.com/'));
  244. q.ok(testBlacklist('https://www.google.com/whatever'));
  245. q.notOk(testBlacklist('http://www.google.com/'));
  246. q.end();
  247. });
  248. });