@@ -133,6 +133,99 @@ func TestListRepoBranches_NoopClientFallback_NotFound(t *testing.T) {
133133 }
134134}
135135
136+ func TestGetPRAndIssue_FallBackWithoutClient (t * testing.T ) {
137+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
138+ w .Header ().Set ("Content-Type" , "application/json" )
139+ switch r .URL .Path {
140+ case "/repos/owner/repo/pulls/7" :
141+ _ , _ = w .Write ([]byte (`{"number":7,"title":"Public PR","state":"open","head":{"ref":"feature"},"base":{"ref":"main"},"user":{"login":"alice"}}` ))
142+ case "/repos/owner/repo/issues/8" :
143+ _ , _ = w .Write ([]byte (`{"number":8,"title":"Public issue","state":"open","user":{"login":"alice"}}` ))
144+ default :
145+ http .NotFound (w , r )
146+ }
147+ }))
148+ defer srv .Close ()
149+
150+ orig := anonymousAPIBase
151+ anonymousAPIBase = srv .URL
152+ defer func () { anonymousAPIBase = orig }()
153+
154+ for _ , client := range []Client {nil , & NoopClient {}} {
155+ svc := & Service {client : client }
156+ pr , err := svc .GetPR (context .Background (), "owner" , "repo" , 7 )
157+ if err != nil {
158+ t .Fatalf ("GetPR() error = %v" , err )
159+ }
160+ if pr .Title != "Public PR" {
161+ t .Fatalf ("GetPR() title = %q, want public PR" , pr .Title )
162+ }
163+
164+ issue , err := svc .GetIssue (context .Background (), "owner" , "repo" , 8 )
165+ if err != nil {
166+ t .Fatalf ("GetIssue() error = %v" , err )
167+ }
168+ if issue .Title != "Public issue" {
169+ t .Fatalf ("GetIssue() title = %q, want public issue" , issue .Title )
170+ }
171+ }
172+ }
173+
174+ func TestGetPRAndIssue_AuthenticatedErrorsAreAuthoritative (t * testing.T ) {
175+ orig := anonymousAPIBase
176+ anonymousAPIBase = "http://127.0.0.1:1"
177+ defer func () { anonymousAPIBase = orig }()
178+
179+ prErr := & GitHubAPIError {StatusCode : http .StatusForbidden }
180+ issueErr := & GitHubAPIError {StatusCode : http .StatusNotFound }
181+ svc := & Service {client : & stubClient {
182+ getPRFunc : func (context.Context , string , string , int ) (* PR , error ) { return nil , prErr },
183+ getIssueFunc : func (context.Context , string , string , int ) (* Issue , error ) {
184+ return nil , issueErr
185+ },
186+ }}
187+
188+ if _ , err := svc .GetPR (context .Background (), "owner" , "repo" , 7 ); ! errors .Is (err , prErr ) {
189+ t .Fatalf ("GetPR() error = %v, want authenticated 403" , err )
190+ }
191+ if _ , err := svc .GetIssue (context .Background (), "owner" , "repo" , 8 ); ! errors .Is (err , issueErr ) {
192+ t .Fatalf ("GetIssue() error = %v, want authenticated 404" , err )
193+ }
194+ }
195+
196+ func TestGetPRAndIssue_AnonymousStatusIsPreserved (t * testing.T ) {
197+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
198+ if strings .Contains (r .URL .Path , "/pulls/" ) {
199+ w .WriteHeader (http .StatusForbidden )
200+ return
201+ }
202+ w .WriteHeader (http .StatusNotFound )
203+ }))
204+ defer srv .Close ()
205+
206+ orig := anonymousAPIBase
207+ anonymousAPIBase = srv .URL
208+ defer func () { anonymousAPIBase = orig }()
209+
210+ svc := & Service {}
211+ for _ , request := range []struct {
212+ name string
213+ call func () error
214+ status int
215+ }{
216+ {name : "PR" , call : func () error { _ , err := svc .GetPR (t .Context (), "owner" , "repo" , 7 ); return err }, status : http .StatusForbidden },
217+ {name : "issue" , call : func () error { _ , err := svc .GetIssue (t .Context (), "owner" , "repo" , 8 ); return err }, status : http .StatusNotFound },
218+ } {
219+ t .Run (request .name , func (t * testing.T ) {
220+ err := request .call ()
221+ var apiErr * GitHubAPIError
222+ if ! errors .As (err , & apiErr ) || apiErr .StatusCode != request .status {
223+ t .Fatalf ("error = %v, want GitHub API status %d" , err , request .status )
224+ }
225+ })
226+ }
227+ }
228+
136229func TestSortBranchesMainFirst (t * testing.T ) {
137230 tests := []struct {
138231 input []string
@@ -173,17 +266,6 @@ func TestSortBranchesMainFirst(t *testing.T) {
173266 }
174267}
175268
176- func TestGetPR_NilClient (t * testing.T ) {
177- svc := & Service {client : nil }
178- _ , err := svc .GetPR (context .Background (), "owner" , "repo" , 1 )
179- if err == nil {
180- t .Fatal ("expected error when client is nil" )
181- }
182- if ! errors .Is (err , ErrNoClient ) {
183- t .Errorf ("err = %v, want ErrNoClient" , err )
184- }
185- }
186-
187269func TestGetPRFeedback_NilClient (t * testing.T ) {
188270 svc := & Service {client : nil }
189271 _ , err := svc .GetPRFeedback (context .Background (), "owner" , "repo" , 1 )
0 commit comments