@@ -14,10 +14,51 @@ describe('findRule', () => {
1414
1515 it ( 'matches by regex url' , ( ) => {
1616 const rules = [
17- { method : 'GET' , url : / ^ h t t p s : \/ \ /f o o \. . + / , alias : 'a' } ,
17+ { method : 'GET' , url : ' https:/ /foo.com' , alias : 'a' } ,
1818 ] ;
19- expect ( findRule ( 'GET' , 'https://foo.com' , rules ) ) . toEqual ( rules [ 0 ] ) ;
20- expect ( findRule ( 'GET' , 'https://foo.org' , rules ) ) . toEqual ( rules [ 0 ] ) ;
19+ expect ( findRule ( 'GET' , / ^ h t t p s : \/ \/ f o o \. .+ / , rules ) ) . toEqual ( rules [ 0 ] ) ;
2120 expect ( findRule ( 'GET' , 'https://bar.com' , rules ) ) . toBeUndefined ( ) ;
2221 } ) ;
22+
23+ it ( 'returns the first matching rule' , ( ) => {
24+ const rules = [
25+ { method : 'GET' , url : 'https://foo.com' , alias : 'first' } ,
26+ { method : 'GET' , url : 'https://foo.com' , alias : 'second' } ,
27+ ] ;
28+ expect ( findRule ( 'GET' , 'https://foo.com' , rules ) ) . toEqual ( rules [ 0 ] ) ;
29+ } ) ;
30+
31+ it ( 'should match by contains url' , ( ) => {
32+ const rules = [
33+ { method : 'GET' , url : 'https://foo.com/api' , alias : 'a' } ,
34+ ] ;
35+ expect ( findRule ( 'GET' , 'https://foo.com/api' , rules ) ) . toEqual ( rules [ 0 ] ) ;
36+ expect ( findRule ( 'GET' , 'https://foo.com/api/users' , rules ) ) . toBeUndefined ( ) ;
37+ expect ( findRule ( 'GET' , 'https://foo.com/home' , rules ) ) . toBeUndefined ( ) ;
38+ } ) ;
39+
40+ it ( 'should match by regex' , ( ) => {
41+ const rules = [
42+ { method : 'GET' , url : 'https://foo.com/api/users/preferences' , alias : 'a' } ,
43+ ] ;
44+ expect ( findRule ( 'GET' , / \/ u s e r s \/ * / , rules ) ) . toEqual ( rules [ 0 ] ) ;
45+ } ) ;
46+
47+ it ( 'should match by contains some path of the url' , ( ) => {
48+ const rules = [
49+ { method : 'GET' , url : 'https://foo.com/api' , alias : 'a' } ,
50+ ] ;
51+ expect ( findRule ( 'GET' , '/api' , rules ) ) . toEqual ( rules [ 0 ] ) ;
52+ expect ( findRule ( 'GET' , '/api/users' , rules ) ) . toBeUndefined ( ) ;
53+ expect ( findRule ( 'GET' , 'https://foo.com/home' , rules ) ) . toBeUndefined ( ) ;
54+ } ) ;
55+
56+ it ( 'is case-insensitive for method' , ( ) => {
57+ const rules = [
58+ { method : 'get' , url : 'https://foo.com' , alias : 'a' } ,
59+ ] ;
60+ expect ( findRule ( 'GET' , 'https://foo.com' , rules ) ) . toEqual ( rules [ 0 ] ) ;
61+ expect ( findRule ( 'get' , 'https://foo.com' , rules ) ) . toEqual ( rules [ 0 ] ) ;
62+ expect ( findRule ( 'Get' , 'https://foo.com' , rules ) ) . toEqual ( rules [ 0 ] ) ;
63+ } ) ;
2364} ) ;
0 commit comments