|
| 1 | +import { matchPattern, MatchResult } from '#src/index.js' |
| 2 | +import { |
| 3 | + NO_MATCH, |
| 4 | + MATCHES_WITH_PARAMS, |
| 5 | + MATCHES_WITHOUT_PARAMS, |
| 6 | +} from '#tests/utils.js' |
| 7 | + |
| 8 | +it.each< |
| 9 | + [ |
| 10 | + Parameters<typeof matchPattern>[0], |
| 11 | + Parameters<typeof matchPattern>[1], |
| 12 | + MatchResult, |
| 13 | + ] |
| 14 | +>([ |
| 15 | + /* Literal IPv6 addresses */ |
| 16 | + ['http://[::1]/', 'http://[::1]/', MATCHES_WITHOUT_PARAMS], |
| 17 | + ['http://[::1]/', new URL('http://[::1]/'), MATCHES_WITHOUT_PARAMS], |
| 18 | + ['http://[::1]', new URL('http://[::1]'), MATCHES_WITHOUT_PARAMS], |
| 19 | + [ |
| 20 | + 'http://[2001:db8::1]/path', |
| 21 | + 'http://[2001:db8::1]/path', |
| 22 | + MATCHES_WITHOUT_PARAMS, |
| 23 | + ], |
| 24 | + [ |
| 25 | + 'http://[2001:db8::a1]/x', |
| 26 | + 'http://[2001:db8::a1]/x', |
| 27 | + MATCHES_WITHOUT_PARAMS, |
| 28 | + ], |
| 29 | + ['http://[fe80::abcd]/', 'http://[fe80::abcd]/', MATCHES_WITHOUT_PARAMS], |
| 30 | + [ |
| 31 | + 'http://[2001:db8:85a3::8a2e:370:7334]/', |
| 32 | + 'http://[2001:db8:85a3::8a2e:370:7334]/', |
| 33 | + MATCHES_WITHOUT_PARAMS, |
| 34 | + ], |
| 35 | + |
| 36 | + /* IPv6 with port */ |
| 37 | + ['http://[::1]:8080/path', 'http://[::1]:8080/path', MATCHES_WITHOUT_PARAMS], |
| 38 | + [ |
| 39 | + 'http://[::1]:*/path', |
| 40 | + 'http://[::1]:8080/path', |
| 41 | + MATCHES_WITH_PARAMS({ '0': '8080' }), |
| 42 | + ], |
| 43 | + |
| 44 | + /* IPv6 zone identifier (URL-encoded `%`) */ |
| 45 | + [ |
| 46 | + 'http://[fe80::1%25eth0]/', |
| 47 | + 'http://[fe80::1%25eth0]/', |
| 48 | + MATCHES_WITHOUT_PARAMS, |
| 49 | + ], |
| 50 | + |
| 51 | + /* Path parameters after an IPv6 host */ |
| 52 | + [ |
| 53 | + 'http://[::1]/user/:id', |
| 54 | + new URL('http://[::1]/user/123'), |
| 55 | + MATCHES_WITH_PARAMS({ id: '123' }), |
| 56 | + ], |
| 57 | + [ |
| 58 | + 'http://[::1]/:resource/:id', |
| 59 | + 'http://[::1]/user/123', |
| 60 | + MATCHES_WITH_PARAMS({ resource: 'user', id: '123' }), |
| 61 | + ], |
| 62 | + [ |
| 63 | + 'http://[2001:db8::1]/user/:id', |
| 64 | + 'http://[2001:db8::1]/user/456', |
| 65 | + MATCHES_WITH_PARAMS({ id: '456' }), |
| 66 | + ], |
| 67 | + |
| 68 | + /* Wildcards alongside an IPv6 host */ |
| 69 | + [ |
| 70 | + 'http://[::1]/*', |
| 71 | + new URL('http://[::1]/foo/bar'), |
| 72 | + MATCHES_WITH_PARAMS({ '0': 'foo/bar' }), |
| 73 | + ], |
| 74 | + ['*://[::1]/path', 'http://[::1]/path', MATCHES_WITH_PARAMS({ '0': 'http' })], |
| 75 | + ['http://*/path', 'http://[::1]/path', MATCHES_WITH_PARAMS({ '0': '[::1]' })], |
| 76 | + |
| 77 | + /* Mismatches */ |
| 78 | + ['http://[::1]/path', 'http://[::2]/path', NO_MATCH], |
| 79 | + ['http://[::1]/path', 'http://[::1]/other', NO_MATCH], |
| 80 | +])('matches %j against %j', (pattern, input, expectedResult) => { |
| 81 | + expect(matchPattern(pattern, input)).toEqual(expectedResult) |
| 82 | +}) |
0 commit comments