index.test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. const assert = require('power-assert');
  3. const match = require('..');
  4. describe('egg-path-matching', () => {
  5. it('options.match and options.ignore both present should throw', () => {
  6. try {
  7. match({ ignore: '/api', match: '/dashboard' });
  8. throw new Error('should not exec');
  9. } catch (e) {
  10. assert(e.message === 'options.match and options.ignore can not both present');
  11. }
  12. });
  13. it('options.match and options.ignore both not present should always return true', () => {
  14. const fn = match({});
  15. assert(fn() === true);
  16. });
  17. describe('match', () => {
  18. it('support string', () => {
  19. const fn = match({ match: '/api' });
  20. assert(fn({ path: '/api/hello' }) === true);
  21. assert(fn({ path: '/api/' }) === true);
  22. assert(fn({ path: '/api' }) === true);
  23. assert(fn({ path: '/api1/hello' }) === false);
  24. assert(fn({ path: '/api1' }) === false);
  25. });
  26. it('support regexp', () => {
  27. const fn = match({ match: /^\/api/ });
  28. assert(fn({ path: '/api/hello' }) === true);
  29. assert(fn({ path: '/api/' }) === true);
  30. assert(fn({ path: '/api' }) === true);
  31. assert(fn({ path: '/api1/hello' }) === true);
  32. assert(fn({ path: '/api1' }) === true);
  33. assert(fn({ path: '/v1/api1' }) === false);
  34. });
  35. it('support global regexp', () => {
  36. const fn = match({ match: /^\/api/g });
  37. assert(fn({ path: '/api/hello' }) === true);
  38. assert(fn({ path: '/api/' }) === true);
  39. assert(fn({ path: '/api' }) === true);
  40. assert(fn({ path: '/api1/hello' }) === true);
  41. assert(fn({ path: '/api1' }) === true);
  42. assert(fn({ path: '/v1/api1' }) === false);
  43. });
  44. it('support function', () => {
  45. const fn = match({
  46. match: ctx => ctx.path.startsWith('/api'),
  47. });
  48. assert(fn({ path: '/api/hello' }) === true);
  49. assert(fn({ path: '/api/' }) === true);
  50. assert(fn({ path: '/api' }) === true);
  51. assert(fn({ path: '/api1/hello' }) === true);
  52. assert(fn({ path: '/api1' }) === true);
  53. assert(fn({ path: '/v1/api1' }) === false);
  54. });
  55. it('support array', () => {
  56. const fn = match({
  57. match: [ ctx => ctx.path.startsWith('/api'), '/ajax', /^\/foo$/ ],
  58. });
  59. assert(fn({ path: '/api/hello' }) === true);
  60. assert(fn({ path: '/api/' }) === true);
  61. assert(fn({ path: '/api' }) === true);
  62. assert(fn({ path: '/api1/hello' }) === true);
  63. assert(fn({ path: '/api1' }) === true);
  64. assert(fn({ path: '/v1/api1' }) === false);
  65. assert(fn({ path: '/ajax/hello' }) === true);
  66. assert(fn({ path: '/foo' }) === true);
  67. });
  68. });
  69. describe('ignore', () => {
  70. it('support string', () => {
  71. const fn = match({ ignore: '/api' });
  72. assert(fn({ path: '/api/hello' }) === false);
  73. assert(fn({ path: '/api/' }) === false);
  74. assert(fn({ path: '/api' }) === false);
  75. assert(fn({ path: '/api1/hello' }) === true);
  76. assert(fn({ path: '/api1' }) === true);
  77. });
  78. it('support regexp', () => {
  79. const fn = match({ ignore: /^\/api/ });
  80. assert(fn({ path: '/api/hello' }) === false);
  81. assert(fn({ path: '/api/' }) === false);
  82. assert(fn({ path: '/api' }) === false);
  83. assert(fn({ path: '/api1/hello' }) === false);
  84. assert(fn({ path: '/api1' }) === false);
  85. assert(fn({ path: '/v1/api1' }) === true);
  86. });
  87. it('support global regexp', () => {
  88. const fn = match({ ignore: /^\/api/g });
  89. assert(fn({ path: '/api/hello' }) === false);
  90. assert(fn({ path: '/api/' }) === false);
  91. assert(fn({ path: '/api' }) === false);
  92. assert(fn({ path: '/api1/hello' }) === false);
  93. assert(fn({ path: '/api1' }) === false);
  94. assert(fn({ path: '/v1/api1' }) === true);
  95. });
  96. it('support function', () => {
  97. const fn = match({
  98. ignore: ctx => ctx.path.startsWith('/api'),
  99. });
  100. assert(fn({ path: '/api/hello' }) === false);
  101. assert(fn({ path: '/api/' }) === false);
  102. assert(fn({ path: '/api' }) === false);
  103. assert(fn({ path: '/api1/hello' }) === false);
  104. assert(fn({ path: '/api1' }) === false);
  105. assert(fn({ path: '/v1/api1' }) === true);
  106. });
  107. it('support array', () => {
  108. const fn = match({
  109. ignore: [ ctx => ctx.path.startsWith('/api'), '/ajax', /^\/foo$/ ],
  110. });
  111. assert(fn({ path: '/api/hello' }) === false);
  112. assert(fn({ path: '/api/' }) === false);
  113. assert(fn({ path: '/api' }) === false);
  114. assert(fn({ path: '/api1/hello' }) === false);
  115. assert(fn({ path: '/api1' }) === false);
  116. assert(fn({ path: '/v1/api1' }) === true);
  117. assert(fn({ path: '/ajax/hello' }) === false);
  118. assert(fn({ path: '/foo' }) === false);
  119. });
  120. });
  121. });