| 1 |
// Copyright 2021 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 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"net/http" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/google/go-cmp/cmp" |
| 16 |
) |
| 17 |
|
| 18 |
func TestRepositoriesService_ListAutolinks(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/repos/o/r/autolinks", func(w http.ResponseWriter, r *http.Request) { |
| 23 |
testMethod(t, r, "GET") |
| 24 |
testFormValues(t, r, values{"page": "2"}) |
| 25 |
fmt.Fprintf(w, `[{"id":1, "key_prefix": "TICKET-", "url_template": "https://example.com/TICKET?query=<num>"}, {"id":2, "key_prefix": "STORY-", "url_template": "https://example.com/STORY?query=<num>"}]`) |
| 26 |
}) |
| 27 |
|
| 28 |
opt := &ListOptions{ |
| 29 |
Page: 2, |
| 30 |
} |
| 31 |
ctx := context.Background() |
| 32 |
autolinks, _, err := client.Repositories.ListAutolinks(ctx, "o", "r", opt) |
| 33 |
if err != nil { |
| 34 |
t.Errorf("Repositories.ListAutolinks returned error: %v", err) |
| 35 |
} |
| 36 |
|
| 37 |
want := []*Autolink{ |
| 38 |
{ID: Int64(1), KeyPrefix: String("TICKET-"), URLTemplate: String("https://example.com/TICKET?query=<num>")}, |
| 39 |
{ID: Int64(2), KeyPrefix: String("STORY-"), URLTemplate: String("https://example.com/STORY?query=<num>")}, |
| 40 |
} |
| 41 |
|
| 42 |
if !cmp.Equal(autolinks, want) { |
| 43 |
t.Errorf("Repositories.ListAutolinks returned %+v, want %+v", autolinks, want) |
| 44 |
} |
| 45 |
|
| 46 |
const methodName = "ListAutolinks" |
| 47 |
testBadOptions(t, methodName, func() (err error) { |
| 48 |
_, _, err = client.Repositories.ListAutolinks(ctx, "\n", "\n", opt) |
| 49 |
return err |
| 50 |
}) |
| 51 |
|
| 52 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 53 |
got, resp, err := client.Repositories.ListAutolinks(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_AddAutolink(t *testing.T) { |
| 62 |
client, mux, _, teardown := setup() |
| 63 |
defer teardown() |
| 64 |
|
| 65 |
opt := &AutolinkOptions{ |
| 66 |
KeyPrefix: String("TICKET-"), |
| 67 |
URLTemplate: String("https://example.com/TICKET?query=<num>"), |
| 68 |
IsAlphanumeric: Bool(true), |
| 69 |
} |
| 70 |
mux.HandleFunc("/repos/o/r/autolinks", func(w http.ResponseWriter, r *http.Request) { |
| 71 |
v := new(AutolinkOptions) |
| 72 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 73 |
testMethod(t, r, "POST") |
| 74 |
if !cmp.Equal(v, opt) { |
| 75 |
t.Errorf("Request body = %+v, want %+v", v, opt) |
| 76 |
} |
| 77 |
w.WriteHeader(http.StatusOK) |
| 78 |
assertWrite(t, w, []byte(` |
| 79 |
{ |
| 80 |
"key_prefix": "TICKET-", |
| 81 |
"url_template": "https://example.com/TICKET?query=<num>", |
| 82 |
"is_alphanumeric": true |
| 83 |
} |
| 84 |
`)) |
| 85 |
}) |
| 86 |
ctx := context.Background() |
| 87 |
autolink, _, err := client.Repositories.AddAutolink(ctx, "o", "r", opt) |
| 88 |
if err != nil { |
| 89 |
t.Errorf("Repositories.AddAutolink returned error: %v", err) |
| 90 |
} |
| 91 |
want := &Autolink{ |
| 92 |
KeyPrefix: String("TICKET-"), |
| 93 |
URLTemplate: String("https://example.com/TICKET?query=<num>"), |
| 94 |
IsAlphanumeric: Bool(true), |
| 95 |
} |
| 96 |
|
| 97 |
if !cmp.Equal(autolink, want) { |
| 98 |
t.Errorf("AddAutolink returned %+v, want %+v", autolink, want) |
| 99 |
} |
| 100 |
|
| 101 |
const methodName = "AddAutolink" |
| 102 |
testBadOptions(t, methodName, func() (err error) { |
| 103 |
_, _, err = client.Repositories.AddAutolink(ctx, "\n", "\n", opt) |
| 104 |
return err |
| 105 |
}) |
| 106 |
|
| 107 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 108 |
got, resp, err := client.Repositories.AddAutolink(ctx, "o", "r", opt) |
| 109 |
if got != nil { |
| 110 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 111 |
} |
| 112 |
return resp, err |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
func TestRepositoriesService_GetAutolink(t *testing.T) { |
| 117 |
client, mux, _, teardown := setup() |
| 118 |
defer teardown() |
| 119 |
|
| 120 |
mux.HandleFunc("/repos/o/r/autolinks/1", func(w http.ResponseWriter, r *http.Request) { |
| 121 |
testMethod(t, r, "GET") |
| 122 |
fmt.Fprintf(w, `{"id":1, "key_prefix": "TICKET-", "url_template": "https://example.com/TICKET?query=<num>"}`) |
| 123 |
}) |
| 124 |
|
| 125 |
ctx := context.Background() |
| 126 |
autolink, _, err := client.Repositories.GetAutolink(ctx, "o", "r", 1) |
| 127 |
if err != nil { |
| 128 |
t.Errorf("Repositories.GetAutolink returned error: %v", err) |
| 129 |
} |
| 130 |
|
| 131 |
want := &Autolink{ID: Int64(1), KeyPrefix: String("TICKET-"), URLTemplate: String("https://example.com/TICKET?query=<num>")} |
| 132 |
if !cmp.Equal(autolink, want) { |
| 133 |
t.Errorf("Repositories.GetAutolink returned %+v, want %+v", autolink, want) |
| 134 |
} |
| 135 |
|
| 136 |
const methodName = "GetAutolink" |
| 137 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 138 |
got, resp, err := client.Repositories.GetAutolink(ctx, "o", "r", 2) |
| 139 |
if got != nil { |
| 140 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 141 |
} |
| 142 |
return resp, err |
| 143 |
}) |
| 144 |
} |
| 145 |
|
| 146 |
func TestRepositoriesService_DeleteAutolink(t *testing.T) { |
| 147 |
client, mux, _, teardown := setup() |
| 148 |
defer teardown() |
| 149 |
|
| 150 |
mux.HandleFunc("/repos/o/r/autolinks/1", func(w http.ResponseWriter, r *http.Request) { |
| 151 |
testMethod(t, r, "DELETE") |
| 152 |
w.WriteHeader(http.StatusNoContent) |
| 153 |
}) |
| 154 |
|
| 155 |
ctx := context.Background() |
| 156 |
_, err := client.Repositories.DeleteAutolink(ctx, "o", "r", 1) |
| 157 |
if err != nil { |
| 158 |
t.Errorf("Repositories.DeleteAutolink returned error: %v", err) |
| 159 |
} |
| 160 |
|
| 161 |
const methodName = "DeleteAutolink" |
| 162 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 163 |
return client.Repositories.DeleteAutolink(ctx, "o", "r", 2) |
| 164 |
}) |
| 165 |
} |
| 166 |
|
| 167 |
func TestAutolinkOptions_Marshal(t *testing.T) { |
| 168 |
testJSONMarshal(t, &AutolinkOptions{}, "{}") |
| 169 |
|
| 170 |
r := &AutolinkOptions{ |
| 171 |
KeyPrefix: String("kp"), |
| 172 |
URLTemplate: String("URLT"), |
| 173 |
IsAlphanumeric: Bool(true), |
| 174 |
} |
| 175 |
|
| 176 |
want := `{ |
| 177 |
"key_prefix": "kp", |
| 178 |
"url_template": "URLT", |
| 179 |
"is_alphanumeric": true |
| 180 |
}` |
| 181 |
|
| 182 |
testJSONMarshal(t, r, want) |
| 183 |
} |
| 184 |
|
| 185 |
func TestAutolink_Marshal(t *testing.T) { |
| 186 |
testJSONMarshal(t, &Autolink{}, "{}") |
| 187 |
|
| 188 |
r := &Autolink{ |
| 189 |
ID: Int64(1), |
| 190 |
KeyPrefix: String("kp"), |
| 191 |
URLTemplate: String("URLT"), |
| 192 |
IsAlphanumeric: Bool(true), |
| 193 |
} |
| 194 |
|
| 195 |
want := `{ |
| 196 |
"id": 1, |
| 197 |
"key_prefix": "kp", |
| 198 |
"url_template": "URLT", |
| 199 |
"is_alphanumeric": true |
| 200 |
}` |
| 201 |
|
| 202 |
testJSONMarshal(t, r, want) |
| 203 |
} |