| 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 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"net/http" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
|
| 16 |
"github.com/google/go-cmp/cmp" |
| 17 |
) |
| 18 |
|
| 19 |
func TestGitService_GetRef_singleRef(t *testing.T) { |
| 20 |
client, mux, _, teardown := setup() |
| 21 |
defer teardown() |
| 22 |
|
| 23 |
mux.HandleFunc("/repos/o/r/git/ref/heads/b", func(w http.ResponseWriter, r *http.Request) { |
| 24 |
testMethod(t, r, "GET") |
| 25 |
fmt.Fprint(w, ` |
| 26 |
{ |
| 27 |
"ref": "refs/heads/b", |
| 28 |
"url": "https://api.github.com/repos/o/r/git/refs/heads/b", |
| 29 |
"object": { |
| 30 |
"type": "commit", |
| 31 |
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", |
| 32 |
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" |
| 33 |
} |
| 34 |
}`) |
| 35 |
}) |
| 36 |
|
| 37 |
ctx := context.Background() |
| 38 |
ref, _, err := client.Git.GetRef(ctx, "o", "r", "refs/heads/b") |
| 39 |
if err != nil { |
| 40 |
t.Fatalf("Git.GetRef returned error: %v", err) |
| 41 |
} |
| 42 |
|
| 43 |
want := &Reference{ |
| 44 |
Ref: String("refs/heads/b"), |
| 45 |
URL: String("https://api.github.com/repos/o/r/git/refs/heads/b"), |
| 46 |
Object: &GitObject{ |
| 47 |
Type: String("commit"), |
| 48 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 49 |
URL: String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 50 |
}, |
| 51 |
} |
| 52 |
if !cmp.Equal(ref, want) { |
| 53 |
t.Errorf("Git.GetRef returned %+v, want %+v", ref, want) |
| 54 |
} |
| 55 |
|
| 56 |
// without 'refs/' prefix |
| 57 |
if _, _, err := client.Git.GetRef(ctx, "o", "r", "heads/b"); err != nil { |
| 58 |
t.Errorf("Git.GetRef returned error: %v", err) |
| 59 |
} |
| 60 |
|
| 61 |
const methodName = "GetRef" |
| 62 |
testBadOptions(t, methodName, func() (err error) { |
| 63 |
_, _, err = client.Git.GetRef(ctx, "\n", "\n", "\n") |
| 64 |
return err |
| 65 |
}) |
| 66 |
|
| 67 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 68 |
got, resp, err := client.Git.GetRef(ctx, "o", "r", "refs/heads/b") |
| 69 |
if got != nil { |
| 70 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 71 |
} |
| 72 |
return resp, err |
| 73 |
}) |
| 74 |
} |
| 75 |
|
| 76 |
func TestGitService_GetRef_noRefs(t *testing.T) { |
| 77 |
client, mux, _, teardown := setup() |
| 78 |
defer teardown() |
| 79 |
|
| 80 |
mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) { |
| 81 |
testMethod(t, r, "GET") |
| 82 |
w.WriteHeader(http.StatusNotFound) |
| 83 |
}) |
| 84 |
|
| 85 |
ctx := context.Background() |
| 86 |
ref, resp, err := client.Git.GetRef(ctx, "o", "r", "refs/heads/b") |
| 87 |
if err == nil { |
| 88 |
t.Errorf("Expected HTTP 404 response") |
| 89 |
} |
| 90 |
if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { |
| 91 |
t.Errorf("Git.GetRef returned status %d, want %d", got, want) |
| 92 |
} |
| 93 |
if ref != nil { |
| 94 |
t.Errorf("Git.GetRef return %+v, want nil", ref) |
| 95 |
} |
| 96 |
|
| 97 |
const methodName = "GetRef" |
| 98 |
testBadOptions(t, methodName, func() (err error) { |
| 99 |
_, _, err = client.Git.GetRef(ctx, "o", "r", "refs/heads/b") |
| 100 |
return err |
| 101 |
}) |
| 102 |
|
| 103 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 104 |
got, resp, err := client.Git.GetRef(ctx, "o", "r", "refs/heads/b") |
| 105 |
if got != nil { |
| 106 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 107 |
} |
| 108 |
return resp, err |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
func TestGitService_ListMatchingRefs_singleRef(t *testing.T) { |
| 113 |
client, mux, _, teardown := setup() |
| 114 |
defer teardown() |
| 115 |
|
| 116 |
mux.HandleFunc("/repos/o/r/git/matching-refs/heads/b", func(w http.ResponseWriter, r *http.Request) { |
| 117 |
testMethod(t, r, "GET") |
| 118 |
fmt.Fprint(w, ` |
| 119 |
[ |
| 120 |
{ |
| 121 |
"ref": "refs/heads/b", |
| 122 |
"url": "https://api.github.com/repos/o/r/git/refs/heads/b", |
| 123 |
"object": { |
| 124 |
"type": "commit", |
| 125 |
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", |
| 126 |
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" |
| 127 |
} |
| 128 |
} |
| 129 |
]`) |
| 130 |
}) |
| 131 |
|
| 132 |
opts := &ReferenceListOptions{Ref: "refs/heads/b"} |
| 133 |
ctx := context.Background() |
| 134 |
refs, _, err := client.Git.ListMatchingRefs(ctx, "o", "r", opts) |
| 135 |
if err != nil { |
| 136 |
t.Fatalf("Git.ListMatchingRefs returned error: %v", err) |
| 137 |
} |
| 138 |
|
| 139 |
ref := refs[0] |
| 140 |
want := &Reference{ |
| 141 |
Ref: String("refs/heads/b"), |
| 142 |
URL: String("https://api.github.com/repos/o/r/git/refs/heads/b"), |
| 143 |
Object: &GitObject{ |
| 144 |
Type: String("commit"), |
| 145 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 146 |
URL: String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 147 |
}, |
| 148 |
} |
| 149 |
if !cmp.Equal(ref, want) { |
| 150 |
t.Errorf("Git.ListMatchingRefs returned %+v, want %+v", ref, want) |
| 151 |
} |
| 152 |
|
| 153 |
// without 'refs/' prefix |
| 154 |
opts = &ReferenceListOptions{Ref: "heads/b"} |
| 155 |
if _, _, err := client.Git.ListMatchingRefs(ctx, "o", "r", opts); err != nil { |
| 156 |
t.Errorf("Git.ListMatchingRefs returned error: %v", err) |
| 157 |
} |
| 158 |
|
| 159 |
const methodName = "ListMatchingRefs" |
| 160 |
testBadOptions(t, methodName, func() (err error) { |
| 161 |
_, _, err = client.Git.ListMatchingRefs(ctx, "\n", "\n", opts) |
| 162 |
return err |
| 163 |
}) |
| 164 |
|
| 165 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 166 |
got, resp, err := client.Git.ListMatchingRefs(ctx, "o", "r", opts) |
| 167 |
if got != nil { |
| 168 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 169 |
} |
| 170 |
return resp, err |
| 171 |
}) |
| 172 |
} |
| 173 |
|
| 174 |
func TestGitService_ListMatchingRefs_multipleRefs(t *testing.T) { |
| 175 |
client, mux, _, teardown := setup() |
| 176 |
defer teardown() |
| 177 |
|
| 178 |
mux.HandleFunc("/repos/o/r/git/matching-refs/heads/b", func(w http.ResponseWriter, r *http.Request) { |
| 179 |
testMethod(t, r, "GET") |
| 180 |
fmt.Fprint(w, ` |
| 181 |
[ |
| 182 |
{ |
| 183 |
"ref": "refs/heads/booger", |
| 184 |
"url": "https://api.github.com/repos/o/r/git/refs/heads/booger", |
| 185 |
"object": { |
| 186 |
"type": "commit", |
| 187 |
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", |
| 188 |
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" |
| 189 |
} |
| 190 |
}, |
| 191 |
{ |
| 192 |
"ref": "refs/heads/bandsaw", |
| 193 |
"url": "https://api.github.com/repos/o/r/git/refs/heads/bandsaw", |
| 194 |
"object": { |
| 195 |
"type": "commit", |
| 196 |
"sha": "612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac", |
| 197 |
"url": "https://api.github.com/repos/o/r/git/commits/612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac" |
| 198 |
} |
| 199 |
} |
| 200 |
] |
| 201 |
`) |
| 202 |
}) |
| 203 |
|
| 204 |
opts := &ReferenceListOptions{Ref: "refs/heads/b"} |
| 205 |
ctx := context.Background() |
| 206 |
refs, _, err := client.Git.ListMatchingRefs(ctx, "o", "r", opts) |
| 207 |
if err != nil { |
| 208 |
t.Errorf("Git.ListMatchingRefs returned error: %v", err) |
| 209 |
} |
| 210 |
|
| 211 |
want := &Reference{ |
| 212 |
Ref: String("refs/heads/booger"), |
| 213 |
URL: String("https://api.github.com/repos/o/r/git/refs/heads/booger"), |
| 214 |
Object: &GitObject{ |
| 215 |
Type: String("commit"), |
| 216 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 217 |
URL: String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 218 |
}, |
| 219 |
} |
| 220 |
if !cmp.Equal(refs[0], want) { |
| 221 |
t.Errorf("Git.ListMatchingRefs returned %+v, want %+v", refs[0], want) |
| 222 |
} |
| 223 |
|
| 224 |
const methodName = "ListMatchingRefs" |
| 225 |
testBadOptions(t, methodName, func() (err error) { |
| 226 |
_, _, err = client.Git.ListMatchingRefs(ctx, "\n", "\n", opts) |
| 227 |
return err |
| 228 |
}) |
| 229 |
|
| 230 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 231 |
got, resp, err := client.Git.ListMatchingRefs(ctx, "o", "r", opts) |
| 232 |
if got != nil { |
| 233 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 234 |
} |
| 235 |
return resp, err |
| 236 |
}) |
| 237 |
} |
| 238 |
|
| 239 |
func TestGitService_ListMatchingRefs_noRefs(t *testing.T) { |
| 240 |
client, mux, _, teardown := setup() |
| 241 |
defer teardown() |
| 242 |
|
| 243 |
mux.HandleFunc("/repos/o/r/git/matching-refs/heads/b", func(w http.ResponseWriter, r *http.Request) { |
| 244 |
testMethod(t, r, "GET") |
| 245 |
fmt.Fprint(w, "[]") |
| 246 |
}) |
| 247 |
|
| 248 |
opts := &ReferenceListOptions{Ref: "refs/heads/b"} |
| 249 |
ctx := context.Background() |
| 250 |
refs, _, err := client.Git.ListMatchingRefs(ctx, "o", "r", opts) |
| 251 |
if err != nil { |
| 252 |
t.Errorf("Git.ListMatchingRefs returned error: %v", err) |
| 253 |
} |
| 254 |
|
| 255 |
if len(refs) != 0 { |
| 256 |
t.Errorf("Git.ListMatchingRefs returned %+v, want an empty slice", refs) |
| 257 |
} |
| 258 |
|
| 259 |
const methodName = "ListMatchingRefs" |
| 260 |
testBadOptions(t, methodName, func() (err error) { |
| 261 |
_, _, err = client.Git.ListMatchingRefs(ctx, "\n", "\n", opts) |
| 262 |
return err |
| 263 |
}) |
| 264 |
|
| 265 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 266 |
got, resp, err := client.Git.ListMatchingRefs(ctx, "o", "r", opts) |
| 267 |
if got != nil { |
| 268 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 269 |
} |
| 270 |
return resp, err |
| 271 |
}) |
| 272 |
} |
| 273 |
|
| 274 |
func TestGitService_ListMatchingRefs_allRefs(t *testing.T) { |
| 275 |
client, mux, _, teardown := setup() |
| 276 |
defer teardown() |
| 277 |
|
| 278 |
mux.HandleFunc("/repos/o/r/git/matching-refs/", func(w http.ResponseWriter, r *http.Request) { |
| 279 |
testMethod(t, r, "GET") |
| 280 |
fmt.Fprint(w, ` |
| 281 |
[ |
| 282 |
{ |
| 283 |
"ref": "refs/heads/branchA", |
| 284 |
"url": "https://api.github.com/repos/o/r/git/refs/heads/branchA", |
| 285 |
"object": { |
| 286 |
"type": "commit", |
| 287 |
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", |
| 288 |
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" |
| 289 |
} |
| 290 |
}, |
| 291 |
{ |
| 292 |
"ref": "refs/heads/branchB", |
| 293 |
"url": "https://api.github.com/repos/o/r/git/refs/heads/branchB", |
| 294 |
"object": { |
| 295 |
"type": "commit", |
| 296 |
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", |
| 297 |
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" |
| 298 |
} |
| 299 |
} |
| 300 |
]`) |
| 301 |
}) |
| 302 |
|
| 303 |
ctx := context.Background() |
| 304 |
refs, _, err := client.Git.ListMatchingRefs(ctx, "o", "r", nil) |
| 305 |
if err != nil { |
| 306 |
t.Errorf("Git.ListMatchingRefs returned error: %v", err) |
| 307 |
} |
| 308 |
|
| 309 |
want := []*Reference{ |
| 310 |
{ |
| 311 |
Ref: String("refs/heads/branchA"), |
| 312 |
URL: String("https://api.github.com/repos/o/r/git/refs/heads/branchA"), |
| 313 |
Object: &GitObject{ |
| 314 |
Type: String("commit"), |
| 315 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 316 |
URL: String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 317 |
}, |
| 318 |
}, |
| 319 |
{ |
| 320 |
Ref: String("refs/heads/branchB"), |
| 321 |
URL: String("https://api.github.com/repos/o/r/git/refs/heads/branchB"), |
| 322 |
Object: &GitObject{ |
| 323 |
Type: String("commit"), |
| 324 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 325 |
URL: String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 326 |
}, |
| 327 |
}, |
| 328 |
} |
| 329 |
if !cmp.Equal(refs, want) { |
| 330 |
t.Errorf("Git.ListMatchingRefs returned %+v, want %+v", refs, want) |
| 331 |
} |
| 332 |
|
| 333 |
const methodName = "ListMatchingRefs" |
| 334 |
testBadOptions(t, methodName, func() (err error) { |
| 335 |
_, _, err = client.Git.ListMatchingRefs(ctx, "\n", "\n", nil) |
| 336 |
return err |
| 337 |
}) |
| 338 |
|
| 339 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 340 |
got, resp, err := client.Git.ListMatchingRefs(ctx, "o", "r", nil) |
| 341 |
if got != nil { |
| 342 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 343 |
} |
| 344 |
return resp, err |
| 345 |
}) |
| 346 |
} |
| 347 |
|
| 348 |
func TestGitService_ListMatchingRefs_options(t *testing.T) { |
| 349 |
client, mux, _, teardown := setup() |
| 350 |
defer teardown() |
| 351 |
|
| 352 |
mux.HandleFunc("/repos/o/r/git/matching-refs/t", func(w http.ResponseWriter, r *http.Request) { |
| 353 |
testMethod(t, r, "GET") |
| 354 |
testFormValues(t, r, values{"page": "2"}) |
| 355 |
fmt.Fprint(w, `[{"ref": "r"}]`) |
| 356 |
}) |
| 357 |
|
| 358 |
opts := &ReferenceListOptions{Ref: "t", ListOptions: ListOptions{Page: 2}} |
| 359 |
ctx := context.Background() |
| 360 |
refs, _, err := client.Git.ListMatchingRefs(ctx, "o", "r", opts) |
| 361 |
if err != nil { |
| 362 |
t.Errorf("Git.ListMatchingRefs returned error: %v", err) |
| 363 |
} |
| 364 |
|
| 365 |
want := []*Reference{{Ref: String("r")}} |
| 366 |
if !cmp.Equal(refs, want) { |
| 367 |
t.Errorf("Git.ListMatchingRefs returned %+v, want %+v", refs, want) |
| 368 |
} |
| 369 |
|
| 370 |
const methodName = "ListMatchingRefs" |
| 371 |
testBadOptions(t, methodName, func() (err error) { |
| 372 |
_, _, err = client.Git.ListMatchingRefs(ctx, "\n", "\n", opts) |
| 373 |
return err |
| 374 |
}) |
| 375 |
|
| 376 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 377 |
got, resp, err := client.Git.ListMatchingRefs(ctx, "o", "r", opts) |
| 378 |
if got != nil { |
| 379 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 380 |
} |
| 381 |
return resp, err |
| 382 |
}) |
| 383 |
} |
| 384 |
|
| 385 |
func TestGitService_CreateRef(t *testing.T) { |
| 386 |
client, mux, _, teardown := setup() |
| 387 |
defer teardown() |
| 388 |
|
| 389 |
args := &createRefRequest{ |
| 390 |
Ref: String("refs/heads/b"), |
| 391 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 392 |
} |
| 393 |
|
| 394 |
mux.HandleFunc("/repos/o/r/git/refs", func(w http.ResponseWriter, r *http.Request) { |
| 395 |
v := new(createRefRequest) |
| 396 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 397 |
|
| 398 |
testMethod(t, r, "POST") |
| 399 |
if !cmp.Equal(v, args) { |
| 400 |
t.Errorf("Request body = %+v, want %+v", v, args) |
| 401 |
} |
| 402 |
fmt.Fprint(w, ` |
| 403 |
{ |
| 404 |
"ref": "refs/heads/b", |
| 405 |
"url": "https://api.github.com/repos/o/r/git/refs/heads/b", |
| 406 |
"object": { |
| 407 |
"type": "commit", |
| 408 |
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", |
| 409 |
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" |
| 410 |
} |
| 411 |
}`) |
| 412 |
}) |
| 413 |
|
| 414 |
ctx := context.Background() |
| 415 |
ref, _, err := client.Git.CreateRef(ctx, "o", "r", &Reference{ |
| 416 |
Ref: String("refs/heads/b"), |
| 417 |
Object: &GitObject{ |
| 418 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 419 |
}, |
| 420 |
}) |
| 421 |
if err != nil { |
| 422 |
t.Errorf("Git.CreateRef returned error: %v", err) |
| 423 |
} |
| 424 |
|
| 425 |
want := &Reference{ |
| 426 |
Ref: String("refs/heads/b"), |
| 427 |
URL: String("https://api.github.com/repos/o/r/git/refs/heads/b"), |
| 428 |
Object: &GitObject{ |
| 429 |
Type: String("commit"), |
| 430 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 431 |
URL: String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 432 |
}, |
| 433 |
} |
| 434 |
if !cmp.Equal(ref, want) { |
| 435 |
t.Errorf("Git.CreateRef returned %+v, want %+v", ref, want) |
| 436 |
} |
| 437 |
|
| 438 |
// without 'refs/' prefix |
| 439 |
_, _, err = client.Git.CreateRef(ctx, "o", "r", &Reference{ |
| 440 |
Ref: String("heads/b"), |
| 441 |
Object: &GitObject{ |
| 442 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 443 |
}, |
| 444 |
}) |
| 445 |
if err != nil { |
| 446 |
t.Errorf("Git.CreateRef returned error: %v", err) |
| 447 |
} |
| 448 |
|
| 449 |
const methodName = "CreateRef" |
| 450 |
testBadOptions(t, methodName, func() (err error) { |
| 451 |
_, _, err = client.Git.CreateRef(ctx, "\n", "\n", &Reference{ |
| 452 |
Ref: String("refs/heads/b"), |
| 453 |
Object: &GitObject{ |
| 454 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 455 |
}, |
| 456 |
}) |
| 457 |
return err |
| 458 |
}) |
| 459 |
|
| 460 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 461 |
got, resp, err := client.Git.CreateRef(ctx, "o", "r", &Reference{ |
| 462 |
Ref: String("refs/heads/b"), |
| 463 |
Object: &GitObject{ |
| 464 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 465 |
}, |
| 466 |
}) |
| 467 |
if got != nil { |
| 468 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 469 |
} |
| 470 |
return resp, err |
| 471 |
}) |
| 472 |
} |
| 473 |
|
| 474 |
func TestGitService_UpdateRef(t *testing.T) { |
| 475 |
client, mux, _, teardown := setup() |
| 476 |
defer teardown() |
| 477 |
|
| 478 |
args := &updateRefRequest{ |
| 479 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 480 |
Force: Bool(true), |
| 481 |
} |
| 482 |
|
| 483 |
mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) { |
| 484 |
v := new(updateRefRequest) |
| 485 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 486 |
|
| 487 |
testMethod(t, r, "PATCH") |
| 488 |
if !cmp.Equal(v, args) { |
| 489 |
t.Errorf("Request body = %+v, want %+v", v, args) |
| 490 |
} |
| 491 |
fmt.Fprint(w, ` |
| 492 |
{ |
| 493 |
"ref": "refs/heads/b", |
| 494 |
"url": "https://api.github.com/repos/o/r/git/refs/heads/b", |
| 495 |
"object": { |
| 496 |
"type": "commit", |
| 497 |
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", |
| 498 |
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" |
| 499 |
} |
| 500 |
}`) |
| 501 |
}) |
| 502 |
|
| 503 |
ctx := context.Background() |
| 504 |
ref, _, err := client.Git.UpdateRef(ctx, "o", "r", &Reference{ |
| 505 |
Ref: String("refs/heads/b"), |
| 506 |
Object: &GitObject{SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd")}, |
| 507 |
}, true) |
| 508 |
if err != nil { |
| 509 |
t.Errorf("Git.UpdateRef returned error: %v", err) |
| 510 |
} |
| 511 |
|
| 512 |
want := &Reference{ |
| 513 |
Ref: String("refs/heads/b"), |
| 514 |
URL: String("https://api.github.com/repos/o/r/git/refs/heads/b"), |
| 515 |
Object: &GitObject{ |
| 516 |
Type: String("commit"), |
| 517 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 518 |
URL: String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 519 |
}, |
| 520 |
} |
| 521 |
if !cmp.Equal(ref, want) { |
| 522 |
t.Errorf("Git.UpdateRef returned %+v, want %+v", ref, want) |
| 523 |
} |
| 524 |
|
| 525 |
// without 'refs/' prefix |
| 526 |
_, _, err = client.Git.UpdateRef(ctx, "o", "r", &Reference{ |
| 527 |
Ref: String("heads/b"), |
| 528 |
Object: &GitObject{SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd")}, |
| 529 |
}, true) |
| 530 |
if err != nil { |
| 531 |
t.Errorf("Git.UpdateRef returned error: %v", err) |
| 532 |
} |
| 533 |
|
| 534 |
const methodName = "UpdateRef" |
| 535 |
testBadOptions(t, methodName, func() (err error) { |
| 536 |
_, _, err = client.Git.UpdateRef(ctx, "\n", "\n", &Reference{ |
| 537 |
Ref: String("refs/heads/b"), |
| 538 |
Object: &GitObject{SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd")}, |
| 539 |
}, true) |
| 540 |
return err |
| 541 |
}) |
| 542 |
|
| 543 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 544 |
got, resp, err := client.Git.UpdateRef(ctx, "o", "r", &Reference{ |
| 545 |
Ref: String("refs/heads/b"), |
| 546 |
Object: &GitObject{SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd")}, |
| 547 |
}, true) |
| 548 |
if got != nil { |
| 549 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 550 |
} |
| 551 |
return resp, err |
| 552 |
}) |
| 553 |
} |
| 554 |
|
| 555 |
func TestGitService_DeleteRef(t *testing.T) { |
| 556 |
client, mux, _, teardown := setup() |
| 557 |
defer teardown() |
| 558 |
|
| 559 |
mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) { |
| 560 |
testMethod(t, r, "DELETE") |
| 561 |
}) |
| 562 |
|
| 563 |
ctx := context.Background() |
| 564 |
_, err := client.Git.DeleteRef(ctx, "o", "r", "refs/heads/b") |
| 565 |
if err != nil { |
| 566 |
t.Errorf("Git.DeleteRef returned error: %v", err) |
| 567 |
} |
| 568 |
|
| 569 |
// without 'refs/' prefix |
| 570 |
if _, err := client.Git.DeleteRef(ctx, "o", "r", "heads/b"); err != nil { |
| 571 |
t.Errorf("Git.DeleteRef returned error: %v", err) |
| 572 |
} |
| 573 |
|
| 574 |
const methodName = "DeleteRef" |
| 575 |
testBadOptions(t, methodName, func() (err error) { |
| 576 |
_, err = client.Git.DeleteRef(ctx, "\n", "\n", "\n") |
| 577 |
return err |
| 578 |
}) |
| 579 |
|
| 580 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 581 |
return client.Git.DeleteRef(ctx, "o", "r", "refs/heads/b") |
| 582 |
}) |
| 583 |
} |
| 584 |
|
| 585 |
func TestGitService_GetRef_pathEscape(t *testing.T) { |
| 586 |
client, mux, _, teardown := setup() |
| 587 |
defer teardown() |
| 588 |
|
| 589 |
mux.HandleFunc("/repos/o/r/git/ref/heads/b", func(w http.ResponseWriter, r *http.Request) { |
| 590 |
testMethod(t, r, "GET") |
| 591 |
if strings.Contains(r.URL.RawPath, "%2F") { |
| 592 |
t.Errorf("RawPath still contains escaped / as %%2F: %v", r.URL.RawPath) |
| 593 |
} |
| 594 |
fmt.Fprint(w, ` |
| 595 |
{ |
| 596 |
"ref": "refs/heads/b", |
| 597 |
"url": "https://api.github.com/repos/o/r/git/refs/heads/b", |
| 598 |
"object": { |
| 599 |
"type": "commit", |
| 600 |
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", |
| 601 |
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" |
| 602 |
} |
| 603 |
}`) |
| 604 |
}) |
| 605 |
|
| 606 |
ctx := context.Background() |
| 607 |
_, _, err := client.Git.GetRef(ctx, "o", "r", "refs/heads/b") |
| 608 |
if err != nil { |
| 609 |
t.Fatalf("Git.GetRef returned error: %v", err) |
| 610 |
} |
| 611 |
|
| 612 |
const methodName = "GetRef" |
| 613 |
testBadOptions(t, methodName, func() (err error) { |
| 614 |
_, _, err = client.Git.GetRef(ctx, "\n", "\n", "\n") |
| 615 |
return err |
| 616 |
}) |
| 617 |
|
| 618 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 619 |
got, resp, err := client.Git.GetRef(ctx, "o", "r", "refs/heads/b") |
| 620 |
if got != nil { |
| 621 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 622 |
} |
| 623 |
return resp, err |
| 624 |
}) |
| 625 |
} |
| 626 |
|
| 627 |
func TestGitService_UpdateRef_pathEscape(t *testing.T) { |
| 628 |
client, mux, _, teardown := setup() |
| 629 |
defer teardown() |
| 630 |
|
| 631 |
args := &updateRefRequest{ |
| 632 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 633 |
Force: Bool(true), |
| 634 |
} |
| 635 |
|
| 636 |
mux.HandleFunc("/repos/o/r/git/refs/heads/b#1", func(w http.ResponseWriter, r *http.Request) { |
| 637 |
v := new(updateRefRequest) |
| 638 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 639 |
|
| 640 |
testMethod(t, r, "PATCH") |
| 641 |
if !cmp.Equal(v, args) { |
| 642 |
t.Errorf("Request body = %+v, want %+v", v, args) |
| 643 |
} |
| 644 |
fmt.Fprint(w, ` |
| 645 |
{ |
| 646 |
"ref": "refs/heads/b#1", |
| 647 |
"url": "https://api.github.com/repos/o/r/git/refs/heads/b%231", |
| 648 |
"object": { |
| 649 |
"type": "commit", |
| 650 |
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", |
| 651 |
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" |
| 652 |
} |
| 653 |
}`) |
| 654 |
}) |
| 655 |
|
| 656 |
ctx := context.Background() |
| 657 |
ref, _, err := client.Git.UpdateRef(ctx, "o", "r", &Reference{ |
| 658 |
Ref: String("refs/heads/b#1"), |
| 659 |
Object: &GitObject{SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd")}, |
| 660 |
}, true) |
| 661 |
if err != nil { |
| 662 |
t.Errorf("Git.UpdateRef returned error: %v", err) |
| 663 |
} |
| 664 |
|
| 665 |
want := &Reference{ |
| 666 |
Ref: String("refs/heads/b#1"), |
| 667 |
URL: String("https://api.github.com/repos/o/r/git/refs/heads/b%231"), |
| 668 |
Object: &GitObject{ |
| 669 |
Type: String("commit"), |
| 670 |
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 671 |
URL: String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"), |
| 672 |
}, |
| 673 |
} |
| 674 |
if !cmp.Equal(ref, want) { |
| 675 |
t.Errorf("Git.UpdateRef returned %+v, want %+v", ref, want) |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
func TestReference_Marshal(t *testing.T) { |
| 680 |
testJSONMarshal(t, &Reference{}, "{}") |
| 681 |
|
| 682 |
u := &Reference{ |
| 683 |
Ref: String("ref"), |
| 684 |
URL: String("url"), |
| 685 |
Object: &GitObject{ |
| 686 |
Type: String("type"), |
| 687 |
SHA: String("sha"), |
| 688 |
URL: String("url"), |
| 689 |
}, |
| 690 |
NodeID: String("nid"), |
| 691 |
} |
| 692 |
|
| 693 |
want := `{ |
| 694 |
"ref": "ref", |
| 695 |
"url": "url", |
| 696 |
"object": { |
| 697 |
"type": "type", |
| 698 |
"sha": "sha", |
| 699 |
"url": "url" |
| 700 |
}, |
| 701 |
"node_id": "nid" |
| 702 |
}` |
| 703 |
|
| 704 |
testJSONMarshal(t, u, want) |
| 705 |
} |
| 706 |
|
| 707 |
func TestGitObject_Marshal(t *testing.T) { |
| 708 |
testJSONMarshal(t, &GitObject{}, "{}") |
| 709 |
|
| 710 |
u := &GitObject{ |
| 711 |
Type: String("type"), |
| 712 |
SHA: String("sha"), |
| 713 |
URL: String("url"), |
| 714 |
} |
| 715 |
|
| 716 |
want := `{ |
| 717 |
"type": "type", |
| 718 |
"sha": "sha", |
| 719 |
"url": "url" |
| 720 |
}` |
| 721 |
|
| 722 |
testJSONMarshal(t, u, want) |
| 723 |
} |
| 724 |
|
| 725 |
func TestCreateRefRequest_Marshal(t *testing.T) { |
| 726 |
testJSONMarshal(t, &createRefRequest{}, "{}") |
| 727 |
|
| 728 |
u := &createRefRequest{ |
| 729 |
Ref: String("ref"), |
| 730 |
SHA: String("sha"), |
| 731 |
} |
| 732 |
|
| 733 |
want := `{ |
| 734 |
"ref": "ref", |
| 735 |
"sha": "sha" |
| 736 |
}` |
| 737 |
|
| 738 |
testJSONMarshal(t, u, want) |
| 739 |
} |
| 740 |
|
| 741 |
func TestUpdateRefRequest_Marshal(t *testing.T) { |
| 742 |
testJSONMarshal(t, &updateRefRequest{}, "{}") |
| 743 |
|
| 744 |
u := &updateRefRequest{ |
| 745 |
SHA: String("sha"), |
| 746 |
Force: Bool(true), |
| 747 |
} |
| 748 |
|
| 749 |
want := `{ |
| 750 |
"sha": "sha", |
| 751 |
"force": true |
| 752 |
}` |
| 753 |
|
| 754 |
testJSONMarshal(t, u, want) |
| 755 |
} |