|
6 | 6 | package github |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "context" |
9 | 10 | "fmt" |
10 | 11 | "net/http" |
11 | 12 | "net/url" |
@@ -685,3 +686,80 @@ func TestRepositoriesService_ListBranchesHeadCommit(t *testing.T) { |
685 | 686 | return resp, err |
686 | 687 | }) |
687 | 688 | } |
| 689 | + |
| 690 | +func TestRepositoriesService_refIsEscaped(t *testing.T) { |
| 691 | + t.Parallel() |
| 692 | + for _, ref := range []struct { |
| 693 | + name string |
| 694 | + escapedRef string |
| 695 | + }{ |
| 696 | + {name: "feature/x", escapedRef: "feature/x"}, |
| 697 | + {name: "fix#12", escapedRef: "fix%2312"}, |
| 698 | + {name: "what?", escapedRef: "what%3F"}, |
| 699 | + {name: "100%", escapedRef: "100%25"}, |
| 700 | + {name: "a b", escapedRef: "a%20b"}, |
| 701 | + {name: "50%25", escapedRef: "50%2525"}, |
| 702 | + } { |
| 703 | + for _, tt := range []struct { |
| 704 | + method string |
| 705 | + wantPath string |
| 706 | + call func(ctx context.Context, client *Client) error |
| 707 | + }{ |
| 708 | + { |
| 709 | + method: "GetCommit", |
| 710 | + wantPath: "/repos/o/r/commits/", |
| 711 | + call: func(ctx context.Context, client *Client) error { |
| 712 | + _, _, err := client.Repositories.GetCommit(ctx, "o", "r", ref.name, nil) |
| 713 | + return err |
| 714 | + }, |
| 715 | + }, |
| 716 | + { |
| 717 | + method: "GetCommitRaw", |
| 718 | + wantPath: "/repos/o/r/commits/", |
| 719 | + call: func(ctx context.Context, client *Client) error { |
| 720 | + _, _, err := client.Repositories.GetCommitRaw(ctx, "o", "r", ref.name, RawOptions{Type: Diff}) |
| 721 | + return err |
| 722 | + }, |
| 723 | + }, |
| 724 | + { |
| 725 | + method: "GetReleaseByTag", |
| 726 | + wantPath: "/repos/o/r/releases/tags/", |
| 727 | + call: func(ctx context.Context, client *Client) error { |
| 728 | + _, _, err := client.Repositories.GetReleaseByTag(ctx, "o", "r", ref.name) |
| 729 | + return err |
| 730 | + }, |
| 731 | + }, |
| 732 | + { |
| 733 | + method: "GetArchiveLink", |
| 734 | + wantPath: "/repos/o/r/tarball/", |
| 735 | + call: func(ctx context.Context, client *Client) error { |
| 736 | + _, _, err := client.Repositories.GetArchiveLink(ctx, "o", "r", Tarball, &RepositoryContentGetOptions{Ref: ref.name}, 0) |
| 737 | + return err |
| 738 | + }, |
| 739 | + }, |
| 740 | + } { |
| 741 | + t.Run(tt.method+"/"+ref.name, func(t *testing.T) { |
| 742 | + t.Parallel() |
| 743 | + client, mux, _ := setup(t) |
| 744 | + |
| 745 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 746 | + if got, want := r.URL.EscapedPath(), tt.wantPath+ref.escapedRef; got != want { |
| 747 | + t.Errorf("Repositories.%v requested path %q, want %q", tt.method, got, want) |
| 748 | + } |
| 749 | + if r.URL.RawQuery != "" { |
| 750 | + t.Errorf("Repositories.%v requested query %q, want none", tt.method, r.URL.RawQuery) |
| 751 | + } |
| 752 | + if tt.method == "GetArchiveLink" { |
| 753 | + http.Redirect(w, r, "https://github.com/a", http.StatusFound) |
| 754 | + return |
| 755 | + } |
| 756 | + fmt.Fprint(w, `{}`) |
| 757 | + }) |
| 758 | + |
| 759 | + if err := tt.call(t.Context(), client); err != nil { |
| 760 | + t.Errorf("Repositories.%v returned error: %v", tt.method, err) |
| 761 | + } |
| 762 | + }) |
| 763 | + } |
| 764 | + } |
| 765 | +} |
0 commit comments