unless.tests.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. var unless = require('..');
  2. var assert = require('chai').assert;
  3. var noop = function(){};
  4. function testMiddleware (req, res, next) {
  5. req.called = true;
  6. }
  7. testMiddleware.unless = unless;
  8. describe('express-unless', function () {
  9. describe('with PATH(with method) exception', function () {
  10. var mid = testMiddleware.unless({
  11. path: [
  12. {
  13. url: '/test',
  14. methods: ['POST', 'GET']
  15. },
  16. {
  17. url: '/bar',
  18. methods: ['PUT']
  19. },
  20. '/foo'
  21. ]
  22. });
  23. it('should not call the middleware when path and method match', function () {
  24. var req = {
  25. originalUrl: '/test?das=123',
  26. method: 'POST'
  27. };
  28. mid(req, {}, noop);
  29. assert.notOk(req.called);
  30. req = {
  31. originalUrl: '/test?test=123',
  32. method: 'GET'
  33. };
  34. mid(req, {}, noop);
  35. assert.notOk(req.called);
  36. req = {
  37. originalUrl: '/bar?test=123',
  38. method: 'PUT'
  39. };
  40. mid(req, {}, noop);
  41. assert.notOk(req.called);
  42. req = {
  43. originalUrl: '/foo',
  44. method: 'PUT'
  45. };
  46. mid(req, {}, noop);
  47. assert.notOk(req.called);
  48. });
  49. it('should call the middleware when path or method mismatch', function () {
  50. req = {
  51. originalUrl: '/test?test=123',
  52. method: 'PUT'
  53. };
  54. mid(req, {}, noop);
  55. assert.ok(req.called);
  56. req = {
  57. originalUrl: '/unless?test=123',
  58. method: 'PUT'
  59. };
  60. mid(req, {}, noop);
  61. assert.ok(req.called);
  62. });
  63. });
  64. describe('with PATH exception', function () {
  65. var mid = testMiddleware.unless({
  66. path: ['/test', '/fobo']
  67. });
  68. it('should not call the middleware when one of the path match', function () {
  69. var req = {
  70. originalUrl: '/test?das=123'
  71. };
  72. mid(req, {}, noop);
  73. assert.notOk(req.called);
  74. req = {
  75. originalUrl: '/fobo?test=123'
  76. };
  77. mid(req, {}, noop);
  78. assert.notOk(req.called);
  79. });
  80. it('should call the middleware when the path doesnt match', function () {
  81. var req = {
  82. originalUrl: '/foobar/test=123'
  83. };
  84. mid(req, {}, noop);
  85. assert.ok(req.called);
  86. });
  87. });
  88. describe('with PATH (regex) exception', function () {
  89. var mid = testMiddleware.unless({
  90. path: ['/test', /ag$/ig]
  91. });
  92. it('should not call the middleware when the regex match', function () {
  93. req = {
  94. originalUrl: '/foboag?test=123'
  95. };
  96. req2 = {
  97. originalUrl: '/foboag?test=456'
  98. };
  99. mid(req, {}, noop);
  100. mid(req2, {}, noop);
  101. assert.notOk(req.called);
  102. assert.notOk(req2.called);
  103. });
  104. });
  105. describe('with PATH (useOriginalUrl) exception', function () {
  106. var mid = testMiddleware.unless({
  107. path: ['/test', '/fobo'],
  108. useOriginalUrl: false
  109. });
  110. it('should not call the middleware when one of the path match '+
  111. 'req.url instead of req.originalUrl', function () {
  112. var req = {
  113. originalUrl: '/orig/test?das=123',
  114. url: '/test?das=123'
  115. };
  116. mid(req, {}, noop);
  117. assert.notOk(req.called);
  118. req = {
  119. originalUrl: '/orig/fobo?test=123',
  120. url: '/fobo?test=123'
  121. };
  122. mid(req, {}, noop);
  123. assert.notOk(req.called);
  124. });
  125. it('should call the middleware when the path doesnt match '+
  126. 'req.url even if path matches req.originalUrl', function () {
  127. var req = {
  128. originalUrl: '/test/test=123',
  129. url: '/foobar/test=123'
  130. };
  131. mid(req, {}, noop);
  132. assert.ok(req.called);
  133. });
  134. });
  135. describe('with EXT exception', function () {
  136. var mid = testMiddleware.unless({
  137. ext: ['jpg', 'html', 'txt']
  138. });
  139. it('should not call the middleware when the ext match', function () {
  140. var req = {
  141. originalUrl: '/foo.html?das=123'
  142. };
  143. mid(req, {}, noop);
  144. assert.notOk(req.called);
  145. });
  146. it('should call the middleware when the ext doesnt match', function () {
  147. var req = {
  148. originalUrl: '/foobar/test=123'
  149. };
  150. mid(req, {}, noop);
  151. assert.ok(req.called);
  152. });
  153. });
  154. describe('with METHOD exception', function () {
  155. var mid = testMiddleware.unless({
  156. method: ['OPTIONS', 'DELETE']
  157. });
  158. it('should not call the middleware when the method match', function () {
  159. var req = {
  160. originalUrl: '/foo.html?das=123',
  161. method: 'OPTIONS'
  162. };
  163. mid(req, {}, noop);
  164. assert.notOk(req.called);
  165. });
  166. it('should call the middleware when the method doesnt match', function () {
  167. var req = {
  168. originalUrl: '/foobar/test=123',
  169. method: 'PUT'
  170. };
  171. mid(req, {}, noop);
  172. assert.ok(req.called);
  173. });
  174. });
  175. describe('with custom exception', function () {
  176. var mid = testMiddleware.unless(function (req) {
  177. return req.baba;
  178. });
  179. it('should not call the middleware when the custom rule match', function () {
  180. var req = {
  181. baba: true
  182. };
  183. mid(req, {}, noop);
  184. assert.notOk(req.called);
  185. });
  186. it('should call the middleware when the custom rule doesnt match', function () {
  187. var req = {
  188. baba: false
  189. };
  190. mid(req, {}, noop);
  191. assert.ok(req.called);
  192. });
  193. });
  194. describe('without originalUrl', function () {
  195. var mid = testMiddleware.unless({
  196. path: ['/test']
  197. });
  198. it('should not call the middleware when one of the path match', function () {
  199. var req = {
  200. url: '/test?das=123'
  201. };
  202. mid(req, {}, noop);
  203. assert.notOk(req.called);
  204. });
  205. it('should call the middleware when the path doesnt match', function () {
  206. var req = {
  207. url: '/foobar/test=123'
  208. };
  209. mid(req, {}, noop);
  210. assert.ok(req.called);
  211. });
  212. });
  213. });