tester.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. t.test('should support magic TLD', q => {
  132. const script = buildScript({
  133. meta: {
  134. include: [
  135. 'https://www.google.tld/*',
  136. ],
  137. },
  138. });
  139. q.ok(testScript('https://www.google.com/', script), 'should match `.com`');
  140. q.ok(testScript('https://www.google.com.hk/', script), 'should match `.com.hk`');
  141. q.notOk(testScript('https://www.google.example.com/', script), 'should not match subdomains');
  142. q.end();
  143. });
  144. });
  145. test('exclude', t => {
  146. t.test('should exclude any', q => {
  147. const script = buildScript({
  148. meta: {
  149. match: [
  150. '*://*/*',
  151. ],
  152. exclude: [
  153. '*',
  154. ],
  155. },
  156. });
  157. q.notOk(testScript('https://www.google.com/', script), 'should exclude `http | https`');
  158. q.end();
  159. });
  160. t.test('should include by regexp', q => {
  161. const script = buildScript({
  162. meta: {
  163. match: [
  164. '*://*/*',
  165. ],
  166. excludeMatch: [
  167. 'https://www.google.com/*',
  168. 'https://twitter.com/*',
  169. ],
  170. },
  171. });
  172. q.notOk(testScript('https://www.google.com/', script), 'should exclude `/`');
  173. q.notOk(testScript('https://www.google.com/hello/world', script), 'exclude by prefix');
  174. q.ok(testScript('https://www.hello.com/', script), 'not exclude by prefix');
  175. q.end();
  176. });
  177. t.test('should support magic TLD', q => {
  178. const script = buildScript({
  179. meta: {
  180. exclude: [
  181. 'https://www.google.tld/*',
  182. ],
  183. },
  184. });
  185. q.notOk(testScript('https://www.google.com/', script), 'should match `.com`');
  186. q.notOk(testScript('https://www.google.com.hk/', script), 'should match `.com.hk`');
  187. q.ok(testScript('https://www.google.example.com/', script), 'should not match subdomains');
  188. q.end();
  189. });
  190. });
  191. test('exclude-match', t => {
  192. t.test('should exclude any', q => {
  193. const script = buildScript({
  194. meta: {
  195. match: [
  196. '*://*/*',
  197. ],
  198. excludeMatch: [
  199. '*://*/*',
  200. ],
  201. },
  202. });
  203. q.notOk(testScript('https://www.google.com/', script), 'should exclude `http | https`');
  204. q.end();
  205. });
  206. t.test('should include by regexp', q => {
  207. const script = buildScript({
  208. meta: {
  209. match: [
  210. '*://*/*',
  211. ],
  212. excludeMatch: [
  213. 'https://www.google.com/*',
  214. 'https://twitter.com/*',
  215. ],
  216. },
  217. });
  218. q.notOk(testScript('https://www.google.com/', script), 'should exclude `/`');
  219. q.notOk(testScript('https://www.google.com/hello/world', script), 'exclude by prefix');
  220. q.ok(testScript('https://www.hello.com/', script), 'not exclude by prefix');
  221. q.end();
  222. });
  223. });
  224. test('custom', t => {
  225. t.test('should ignore original rules', q => {
  226. const script = buildScript({
  227. custom: {
  228. match: [
  229. 'https://twitter.com/*',
  230. ],
  231. },
  232. meta: {
  233. match: [
  234. 'https://www.google.com/*',
  235. ],
  236. },
  237. });
  238. q.ok(testScript('https://twitter.com/', script), 'should match custom rules');
  239. q.notOk(testScript('https://www.google.com/', script), 'should not match original rules');
  240. q.end();
  241. });
  242. });
  243. test('blacklist', t => {
  244. t.test('should exclude match rules', q => {
  245. resetBlacklist(`\
  246. # match rules
  247. *://www.google.com/*
  248. `);
  249. q.ok(testBlacklist('http://www.google.com/'));
  250. q.ok(testBlacklist('https://www.google.com/'));
  251. q.notOk(testBlacklist('https://twitter.com/'));
  252. q.end();
  253. });
  254. t.test('should exclude domains', q => {
  255. resetBlacklist(`\
  256. # domains
  257. www.google.com
  258. `);
  259. q.ok(testBlacklist('http://www.google.com/'));
  260. q.ok(testBlacklist('https://www.google.com/'));
  261. q.notOk(testBlacklist('https://twitter.com/'));
  262. q.end();
  263. });
  264. t.test('should support @exclude rules', q => {
  265. resetBlacklist(`\
  266. # @exclude rules
  267. @exclude https://www.google.com/*
  268. `);
  269. q.ok(testBlacklist('https://www.google.com/'));
  270. q.ok(testBlacklist('https://www.google.com/whatever'));
  271. q.notOk(testBlacklist('http://www.google.com/'));
  272. q.end();
  273. });
  274. });