|
| 1 | +package com.retailsvc.http.internal; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +class PathPatternTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + void exactPathHasNoWildcardAndMatchesItself() { |
| 12 | + PathPattern p = PathPattern.compile("/alive"); |
| 13 | + assertThat(p.hasWildcard()).isFalse(); |
| 14 | + assertThat(p.matches("/alive")).isTrue(); |
| 15 | + assertThat(p.matches("/alive/")).isFalse(); |
| 16 | + assertThat(p.matches("/alive232")).isFalse(); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + void singleStarMatchesOneSegment() { |
| 21 | + PathPattern p = PathPattern.compile("/files/*"); |
| 22 | + assertThat(p.hasWildcard()).isTrue(); |
| 23 | + assertThat(p.matches("/files/a")).isTrue(); |
| 24 | + assertThat(p.matches("/files/abc.txt")).isTrue(); |
| 25 | + assertThat(p.matches("/files/")).isFalse(); |
| 26 | + assertThat(p.matches("/files/a/b")).isFalse(); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void doubleStarMatchesAnyDepth() { |
| 31 | + PathPattern p = PathPattern.compile("/files/**"); |
| 32 | + assertThat(p.matches("/files/")).isTrue(); |
| 33 | + assertThat(p.matches("/files/a")).isTrue(); |
| 34 | + assertThat(p.matches("/files/a/b/c")).isTrue(); |
| 35 | + assertThat(p.matches("/files")).isFalse(); |
| 36 | + assertThat(p.matches("/filesx/a")).isFalse(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void midPathDoubleStarSurroundedByLiterals() { |
| 41 | + PathPattern p = PathPattern.compile("/schemas/**/openapi.yaml"); |
| 42 | + assertThat(p.matches("/schemas/a/openapi.yaml")).isTrue(); |
| 43 | + assertThat(p.matches("/schemas/a/b/openapi.yaml")).isTrue(); |
| 44 | + assertThat(p.matches("/schemas/openapi.yaml")).isFalse(); |
| 45 | + assertThat(p.matches("/schemas/a/openapi.yamlx")).isFalse(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void mixedSegmentRejected() { |
| 50 | + assertThatThrownBy(() -> PathPattern.compile("/files/prefix-*.json")) |
| 51 | + .isInstanceOf(IllegalArgumentException.class) |
| 52 | + .hasMessageContaining("must be a whole segment"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void emptySegmentRejected() { |
| 57 | + assertThatThrownBy(() -> PathPattern.compile("/files//a")) |
| 58 | + .isInstanceOf(IllegalArgumentException.class) |
| 59 | + .hasMessageContaining("empty segment"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void adjacentDoubleStarsRejected() { |
| 64 | + assertThatThrownBy(() -> PathPattern.compile("/a/**/**/b")) |
| 65 | + .isInstanceOf(IllegalArgumentException.class) |
| 66 | + .hasMessageContaining("adjacent"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void mustStartWithSlash() { |
| 71 | + assertThatThrownBy(() -> PathPattern.compile("files/*")) |
| 72 | + .isInstanceOf(IllegalArgumentException.class) |
| 73 | + .hasMessageContaining("must start with '/'"); |
| 74 | + } |
| 75 | +} |
0 commit comments