|
| 1 | +package goster |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +type IsDynamicMatchCase struct { |
| 8 | + name string |
| 9 | + url string |
| 10 | + dynPath string |
| 11 | + expectedResult bool |
| 12 | +} |
| 13 | + |
| 14 | +func TestIsDynamicRouteMatch(t *testing.T) { |
| 15 | + ctx := Ctx{ |
| 16 | + Meta: Meta{ |
| 17 | + Query: Params{ |
| 18 | + values: make(map[string]string), |
| 19 | + }, |
| 20 | + Path: Path{ |
| 21 | + values: make(map[string]string), |
| 22 | + }, |
| 23 | + }, |
| 24 | + } |
| 25 | + |
| 26 | + testCases := []IsDynamicMatchCase{ |
| 27 | + { |
| 28 | + name: "Depth 1", |
| 29 | + dynPath: "path/another_path/:var", |
| 30 | + url: "path/another_path/something", |
| 31 | + expectedResult: true, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "Depth 1 (with '/' suffix on URL)", |
| 35 | + dynPath: "path/another_path/:var", |
| 36 | + url: "path/another_path/something/", |
| 37 | + expectedResult: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Depth 1 (with '/' suffix on dynPath)", |
| 41 | + dynPath: "path/another_path/:var/", |
| 42 | + url: "path/another_path/something", |
| 43 | + expectedResult: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Depth 1 (with Depth 2 URL)", |
| 47 | + dynPath: "path/another_path/:var", |
| 48 | + url: "path/another_path/something/something2", |
| 49 | + expectedResult: false, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Depth 2", |
| 53 | + dynPath: "path/another_path/:var/:var2", |
| 54 | + url: "path/another_path/something/something2", |
| 55 | + expectedResult: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "Depth 2 (with Depth 1 URL)", |
| 59 | + dynPath: "path/another_path/:var/:var2", |
| 60 | + url: "path/another_path/something", |
| 61 | + expectedResult: false, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "Depth 2 (with '/' suffix on dynPath)", |
| 65 | + dynPath: "path/another_path/:var/:var2/", |
| 66 | + url: "path/another_path/something/something2", |
| 67 | + expectedResult: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "Depth 2 (with '/' suffix on URL)", |
| 71 | + dynPath: "path/another_path/:var/:var2", |
| 72 | + url: "path/another_path/something/something2/", |
| 73 | + expectedResult: true, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + failedCases := make(map[int]IsDynamicMatchCase, 0) |
| 78 | + for i, c := range testCases { |
| 79 | + if isDynamicRouteMatch(&ctx, c.url, c.dynPath) != c.expectedResult { |
| 80 | + failedCases[i] = c |
| 81 | + } else { |
| 82 | + t.Logf("PASSED [%d] - %s\n", i, c.name) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Space |
| 87 | + t.Log("") |
| 88 | + |
| 89 | + for i, c := range failedCases { |
| 90 | + t.Errorf("FAILED [%d] - %s\n", i, c.name) |
| 91 | + } |
| 92 | + |
| 93 | + t.Logf("TOTAL CASES: %d\n", len(testCases)) |
| 94 | + t.Logf("FAILED CASES: %d\n", len(failedCases)) |
| 95 | +} |
0 commit comments