github.com/google/go-github/v33@v33.0.0/github/repos_forks_test.go (about) 1 // Copyright 2013 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 "net/http" 12 "reflect" 13 "testing" 14 ) 15 16 func TestRepositoriesService_ListForks(t *testing.T) { 17 client, mux, _, teardown := setup() 18 defer teardown() 19 20 mux.HandleFunc("/repos/o/r/forks", func(w http.ResponseWriter, r *http.Request) { 21 testMethod(t, r, "GET") 22 testHeader(t, r, "Accept", mediaTypeTopicsPreview) 23 testFormValues(t, r, values{ 24 "sort": "newest", 25 "page": "3", 26 }) 27 fmt.Fprint(w, `[{"id":1},{"id":2}]`) 28 }) 29 30 opt := &RepositoryListForksOptions{ 31 Sort: "newest", 32 ListOptions: ListOptions{Page: 3}, 33 } 34 repos, _, err := client.Repositories.ListForks(context.Background(), "o", "r", opt) 35 if err != nil { 36 t.Errorf("Repositories.ListForks returned error: %v", err) 37 } 38 39 want := []*Repository{{ID: Int64(1)}, {ID: Int64(2)}} 40 if !reflect.DeepEqual(repos, want) { 41 t.Errorf("Repositories.ListForks returned %+v, want %+v", repos, want) 42 } 43 } 44 45 func TestRepositoriesService_ListForks_invalidOwner(t *testing.T) { 46 client, _, _, teardown := setup() 47 defer teardown() 48 49 _, _, err := client.Repositories.ListForks(context.Background(), "%", "r", nil) 50 testURLParseError(t, err) 51 } 52 53 func TestRepositoriesService_CreateFork(t *testing.T) { 54 client, mux, _, teardown := setup() 55 defer teardown() 56 57 mux.HandleFunc("/repos/o/r/forks", func(w http.ResponseWriter, r *http.Request) { 58 testMethod(t, r, "POST") 59 testFormValues(t, r, values{"organization": "o"}) 60 fmt.Fprint(w, `{"id":1}`) 61 }) 62 63 opt := &RepositoryCreateForkOptions{Organization: "o"} 64 repo, _, err := client.Repositories.CreateFork(context.Background(), "o", "r", opt) 65 if err != nil { 66 t.Errorf("Repositories.CreateFork returned error: %v", err) 67 } 68 69 want := &Repository{ID: Int64(1)} 70 if !reflect.DeepEqual(repo, want) { 71 t.Errorf("Repositories.CreateFork returned %+v, want %+v", repo, want) 72 } 73 } 74 75 func TestRepositoriesService_CreateFork_deferred(t *testing.T) { 76 client, mux, _, teardown := setup() 77 defer teardown() 78 79 mux.HandleFunc("/repos/o/r/forks", func(w http.ResponseWriter, r *http.Request) { 80 testMethod(t, r, "POST") 81 testFormValues(t, r, values{"organization": "o"}) 82 // This response indicates the fork will happen asynchronously. 83 w.WriteHeader(http.StatusAccepted) 84 fmt.Fprint(w, `{"id":1}`) 85 }) 86 87 opt := &RepositoryCreateForkOptions{Organization: "o"} 88 repo, _, err := client.Repositories.CreateFork(context.Background(), "o", "r", opt) 89 if _, ok := err.(*AcceptedError); !ok { 90 t.Errorf("Repositories.CreateFork returned error: %v (want AcceptedError)", err) 91 } 92 93 want := &Repository{ID: Int64(1)} 94 if !reflect.DeepEqual(repo, want) { 95 t.Errorf("Repositories.CreateFork returned %+v, want %+v", repo, want) 96 } 97 } 98 99 func TestRepositoriesService_CreateFork_invalidOwner(t *testing.T) { 100 client, _, _, teardown := setup() 101 defer teardown() 102 103 _, _, err := client.Repositories.CreateFork(context.Background(), "%", "r", nil) 104 testURLParseError(t, err) 105 }