(t *testing.T)
| 439 | } |
| 440 | |
| 441 | func TestRESTMatch(t *testing.T) { |
| 442 | get := &openapi3.PathItem{Get: &openapi3.Operation{}} |
| 443 | idx := &restIndex{} |
| 444 | for tmpl, item := range map[string]*openapi3.PathItem{ |
| 445 | "/": get, |
| 446 | "/repos/{owner}/{repo}": get, |
| 447 | "/repos/{owner}/{repo}/issues/{issue_number}": get, |
| 448 | "/repos/{owner}/{repo}/issues/comments": get, |
| 449 | "/repos/{owner}/{repo}/contents/{path}": get, |
| 450 | "/repos/{owner}/{repo}/compare/{basehead}": get, |
| 451 | "/files/{name}.{ext}": get, |
| 452 | "/orgs/{org}/attestations/{attestation_id}": {Delete: &openapi3.Operation{}}, |
| 453 | "/orgs/{org}/attestations/{subject_digest}": get, |
| 454 | } { |
| 455 | idx.routes = append(idx.routes, restRoute{template: tmpl, segments: parseTemplate(tmpl), item: item}) |
| 456 | } |
| 457 | sort.Slice(idx.routes, func(i, j int) bool { return idx.routes[i].template < idx.routes[j].template }) |
| 458 | |
| 459 | tests := []struct { |
| 460 | method string |
| 461 | path string |
| 462 | want string |
| 463 | }{ |
| 464 | {path: "/", want: "/"}, |
| 465 | {path: "/repos/o/r", want: "/repos/{owner}/{repo}"}, |
| 466 | {path: "/repos/o/r/", want: "/repos/{owner}/{repo}"}, |
| 467 | {path: "/repos/o/r/issues/1", want: "/repos/{owner}/{repo}/issues/{issue_number}"}, |
| 468 | {path: "/repos/o/r/issues/comments", want: "/repos/{owner}/{repo}/issues/comments"}, |
| 469 | {path: "/repos/o/r/contents/docs/README.md", want: "/repos/{owner}/{repo}/contents/{path}"}, |
| 470 | {path: "/repos/o/r/compare/main...feature/x", want: "/repos/{owner}/{repo}/compare/{basehead}"}, |
| 471 | {path: "/files/report.txt", want: "/files/{name}.{ext}"}, |
| 472 | {path: "/files/report"}, |
| 473 | {path: "/repos/o"}, |
| 474 | {path: "/repos/o/r/issues"}, |
| 475 | {path: "/repos/o/r/issues/1/extra"}, |
| 476 | {path: "/repos//r"}, |
| 477 | {path: "/orgs/o/attestations/sha256:abc", want: "/orgs/{org}/attestations/{subject_digest}"}, |
| 478 | {method: "DELETE", path: "/orgs/o/attestations/1", want: "/orgs/{org}/attestations/{attestation_id}"}, |
| 479 | {method: "PATCH", path: "/repos/o/r/issues/comments", want: "/repos/{owner}/{repo}/issues/comments"}, |
| 480 | } |
| 481 | |
| 482 | for _, tt := range tests { |
| 483 | method := tt.method |
| 484 | if method == "" { |
| 485 | method = "GET" |
| 486 | } |
| 487 | t.Run(method+" "+tt.path, func(t *testing.T) { |
| 488 | got := "" |
| 489 | if route := idx.match(method, tt.path); route != nil { |
| 490 | got = route.template |
| 491 | } |
| 492 | assert.Equal(t, tt.want, got) |
| 493 | }) |
| 494 | } |
| 495 | } |
nothing calls this directly
no test coverage detected