package httpapi import ( "encoding/json" "os" "path/filepath" "reflect" "runtime" "sort" "testing" ) func TestGoRouteSurfaceExactlyMatchesCheckedInPublicHTTPContract(t *testing.T) { _, source, _, _ := runtime.Caller(0) raw, err := os.ReadFile(filepath.Join(filepath.Dir(source), "../../../contracts/http/route-surface.v1.json")) if err != nil { t.Fatal(err) } var fixture struct { Version int `json:"version"` Routes []RouteSurface `json:"routes"` } if err := json.Unmarshal(raw, &fixture); err != nil { t.Fatal(err) } if fixture.Version != 1 { t.Fatalf("version=%d", fixture.Version) } got := GoRouteSurface() less := func(items []RouteSurface) { sort.Slice(items, func(i, j int) bool { if items[i].Method != items[j].Method { return items[i].Method < items[j].Method } return items[i].Path < items[j].Path }) } less(got) less(fixture.Routes) if !reflect.DeepEqual(got, fixture.Routes) { t.Fatalf("Go route surface drift\ngot=%#v\nwant=%#v", got, fixture.Routes) } } func TestMatchSurfacePath(t *testing.T) { for _, test := range []struct { pattern, path string want bool }{{"/api/x/{id}", "/api/x/a", true}, {"/api/x/{id}", "/api/x/a/b", false}, {"/uploads/{path...}", "/uploads/a/b", true}, {"/uploads/{path...}", "/uploads/", false}, {"/generated-results/{path...}", "/generated-results/", false}, {"/api/health", "/api/health", true}} { if got := MatchSurfacePath(test.pattern, test.path); got != test.want { t.Fatalf("%q %q=%v", test.pattern, test.path, got) } } }