| 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 |
"testing" |
| 13 |
|
| 14 |
"github.com/google/go-cmp/cmp" |
| 15 |
) |
| 16 |
|
| 17 |
func TestRepositoriesService_ListForks(t *testing.T) { |
| 18 |
client, mux, _, teardown := setup() |
| 19 |
defer teardown() |
| 20 |
|
| 21 |
mux.HandleFunc("/repos/o/r/forks", func(w http.ResponseWriter, r *http.Request) { |
| 22 |
testMethod(t, r, "GET") |
| 23 |
testHeader(t, r, "Accept", mediaTypeTopicsPreview) |
| 24 |
testFormValues(t, r, values{ |
| 25 |
"sort": "newest", |
| 26 |
"page": "3", |
| 27 |
}) |
| 28 |
fmt.Fprint(w, `[{"id":1},{"id":2}]`) |
| 29 |
}) |
| 30 |
|
| 31 |
opt := &RepositoryListForksOptions{ |
| 32 |
Sort: "newest", |
| 33 |
ListOptions: ListOptions{Page: 3}, |
| 34 |
} |
| 35 |
ctx := context.Background() |
| 36 |
repos, _, err := client.Repositories.ListForks(ctx, "o", "r", opt) |
| 37 |
if err != nil { |
| 38 |
t.Errorf("Repositories.ListForks returned error: %v", err) |
| 39 |
} |
| 40 |
|
| 41 |
want := []*Repository{{ID: Int64(1)}, {ID: Int64(2)}} |
| 42 |
if !cmp.Equal(repos, want) { |
| 43 |
t.Errorf("Repositories.ListForks returned %+v, want %+v", repos, want) |
| 44 |
} |
| 45 |
|
| 46 |
const methodName = "ListForks" |
| 47 |
testBadOptions(t, methodName, func() (err error) { |
| 48 |
_, _, err = client.Repositories.ListForks(ctx, "\n", "\n", opt) |
| 49 |
return err |
| 50 |
}) |
| 51 |
|
| 52 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 53 |
got, resp, err := client.Repositories.ListForks(ctx, "o", "r", opt) |
| 54 |
if got != nil { |
| 55 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 56 |
} |
| 57 |
return resp, err |
| 58 |
}) |
| 59 |
} |
| 60 |
|
| 61 |
func TestRepositoriesService_ListForks_invalidOwner(t *testing.T) { |
| 62 |
client, _, _, teardown := setup() |
| 63 |
defer teardown() |
| 64 |
|
| 65 |
ctx := context.Background() |
| 66 |
_, _, err := client.Repositories.ListForks(ctx, "%", "r", nil) |
| 67 |
testURLParseError(t, err) |
| 68 |
} |
| 69 |
|
| 70 |
func TestRepositoriesService_CreateFork(t *testing.T) { |
| 71 |
client, mux, _, teardown := setup() |
| 72 |
defer teardown() |
| 73 |
|
| 74 |
mux.HandleFunc("/repos/o/r/forks", func(w http.ResponseWriter, r *http.Request) { |
| 75 |
testMethod(t, r, "POST") |
| 76 |
testBody(t, r, `{"organization":"o","name":"n","default_branch_only":true}`+"\n") |
| 77 |
fmt.Fprint(w, `{"id":1}`) |
| 78 |
}) |
| 79 |
|
| 80 |
opt := &RepositoryCreateForkOptions{Organization: "o", Name: "n", DefaultBranchOnly: true} |
| 81 |
ctx := context.Background() |
| 82 |
repo, _, err := client.Repositories.CreateFork(ctx, "o", "r", opt) |
| 83 |
if err != nil { |
| 84 |
t.Errorf("Repositories.CreateFork returned error: %v", err) |
| 85 |
} |
| 86 |
|
| 87 |
want := &Repository{ID: Int64(1)} |
| 88 |
if !cmp.Equal(repo, want) { |
| 89 |
t.Errorf("Repositories.CreateFork returned %+v, want %+v", repo, want) |
| 90 |
} |
| 91 |
|
| 92 |
const methodName = "CreateFork" |
| 93 |
testBadOptions(t, methodName, func() (err error) { |
| 94 |
_, _, err = client.Repositories.CreateFork(ctx, "\n", "\n", opt) |
| 95 |
return err |
| 96 |
}) |
| 97 |
|
| 98 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 99 |
got, resp, err := client.Repositories.CreateFork(ctx, "o", "r", opt) |
| 100 |
if got != nil { |
| 101 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 102 |
} |
| 103 |
return resp, err |
| 104 |
}) |
| 105 |
} |
| 106 |
|
| 107 |
func TestRepositoriesService_CreateFork_deferred(t *testing.T) { |
| 108 |
client, mux, _, teardown := setup() |
| 109 |
defer teardown() |
| 110 |
|
| 111 |
mux.HandleFunc("/repos/o/r/forks", func(w http.ResponseWriter, r *http.Request) { |
| 112 |
testMethod(t, r, "POST") |
| 113 |
testBody(t, r, `{"organization":"o","name":"n","default_branch_only":true}`+"\n") |
| 114 |
// This response indicates the fork will happen asynchronously. |
| 115 |
w.WriteHeader(http.StatusAccepted) |
| 116 |
fmt.Fprint(w, `{"id":1}`) |
| 117 |
}) |
| 118 |
|
| 119 |
opt := &RepositoryCreateForkOptions{Organization: "o", Name: "n", DefaultBranchOnly: true} |
| 120 |
ctx := context.Background() |
| 121 |
repo, _, err := client.Repositories.CreateFork(ctx, "o", "r", opt) |
| 122 |
if _, ok := err.(*AcceptedError); !ok { |
| 123 |
t.Errorf("Repositories.CreateFork returned error: %v (want AcceptedError)", err) |
| 124 |
} |
| 125 |
|
| 126 |
want := &Repository{ID: Int64(1)} |
| 127 |
if !cmp.Equal(repo, want) { |
| 128 |
t.Errorf("Repositories.CreateFork returned %+v, want %+v", repo, want) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func TestRepositoriesService_CreateFork_invalidOwner(t *testing.T) { |
| 133 |
client, _, _, teardown := setup() |
| 134 |
defer teardown() |
| 135 |
|
| 136 |
ctx := context.Background() |
| 137 |
_, _, err := client.Repositories.CreateFork(ctx, "%", "r", nil) |
| 138 |
testURLParseError(t, err) |
| 139 |
} |