github.com/google/go-github/v33@v33.0.0/github/repos_statuses_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 "encoding/json" 11 "fmt" 12 "net/http" 13 "reflect" 14 "testing" 15 ) 16 17 func TestRepositoriesService_ListStatuses(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 mux.HandleFunc("/repos/o/r/commits/r/statuses", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 testFormValues(t, r, values{"page": "2"}) 24 fmt.Fprint(w, `[{"id":1}]`) 25 }) 26 27 opt := &ListOptions{Page: 2} 28 statuses, _, err := client.Repositories.ListStatuses(context.Background(), "o", "r", "r", opt) 29 if err != nil { 30 t.Errorf("Repositories.ListStatuses returned error: %v", err) 31 } 32 33 want := []*RepoStatus{{ID: Int64(1)}} 34 if !reflect.DeepEqual(statuses, want) { 35 t.Errorf("Repositories.ListStatuses returned %+v, want %+v", statuses, want) 36 } 37 } 38 39 func TestRepositoriesService_ListStatuses_invalidOwner(t *testing.T) { 40 client, _, _, teardown := setup() 41 defer teardown() 42 43 _, _, err := client.Repositories.ListStatuses(context.Background(), "%", "r", "r", nil) 44 testURLParseError(t, err) 45 } 46 47 func TestRepositoriesService_CreateStatus(t *testing.T) { 48 client, mux, _, teardown := setup() 49 defer teardown() 50 51 input := &RepoStatus{State: String("s"), TargetURL: String("t"), Description: String("d")} 52 53 mux.HandleFunc("/repos/o/r/statuses/r", func(w http.ResponseWriter, r *http.Request) { 54 v := new(RepoStatus) 55 json.NewDecoder(r.Body).Decode(v) 56 57 testMethod(t, r, "POST") 58 if !reflect.DeepEqual(v, input) { 59 t.Errorf("Request body = %+v, want %+v", v, input) 60 } 61 fmt.Fprint(w, `{"id":1}`) 62 }) 63 64 status, _, err := client.Repositories.CreateStatus(context.Background(), "o", "r", "r", input) 65 if err != nil { 66 t.Errorf("Repositories.CreateStatus returned error: %v", err) 67 } 68 69 want := &RepoStatus{ID: Int64(1)} 70 if !reflect.DeepEqual(status, want) { 71 t.Errorf("Repositories.CreateStatus returned %+v, want %+v", status, want) 72 } 73 } 74 75 func TestRepositoriesService_CreateStatus_invalidOwner(t *testing.T) { 76 client, _, _, teardown := setup() 77 defer teardown() 78 79 _, _, err := client.Repositories.CreateStatus(context.Background(), "%", "r", "r", nil) 80 testURLParseError(t, err) 81 } 82 83 func TestRepositoriesService_GetCombinedStatus(t *testing.T) { 84 client, mux, _, teardown := setup() 85 defer teardown() 86 87 mux.HandleFunc("/repos/o/r/commits/r/status", func(w http.ResponseWriter, r *http.Request) { 88 testMethod(t, r, "GET") 89 testFormValues(t, r, values{"page": "2"}) 90 fmt.Fprint(w, `{"state":"success", "statuses":[{"id":1}]}`) 91 }) 92 93 opt := &ListOptions{Page: 2} 94 status, _, err := client.Repositories.GetCombinedStatus(context.Background(), "o", "r", "r", opt) 95 if err != nil { 96 t.Errorf("Repositories.GetCombinedStatus returned error: %v", err) 97 } 98 99 want := &CombinedStatus{State: String("success"), Statuses: []*RepoStatus{{ID: Int64(1)}}} 100 if !reflect.DeepEqual(status, want) { 101 t.Errorf("Repositories.GetCombinedStatus returned %+v, want %+v", status, want) 102 } 103 }