github.com/google/go-github/v42@v42.0.0/github/repos_autolinks_test.go (about) 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{KeyPrefix: String("TICKET-"), URLTemplate: String("https://example.com/TICKET?query=<num>")} 66 mux.HandleFunc("/repos/o/r/autolinks", func(w http.ResponseWriter, r *http.Request) { 67 v := new(AutolinkOptions) 68 json.NewDecoder(r.Body).Decode(v) 69 testMethod(t, r, "POST") 70 if !cmp.Equal(v, opt) { 71 t.Errorf("Request body = %+v, want %+v", v, opt) 72 } 73 w.WriteHeader(http.StatusOK) 74 w.Write([]byte(`{"key_prefix": "TICKET-","url_template": "https://example.com/TICKET?query=<num>"}`)) 75 }) 76 ctx := context.Background() 77 autolink, _, err := client.Repositories.AddAutolink(ctx, "o", "r", opt) 78 if err != nil { 79 t.Errorf("Repositories.AddAutolink returned error: %v", err) 80 } 81 want := &Autolink{ 82 KeyPrefix: String("TICKET-"), 83 URLTemplate: String("https://example.com/TICKET?query=<num>"), 84 } 85 86 if !cmp.Equal(autolink, want) { 87 t.Errorf("AddAutolink returned %+v, want %+v", autolink, want) 88 } 89 90 const methodName = "AddAutolink" 91 testBadOptions(t, methodName, func() (err error) { 92 _, _, err = client.Repositories.AddAutolink(ctx, "\n", "\n", opt) 93 return err 94 }) 95 96 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 97 got, resp, err := client.Repositories.AddAutolink(ctx, "o", "r", opt) 98 if got != nil { 99 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 100 } 101 return resp, err 102 }) 103 } 104 105 func TestRepositoriesService_GetAutolink(t *testing.T) { 106 client, mux, _, teardown := setup() 107 defer teardown() 108 109 mux.HandleFunc("/repos/o/r/autolinks/1", func(w http.ResponseWriter, r *http.Request) { 110 testMethod(t, r, "GET") 111 fmt.Fprintf(w, `{"id":1, "key_prefix": "TICKET-", "url_template": "https://example.com/TICKET?query=<num>"}`) 112 }) 113 114 ctx := context.Background() 115 autolink, _, err := client.Repositories.GetAutolink(ctx, "o", "r", 1) 116 if err != nil { 117 t.Errorf("Repositories.GetAutolink returned error: %v", err) 118 } 119 120 want := &Autolink{ID: Int64(1), KeyPrefix: String("TICKET-"), URLTemplate: String("https://example.com/TICKET?query=<num>")} 121 if !cmp.Equal(autolink, want) { 122 t.Errorf("Repositories.GetAutolink returned %+v, want %+v", autolink, want) 123 } 124 125 const methodName = "GetAutolink" 126 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 127 got, resp, err := client.Repositories.GetAutolink(ctx, "o", "r", 2) 128 if got != nil { 129 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 130 } 131 return resp, err 132 }) 133 } 134 135 func TestRepositoriesService_DeleteAutolink(t *testing.T) { 136 client, mux, _, teardown := setup() 137 defer teardown() 138 139 mux.HandleFunc("/repos/o/r/autolinks/1", func(w http.ResponseWriter, r *http.Request) { 140 testMethod(t, r, "DELETE") 141 w.WriteHeader(http.StatusNoContent) 142 }) 143 144 ctx := context.Background() 145 _, err := client.Repositories.DeleteAutolink(ctx, "o", "r", 1) 146 if err != nil { 147 t.Errorf("Repositories.DeleteAutolink returned error: %v", err) 148 } 149 150 const methodName = "DeleteAutolink" 151 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 152 return client.Repositories.DeleteAutolink(ctx, "o", "r", 2) 153 }) 154 } 155 156 func TestAutolinkOptions_Marshal(t *testing.T) { 157 testJSONMarshal(t, &AutolinkOptions{}, "{}") 158 159 r := &AutolinkOptions{ 160 KeyPrefix: String("kp"), 161 URLTemplate: String("URLT"), 162 } 163 164 want := `{ 165 "key_prefix": "kp", 166 "url_template": "URLT" 167 }` 168 169 testJSONMarshal(t, r, want) 170 } 171 172 func TestAutolink_Marshal(t *testing.T) { 173 testJSONMarshal(t, &Autolink{}, "{}") 174 175 r := &Autolink{ 176 ID: Int64(1), 177 KeyPrefix: String("kp"), 178 URLTemplate: String("URLT"), 179 } 180 181 want := `{ 182 "id": 1, 183 "key_prefix": "kp", 184 "url_template": "URLT" 185 }` 186 187 testJSONMarshal(t, r, want) 188 }