123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 'use strict';
- const assert = require('power-assert');
- const match = require('..');
- describe('egg-path-matching', () => {
- it('options.match and options.ignore both present should throw', () => {
- try {
- match({ ignore: '/api', match: '/dashboard' });
- throw new Error('should not exec');
- } catch (e) {
- assert(e.message === 'options.match and options.ignore can not both present');
- }
- });
- it('options.match and options.ignore both not present should always return true', () => {
- const fn = match({});
- assert(fn() === true);
- });
- describe('match', () => {
- it('support string', () => {
- const fn = match({ match: '/api' });
- assert(fn({ path: '/api/hello' }) === true);
- assert(fn({ path: '/api/' }) === true);
- assert(fn({ path: '/api' }) === true);
- assert(fn({ path: '/api1/hello' }) === false);
- assert(fn({ path: '/api1' }) === false);
- });
- it('support regexp', () => {
- const fn = match({ match: /^\/api/ });
- assert(fn({ path: '/api/hello' }) === true);
- assert(fn({ path: '/api/' }) === true);
- assert(fn({ path: '/api' }) === true);
- assert(fn({ path: '/api1/hello' }) === true);
- assert(fn({ path: '/api1' }) === true);
- assert(fn({ path: '/v1/api1' }) === false);
- });
- it('support global regexp', () => {
- const fn = match({ match: /^\/api/g });
- assert(fn({ path: '/api/hello' }) === true);
- assert(fn({ path: '/api/' }) === true);
- assert(fn({ path: '/api' }) === true);
- assert(fn({ path: '/api1/hello' }) === true);
- assert(fn({ path: '/api1' }) === true);
- assert(fn({ path: '/v1/api1' }) === false);
- });
- it('support function', () => {
- const fn = match({
- match: ctx => ctx.path.startsWith('/api'),
- });
- assert(fn({ path: '/api/hello' }) === true);
- assert(fn({ path: '/api/' }) === true);
- assert(fn({ path: '/api' }) === true);
- assert(fn({ path: '/api1/hello' }) === true);
- assert(fn({ path: '/api1' }) === true);
- assert(fn({ path: '/v1/api1' }) === false);
- });
- it('support array', () => {
- const fn = match({
- match: [ ctx => ctx.path.startsWith('/api'), '/ajax', /^\/foo$/ ],
- });
- assert(fn({ path: '/api/hello' }) === true);
- assert(fn({ path: '/api/' }) === true);
- assert(fn({ path: '/api' }) === true);
- assert(fn({ path: '/api1/hello' }) === true);
- assert(fn({ path: '/api1' }) === true);
- assert(fn({ path: '/v1/api1' }) === false);
- assert(fn({ path: '/ajax/hello' }) === true);
- assert(fn({ path: '/foo' }) === true);
- });
- });
- describe('ignore', () => {
- it('support string', () => {
- const fn = match({ ignore: '/api' });
- assert(fn({ path: '/api/hello' }) === false);
- assert(fn({ path: '/api/' }) === false);
- assert(fn({ path: '/api' }) === false);
- assert(fn({ path: '/api1/hello' }) === true);
- assert(fn({ path: '/api1' }) === true);
- });
- it('support regexp', () => {
- const fn = match({ ignore: /^\/api/ });
- assert(fn({ path: '/api/hello' }) === false);
- assert(fn({ path: '/api/' }) === false);
- assert(fn({ path: '/api' }) === false);
- assert(fn({ path: '/api1/hello' }) === false);
- assert(fn({ path: '/api1' }) === false);
- assert(fn({ path: '/v1/api1' }) === true);
- });
- it('support global regexp', () => {
- const fn = match({ ignore: /^\/api/g });
- assert(fn({ path: '/api/hello' }) === false);
- assert(fn({ path: '/api/' }) === false);
- assert(fn({ path: '/api' }) === false);
- assert(fn({ path: '/api1/hello' }) === false);
- assert(fn({ path: '/api1' }) === false);
- assert(fn({ path: '/v1/api1' }) === true);
- });
- it('support function', () => {
- const fn = match({
- ignore: ctx => ctx.path.startsWith('/api'),
- });
- assert(fn({ path: '/api/hello' }) === false);
- assert(fn({ path: '/api/' }) === false);
- assert(fn({ path: '/api' }) === false);
- assert(fn({ path: '/api1/hello' }) === false);
- assert(fn({ path: '/api1' }) === false);
- assert(fn({ path: '/v1/api1' }) === true);
- });
- it('support array', () => {
- const fn = match({
- ignore: [ ctx => ctx.path.startsWith('/api'), '/ajax', /^\/foo$/ ],
- });
- assert(fn({ path: '/api/hello' }) === false);
- assert(fn({ path: '/api/' }) === false);
- assert(fn({ path: '/api' }) === false);
- assert(fn({ path: '/api1/hello' }) === false);
- assert(fn({ path: '/api1' }) === false);
- assert(fn({ path: '/v1/api1' }) === true);
- assert(fn({ path: '/ajax/hello' }) === false);
- assert(fn({ path: '/foo' }) === false);
- });
- });
- });
|