|
| 1 | +package global |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/caddyserver/caddy/v2" |
| 6 | + "github.com/caddyserver/caddy/v2/caddyconfig" |
| 7 | + "github.com/caddyserver/caddy/v2/modules/caddyhttp" |
| 8 | + "reflect" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestIngressSort(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + routes []struct { |
| 16 | + id int |
| 17 | + path string |
| 18 | + } |
| 19 | + expect []int |
| 20 | + }{ |
| 21 | + |
| 22 | + { |
| 23 | + name: "multiple exact paths", |
| 24 | + routes: []struct { |
| 25 | + id int |
| 26 | + path string |
| 27 | + }{ |
| 28 | + {id: 0, path: "/path/a"}, |
| 29 | + {id: 1, path: "/path/"}, |
| 30 | + {id: 2, path: "/other"}, |
| 31 | + }, |
| 32 | + expect: []int{0, 1, 2}, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "multiple prefix paths", |
| 36 | + routes: []struct { |
| 37 | + id int |
| 38 | + path string |
| 39 | + }{ |
| 40 | + {id: 0, path: "/path/*"}, |
| 41 | + {id: 1, path: "/path/auth/*"}, |
| 42 | + {id: 2, path: "/other/*"}, |
| 43 | + {id: 3, path: "/login/*"}, |
| 44 | + }, |
| 45 | + expect: []int{1, 2, 3, 0}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "mixed exact and prefixed", |
| 49 | + routes: []struct { |
| 50 | + id int |
| 51 | + path string |
| 52 | + }{ |
| 53 | + {id: 0, path: "/path/*"}, |
| 54 | + {id: 1, path: "/path/auth/"}, |
| 55 | + {id: 2, path: "/path/v2/*"}, |
| 56 | + {id: 3, path: "/path/new"}, |
| 57 | + }, |
| 58 | + expect: []int{1, 3, 2, 0}, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "mixed exact, prefix and empty", |
| 62 | + routes: []struct { |
| 63 | + id int |
| 64 | + path string |
| 65 | + }{ |
| 66 | + {id: 0, path: "/path/*"}, |
| 67 | + {id: 1, path: ""}, |
| 68 | + {id: 2, path: "/path/v2/*"}, |
| 69 | + {id: 3, path: "/path/new"}, |
| 70 | + {id: 4, path: ""}, |
| 71 | + }, |
| 72 | + expect: []int{3, 2, 0, 1, 4}, |
| 73 | + }, |
| 74 | + } |
| 75 | + for _, test := range tests { |
| 76 | + t.Run(test.name, func(t *testing.T) { |
| 77 | + routes := []caddyhttp.Route{} |
| 78 | + |
| 79 | + for _, route := range test.routes { |
| 80 | + match := caddy.ModuleMap{} |
| 81 | + match["id"] = caddyconfig.JSON(route.id, nil) |
| 82 | + |
| 83 | + if route.path != "" { |
| 84 | + match["path"] = caddyconfig.JSON(caddyhttp.MatchPath{route.path}, nil) |
| 85 | + } |
| 86 | + |
| 87 | + r := caddyhttp.Route{MatcherSetsRaw: []caddy.ModuleMap{match}} |
| 88 | + routes = append(routes, r) |
| 89 | + } |
| 90 | + |
| 91 | + sortRoutes(routes) |
| 92 | + |
| 93 | + var got []int |
| 94 | + for i := range test.expect { |
| 95 | + var currentId int |
| 96 | + err := json.Unmarshal(routes[i].MatcherSetsRaw[0]["id"], ¤tId) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("error unmarshaling id for i %v, %v", i, err) |
| 99 | + } |
| 100 | + got = append(got, currentId) |
| 101 | + } |
| 102 | + |
| 103 | + if !reflect.DeepEqual(test.expect, got) { |
| 104 | + t.Errorf("expected order to match: got %v, expected %v, %s", got, test.expect, routes[1].MatcherSetsRaw) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments