|
| 1 | +import 'package:swagger_parser/src/parser/utils/path_match.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('matchesPathPattern', () { |
| 6 | + test('exact match without wildcards', () { |
| 7 | + expect(matchesPathPattern('/api/users', ['/api/users']), isTrue); |
| 8 | + expect(matchesPathPattern('/api/users/profile', ['/api/users/profile']), isTrue); |
| 9 | + expect(matchesPathPattern('/exact/path/{id}', ['/exact/path/{id}']), isTrue); |
| 10 | + }); |
| 11 | + |
| 12 | + test('no match for different paths', () { |
| 13 | + expect(matchesPathPattern('/api/users', ['/api/posts']), isFalse); |
| 14 | + expect(matchesPathPattern('/api/users/profile', ['/api/users/settings']), isFalse); |
| 15 | + }); |
| 16 | + |
| 17 | + test('single asterisk wildcard (*)', () { |
| 18 | + // Basic single segment match |
| 19 | + expect(matchesPathPattern('/users/123/update', ['/users/*/update']), isTrue); |
| 20 | + expect(matchesPathPattern('/api/v1/users', ['/api/*/users']), isTrue); |
| 21 | + |
| 22 | + // No match when path has more segments |
| 23 | + expect(matchesPathPattern('/users/123/profile/update', ['/users/*/update']), isFalse); |
| 24 | + expect(matchesPathPattern('/api/v1/extra/users', ['/api/*/users']), isFalse); |
| 25 | + |
| 26 | + // Match at different positions |
| 27 | + expect(matchesPathPattern('/users/123', ['/users/*']), isTrue); |
| 28 | + expect(matchesPathPattern('/api/123/endpoint', ['/api/*/endpoint']), isTrue); |
| 29 | + }); |
| 30 | + |
| 31 | + test('double asterisk wildcard (**) - long path', () { |
| 32 | + expect(matchesPathPattern('/api/service/loyalty/balance/history', ['/api/service/loyalty/**']), isTrue); |
| 33 | + }); |
| 34 | + |
| 35 | + test('double asterisk wildcard (**) - another', () { |
| 36 | + expect(matchesPathPattern('/another/wildcard/long/path', ['/another/wildcard/**']), isTrue); |
| 37 | + }); |
| 38 | + |
| 39 | + test('double asterisk wildcard (**) - parts', () { |
| 40 | + expect(matchesPathPattern('path/with/several/parts', ['path/**/parts']), isTrue); |
| 41 | + }); |
| 42 | + |
| 43 | + test('double asterisk wildcard (**) - single', () { |
| 44 | + expect(matchesPathPattern('/api/single', ['/api/**']), isTrue); |
| 45 | + }); |
| 46 | + |
| 47 | + test('double asterisk wildcard (**) - wildcard', () { |
| 48 | + expect(matchesPathPattern('wildcard/path', ['wildcard/**']), isTrue); |
| 49 | + }); |
| 50 | + |
| 51 | + test('double asterisk wildcard (**) - empty', () { |
| 52 | + expect(matchesPathPattern('/api', ['/api/**']), isTrue); |
| 53 | + }); |
| 54 | + |
| 55 | + test('multiple patterns', () { |
| 56 | + expect(matchesPathPattern('/users/123/update', ['/posts/*/create', '/users/*/update']), isTrue); |
| 57 | + expect(matchesPathPattern('/api/v1/users', ['/api/v2/users', '/api/v1/*']), isTrue); |
| 58 | + expect(matchesPathPattern('/api/service/loyalty/balance', ['/api/*/loyalty', '/api/service/**']), isTrue); |
| 59 | + }); |
| 60 | + |
| 61 | + test('special regex characters in paths', () { |
| 62 | + // Dots in paths |
| 63 | + expect(matchesPathPattern('/api/v1.0/users', ['/api/*/users']), isTrue); |
| 64 | + expect(matchesPathPattern('/files/document.pdf', ['/files/*']), isTrue); |
| 65 | + |
| 66 | + // Plus signs |
| 67 | + expect(matchesPathPattern('/api/v1+users', ['/api/*']), isTrue); |
| 68 | + |
| 69 | + // Parentheses |
| 70 | + expect(matchesPathPattern('/api(v1)', ['/*']), isTrue); |
| 71 | + }); |
| 72 | + |
| 73 | + test('empty and edge cases', () { |
| 74 | + // Empty patterns list should not match |
| 75 | + expect(matchesPathPattern('/any/path', []), isFalse); |
| 76 | + |
| 77 | + // Empty path |
| 78 | + expect(matchesPathPattern('', ['']), isTrue); |
| 79 | + expect(matchesPathPattern('', ['*']), isFalse); // * requires at least one non-slash char |
| 80 | + expect(matchesPathPattern('', ['**']), isTrue); |
| 81 | + |
| 82 | + // Root path |
| 83 | + expect(matchesPathPattern('/', ['/']), isTrue); |
| 84 | + expect(matchesPathPattern('/', ['/*']), isFalse); |
| 85 | + }); |
| 86 | + |
| 87 | + test('complex patterns', () { |
| 88 | + // Mixed wildcards |
| 89 | + expect(matchesPathPattern('/api/v1/users/123/posts/456/comments', ['/api/*/users/*/posts/**']), isTrue); |
| 90 | + expect(matchesPathPattern('/users/123/profile/settings/advanced', ['/users/*/profile/**']), isTrue); |
| 91 | + |
| 92 | + // Wildcard at start (matches one segment) |
| 93 | + expect(matchesPathPattern('/dynamic/endpoint', ['*/endpoint']), isTrue); |
| 94 | + expect(matchesPathPattern('/some/deep/path/endpoint', ['**/endpoint']), isTrue); |
| 95 | + }); |
| 96 | + |
| 97 | + test('no match cases', () { |
| 98 | + // Different structure |
| 99 | + expect(matchesPathPattern('/users/123/update', ['/posts/*/create']), isFalse); |
| 100 | + expect(matchesPathPattern('/api/v1/users', ['/api/v2/*']), isFalse); |
| 101 | + |
| 102 | + // Too many segments for single * |
| 103 | + expect(matchesPathPattern('/api/deep/nested/path', ['/api/*']), isFalse); |
| 104 | + expect(matchesPathPattern('/users/123/profile/update', ['/users/*/update']), isFalse); |
| 105 | + |
| 106 | + // Insufficient segments for ** |
| 107 | + expect(matchesPathPattern('/api', ['/api/**/endpoint']), isFalse); |
| 108 | + }); |
| 109 | + }); |
| 110 | +} |
0 commit comments