@@ -126,17 +126,72 @@ func TestRepositoriesService_GetReadme(t *testing.T) {
126126 })
127127}
128128
129- func TestRepositoriesService_DownloadContents_Success (t * testing.T ) {
129+ func TestRepositoriesService_DownloadContents_SuccessWithContent (t * testing.T ) {
130130 t .Parallel ()
131- client , mux , _ := setup (t )
131+ client , mux , serverURL := setup (t )
132132
133133 mux .HandleFunc ("/repos/o/r/contents/d/f" , func (w http.ResponseWriter , r * http.Request ) {
134134 testMethod (t , r , "GET" )
135- fmt .Fprint (w , `{
135+ fmt .Fprintf (w , `{
136136 "type": "file",
137137 "name": "f",
138- "content": "foo"
139- }` )
138+ "content": "foo",
139+ "download_url": "%v/download/f"
140+ }` , serverURL + baseURLPath )
141+ })
142+
143+ ctx := t .Context ()
144+ r , resp , err := client .Repositories .DownloadContents (ctx , "o" , "r" , "d/f" , nil )
145+ if err != nil {
146+ t .Errorf ("Repositories.DownloadContents returned error: %v" , err )
147+ }
148+
149+ if got , want := resp .Response .StatusCode , http .StatusOK ; got != want {
150+ t .Errorf ("Repositories.DownloadContents returned status code %v, want %v" , got , want )
151+ }
152+
153+ bytes , err := io .ReadAll (r )
154+ if err != nil {
155+ t .Errorf ("Error reading response body: %v" , err )
156+ }
157+ r .Close ()
158+
159+ if got , want := string (bytes ), "foo" ; got != want {
160+ t .Errorf ("Repositories.DownloadContents returned %v, want %v" , got , want )
161+ }
162+
163+ const methodName = "DownloadContents"
164+ testBadOptions (t , methodName , func () (err error ) {
165+ _ , _ , err = client .Repositories .DownloadContents (ctx , "\n " , "\n " , "\n " , nil )
166+ return err
167+ })
168+
169+ testNewRequestAndDoFailure (t , methodName , client , func () (* Response , error ) {
170+ got , resp , err := client .Repositories .DownloadContents (ctx , "o" , "r" , "d/f" , nil )
171+ if got != nil {
172+ t .Errorf ("testNewRequestAndDoFailure %v = %#v, want nil" , methodName , got )
173+ }
174+ return resp , err
175+ })
176+ }
177+
178+ func TestRepositoriesService_DownloadContents_SuccessByDownload (t * testing.T ) {
179+ t .Parallel ()
180+ client , mux , serverURL := setup (t )
181+
182+ mux .HandleFunc ("/repos/o/r/contents/d/f" , func (w http.ResponseWriter , r * http.Request ) {
183+ testMethod (t , r , "GET" )
184+ fmt .Fprintf (w , `{
185+ "type": "file",
186+ "name": "f",
187+ "content": "",
188+ "download_url": "%v/download/f"
189+ }` , serverURL + baseURLPath )
190+ })
191+
192+ mux .HandleFunc ("/download/f" , func (w http.ResponseWriter , r * http.Request ) {
193+ testMethod (t , r , "GET" )
194+ fmt .Fprint (w , "foo" )
140195 })
141196
142197 ctx := t .Context ()
@@ -174,6 +229,46 @@ func TestRepositoriesService_DownloadContents_Success(t *testing.T) {
174229 })
175230}
176231
232+ func TestRepositoriesService_DownloadContents_FailedResponse (t * testing.T ) {
233+ t .Parallel ()
234+ client , mux , serverURL := setup (t )
235+
236+ mux .HandleFunc ("/repos/o/r/contents/d/f" , func (w http.ResponseWriter , r * http.Request ) {
237+ testMethod (t , r , "GET" )
238+ fmt .Fprintf (w , `{
239+ "type": "file",
240+ "name": "f",
241+ "content": "",
242+ "download_url": "%v/download/f"
243+ }` , serverURL + baseURLPath )
244+ })
245+ mux .HandleFunc ("/download/f" , func (w http.ResponseWriter , r * http.Request ) {
246+ testMethod (t , r , "GET" )
247+ w .WriteHeader (http .StatusInternalServerError )
248+ fmt .Fprint (w , "foo error" )
249+ })
250+
251+ ctx := t .Context ()
252+ r , resp , err := client .Repositories .DownloadContents (ctx , "o" , "r" , "d/f" , nil )
253+ if err != nil {
254+ t .Errorf ("Repositories.DownloadContents returned error: %v" , err )
255+ }
256+
257+ if got , want := resp .Response .StatusCode , http .StatusInternalServerError ; got != want {
258+ t .Errorf ("Repositories.DownloadContents returned status code %v, want %v" , got , want )
259+ }
260+
261+ bytes , err := io .ReadAll (r )
262+ if err != nil {
263+ t .Errorf ("Error reading response body: %v" , err )
264+ }
265+ r .Close ()
266+
267+ if got , want := string (bytes ), "foo error" ; got != want {
268+ t .Errorf ("Repositories.DownloadContents returned %v, want %v" , got , want )
269+ }
270+ }
271+
177272func TestRepositoriesService_DownloadContents_NoDownloadURL (t * testing.T ) {
178273 t .Parallel ()
179274 client , mux , _ := setup (t )
@@ -182,7 +277,8 @@ func TestRepositoriesService_DownloadContents_NoDownloadURL(t *testing.T) {
182277 testMethod (t , r , "GET" )
183278 fmt .Fprint (w , `{
184279 "type": "file",
185- "name": "f"
280+ "name": "f",
281+ "content": ""
186282}` )
187283 })
188284
@@ -197,11 +293,11 @@ func TestRepositoriesService_DownloadContents_NoDownloadURL(t *testing.T) {
197293 }
198294
199295 if reader != nil {
200- t .Error ("Repositories.DownloadContents returned unexpected reader" )
296+ t .Error ("Repositories.DownloadContents did not return expected reader" )
201297 }
202298}
203299
204- func TestRepositoriesService_DownloadContents_GetContentsError (t * testing.T ) {
300+ func TestRepositoriesService_DownloadContents_NoFile (t * testing.T ) {
205301 t .Parallel ()
206302 client , mux , _ := setup (t )
207303
@@ -221,21 +317,22 @@ func TestRepositoriesService_DownloadContents_GetContentsError(t *testing.T) {
221317 }
222318
223319 if reader != nil {
224- t .Error ("Repositories.DownloadContents returned unexpected reader" )
320+ t .Error ("Repositories.DownloadContents did not return expected reader" )
225321 }
226322}
227323
228324func TestRepositoriesService_DownloadContentsWithMeta_SuccessWithContent (t * testing.T ) {
229325 t .Parallel ()
230- client , mux , _ := setup (t )
326+ client , mux , serverURL := setup (t )
231327
232328 mux .HandleFunc ("/repos/o/r/contents/d/f" , func (w http.ResponseWriter , r * http.Request ) {
233329 testMethod (t , r , "GET" )
234- fmt .Fprint (w , `{
330+ fmt .Fprintf (w , `{
235331 "type": "file",
236332 "name": "f",
237- "content": "foo"
238- }` )
333+ "content": "foo",
334+ "download_url": "%v/download/f"
335+ }` , serverURL + baseURLPath )
239336 })
240337
241338 ctx := t .Context ()
@@ -284,7 +381,7 @@ func TestRepositoriesService_DownloadContentsWithMeta_SuccessWithContent(t *test
284381 })
285382}
286383
287- func TestRepositoriesService_DownloadContentsWithMeta_SuccessViaDownloadURL (t * testing.T ) {
384+ func TestRepositoriesService_DownloadContentsWithMeta_SuccessByDownload (t * testing.T ) {
288385 t .Parallel ()
289386 client , mux , serverURL := setup (t )
290387
@@ -296,6 +393,7 @@ func TestRepositoriesService_DownloadContentsWithMeta_SuccessViaDownloadURL(t *t
296393 "download_url": "%v/download/f"
297394}` , serverURL + baseURLPath )
298395 })
396+
299397 mux .HandleFunc ("/download/f" , func (w http.ResponseWriter , r * http.Request ) {
300398 testMethod (t , r , "GET" )
301399 fmt .Fprint (w , "foo" )
@@ -385,7 +483,8 @@ func TestRepositoriesService_DownloadContentsWithMeta_NoDownloadURL(t *testing.T
385483 testMethod (t , r , "GET" )
386484 fmt .Fprint (w , `{
387485 "type": "file",
388- "name": "f"
486+ "name": "f",
487+ "content": ""
389488}` )
390489 })
391490
@@ -396,7 +495,7 @@ func TestRepositoriesService_DownloadContentsWithMeta_NoDownloadURL(t *testing.T
396495 }
397496
398497 if reader != nil {
399- t .Error ("Repositories.DownloadContentsWithMeta returned unexpected reader" )
498+ t .Error ("Repositories.DownloadContentsWithMeta did not return expected reader" )
400499 }
401500
402501 if resp == nil {
@@ -408,7 +507,7 @@ func TestRepositoriesService_DownloadContentsWithMeta_NoDownloadURL(t *testing.T
408507 }
409508}
410509
411- func TestRepositoriesService_DownloadContentsWithMeta_GetContentsError (t * testing.T ) {
510+ func TestRepositoriesService_DownloadContentsWithMeta_NoFile (t * testing.T ) {
412511 t .Parallel ()
413512 client , mux , _ := setup (t )
414513
@@ -418,53 +517,14 @@ func TestRepositoriesService_DownloadContentsWithMeta_GetContentsError(t *testin
418517 })
419518
420519 ctx := t .Context ()
421- reader , contents , resp , err := client .Repositories .DownloadContentsWithMeta (ctx , "o" , "r" , "d/f" , nil )
422- if err == nil {
423- t .Error ("Repositories.DownloadContentsWithMeta did not return expected error" )
424- }
425-
426- if reader != nil {
427- t .Error ("Repositories.DownloadContentsWithMeta returned unexpected reader" )
428- }
429-
430- if resp == nil {
431- t .Error ("Repositories.DownloadContentsWithMeta did not return expected response" )
432- }
433-
434- if contents != nil {
435- t .Error ("Repositories.DownloadContentsWithMeta returned unexpected content" )
436- }
437- }
438-
439- func TestRepositoriesService_DownloadContentsWithMeta_NilFileContent (t * testing.T ) {
440- t .Parallel ()
441- client , mux , _ := setup (t )
442-
443- mux .HandleFunc ("/repos/o/r/contents/d" , func (w http.ResponseWriter , r * http.Request ) {
444- testMethod (t , r , "GET" )
445- fmt .Fprint (w , `[{
446- "type": "file",
447- "name": "f"
448- }]` )
449- })
450-
451- ctx := t .Context ()
452- reader , contents , resp , err := client .Repositories .DownloadContentsWithMeta (ctx , "o" , "r" , "d" , nil )
520+ _ , _ , resp , err := client .Repositories .DownloadContentsWithMeta (ctx , "o" , "r" , "d/f" , nil )
453521 if err == nil {
454522 t .Error ("Repositories.DownloadContentsWithMeta did not return expected error" )
455523 }
456524
457- if reader != nil {
458- t .Error ("Repositories.DownloadContentsWithMeta returned unexpected reader" )
459- }
460-
461525 if resp == nil {
462526 t .Error ("Repositories.DownloadContentsWithMeta did not return expected response" )
463527 }
464-
465- if contents != nil {
466- t .Error ("Repositories.DownloadContentsWithMeta returned unexpected content" )
467- }
468528}
469529
470530func TestRepositoriesService_GetContents_File (t * testing.T ) {
0 commit comments