github.com/google/go-github/v42@v42.0.0/github/misc_test.go (about) 1 // Copyright 2014 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 TestMarkdown(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 input := &markdownRequest{ 23 Text: String("# text #"), 24 Mode: String("gfm"), 25 Context: String("google/go-github"), 26 } 27 mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { 28 v := new(markdownRequest) 29 json.NewDecoder(r.Body).Decode(v) 30 31 testMethod(t, r, "POST") 32 if !cmp.Equal(v, input) { 33 t.Errorf("Request body = %+v, want %+v", v, input) 34 } 35 fmt.Fprint(w, `<h1>text</h1>`) 36 }) 37 38 ctx := context.Background() 39 md, _, err := client.Markdown(ctx, "# text #", &MarkdownOptions{ 40 Mode: "gfm", 41 Context: "google/go-github", 42 }) 43 if err != nil { 44 t.Errorf("Markdown returned error: %v", err) 45 } 46 47 if want := "<h1>text</h1>"; want != md { 48 t.Errorf("Markdown returned %+v, want %+v", md, want) 49 } 50 51 const methodName = "Markdown" 52 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 53 got, resp, err := client.Markdown(ctx, "# text #", &MarkdownOptions{ 54 Mode: "gfm", 55 Context: "google/go-github", 56 }) 57 if got != "" { 58 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 59 } 60 return resp, err 61 }) 62 } 63 64 func TestListEmojis(t *testing.T) { 65 client, mux, _, teardown := setup() 66 defer teardown() 67 68 mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) { 69 testMethod(t, r, "GET") 70 fmt.Fprint(w, `{"+1": "+1.png"}`) 71 }) 72 73 ctx := context.Background() 74 emoji, _, err := client.ListEmojis(ctx) 75 if err != nil { 76 t.Errorf("ListEmojis returned error: %v", err) 77 } 78 79 want := map[string]string{"+1": "+1.png"} 80 if !cmp.Equal(want, emoji) { 81 t.Errorf("ListEmojis returned %+v, want %+v", emoji, want) 82 } 83 84 const methodName = "ListEmojis" 85 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 86 got, resp, err := client.ListEmojis(ctx) 87 if got != nil { 88 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 89 } 90 return resp, err 91 }) 92 } 93 94 func TestListCodesOfConduct(t *testing.T) { 95 client, mux, _, teardown := setup() 96 defer teardown() 97 98 mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) { 99 testMethod(t, r, "GET") 100 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) 101 fmt.Fprint(w, `[{ 102 "key": "key", 103 "name": "name", 104 "url": "url"} 105 ]`) 106 }) 107 108 ctx := context.Background() 109 cs, _, err := client.ListCodesOfConduct(ctx) 110 if err != nil { 111 t.Errorf("ListCodesOfConduct returned error: %v", err) 112 } 113 114 want := []*CodeOfConduct{ 115 { 116 Key: String("key"), 117 Name: String("name"), 118 URL: String("url"), 119 }} 120 if !cmp.Equal(want, cs) { 121 t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want) 122 } 123 124 const methodName = "ListCodesOfConduct" 125 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 126 got, resp, err := client.ListCodesOfConduct(ctx) 127 if got != nil { 128 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 129 } 130 return resp, err 131 }) 132 } 133 134 func TestGetCodeOfConduct(t *testing.T) { 135 client, mux, _, teardown := setup() 136 defer teardown() 137 138 mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) { 139 testMethod(t, r, "GET") 140 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) 141 fmt.Fprint(w, `{ 142 "key": "key", 143 "name": "name", 144 "url": "url", 145 "body": "body"}`, 146 ) 147 }) 148 149 ctx := context.Background() 150 coc, _, err := client.GetCodeOfConduct(ctx, "k") 151 if err != nil { 152 t.Errorf("ListCodesOfConduct returned error: %v", err) 153 } 154 155 want := &CodeOfConduct{ 156 Key: String("key"), 157 Name: String("name"), 158 URL: String("url"), 159 Body: String("body"), 160 } 161 if !cmp.Equal(want, coc) { 162 t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want) 163 } 164 165 const methodName = "GetCodeOfConduct" 166 testBadOptions(t, methodName, func() (err error) { 167 _, _, err = client.GetCodeOfConduct(ctx, "\n") 168 return err 169 }) 170 171 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 172 got, resp, err := client.GetCodeOfConduct(ctx, "k") 173 if got != nil { 174 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 175 } 176 return resp, err 177 }) 178 } 179 180 func TestAPIMeta_Marshal(t *testing.T) { 181 testJSONMarshal(t, &APIMeta{}, "{}") 182 183 a := &APIMeta{ 184 Hooks: []string{"h"}, 185 Git: []string{"g"}, 186 VerifiablePasswordAuthentication: Bool(true), 187 Pages: []string{"p"}, 188 Importer: []string{"i"}, 189 Actions: []string{"a"}, 190 Dependabot: []string{"d"}, 191 } 192 want := `{ 193 "hooks":["h"], 194 "git":["g"], 195 "verifiable_password_authentication":true, 196 "pages":["p"], 197 "importer":["i"], 198 "actions":["a"], 199 "dependabot":["d"] 200 }` 201 202 testJSONMarshal(t, a, want) 203 } 204 205 func TestAPIMeta(t *testing.T) { 206 client, mux, _, teardown := setup() 207 defer teardown() 208 209 mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) { 210 testMethod(t, r, "GET") 211 fmt.Fprint(w, `{"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true}`) 212 }) 213 214 ctx := context.Background() 215 meta, _, err := client.APIMeta(ctx) 216 if err != nil { 217 t.Errorf("APIMeta returned error: %v", err) 218 } 219 220 want := &APIMeta{ 221 Hooks: []string{"h"}, 222 Git: []string{"g"}, 223 Pages: []string{"p"}, 224 Importer: []string{"i"}, 225 Actions: []string{"a"}, 226 Dependabot: []string{"d"}, 227 228 VerifiablePasswordAuthentication: Bool(true), 229 } 230 if !cmp.Equal(want, meta) { 231 t.Errorf("APIMeta returned %+v, want %+v", meta, want) 232 } 233 234 const methodName = "APIMeta" 235 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 236 got, resp, err := client.APIMeta(ctx) 237 if got != nil { 238 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 239 } 240 return resp, err 241 }) 242 } 243 244 func TestOctocat(t *testing.T) { 245 client, mux, _, teardown := setup() 246 defer teardown() 247 248 input := "input" 249 output := "sample text" 250 251 mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { 252 testMethod(t, r, "GET") 253 testFormValues(t, r, values{"s": input}) 254 w.Header().Set("Content-Type", "application/octocat-stream") 255 fmt.Fprint(w, output) 256 }) 257 258 ctx := context.Background() 259 got, _, err := client.Octocat(ctx, input) 260 if err != nil { 261 t.Errorf("Octocat returned error: %v", err) 262 } 263 264 if want := output; got != want { 265 t.Errorf("Octocat returned %+v, want %+v", got, want) 266 } 267 268 const methodName = "Octocat" 269 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 270 got, resp, err := client.Octocat(ctx, input) 271 if got != "" { 272 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 273 } 274 return resp, err 275 }) 276 } 277 278 func TestZen(t *testing.T) { 279 client, mux, _, teardown := setup() 280 defer teardown() 281 282 output := "sample text" 283 284 mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { 285 testMethod(t, r, "GET") 286 w.Header().Set("Content-Type", "text/plain;charset=utf-8") 287 fmt.Fprint(w, output) 288 }) 289 290 ctx := context.Background() 291 got, _, err := client.Zen(ctx) 292 if err != nil { 293 t.Errorf("Zen returned error: %v", err) 294 } 295 296 if want := output; got != want { 297 t.Errorf("Zen returned %+v, want %+v", got, want) 298 } 299 300 const methodName = "Zen" 301 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 302 got, resp, err := client.Zen(ctx) 303 if got != "" { 304 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 305 } 306 return resp, err 307 }) 308 } 309 310 func TestListServiceHooks(t *testing.T) { 311 client, mux, _, teardown := setup() 312 defer teardown() 313 314 mux.HandleFunc("/hooks", func(w http.ResponseWriter, r *http.Request) { 315 testMethod(t, r, "GET") 316 fmt.Fprint(w, `[{ 317 "name":"n", 318 "events":["e"], 319 "supported_events":["s"], 320 "schema":[ 321 ["a", "b"] 322 ] 323 }]`) 324 }) 325 326 ctx := context.Background() 327 hooks, _, err := client.ListServiceHooks(ctx) 328 if err != nil { 329 t.Errorf("ListServiceHooks returned error: %v", err) 330 } 331 332 want := []*ServiceHook{{ 333 Name: String("n"), 334 Events: []string{"e"}, 335 SupportedEvents: []string{"s"}, 336 Schema: [][]string{{"a", "b"}}, 337 }} 338 if !cmp.Equal(hooks, want) { 339 t.Errorf("ListServiceHooks returned %+v, want %+v", hooks, want) 340 } 341 342 const methodName = "ListServiceHooks" 343 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 344 got, resp, err := client.ListServiceHooks(ctx) 345 if got != nil { 346 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 347 } 348 return resp, err 349 }) 350 } 351 352 func TestMarkdownRequest_Marshal(t *testing.T) { 353 testJSONMarshal(t, &markdownRequest{}, "{}") 354 355 a := &markdownRequest{ 356 Text: String("txt"), 357 Mode: String("mode"), 358 Context: String("ctx"), 359 } 360 361 want := `{ 362 "text": "txt", 363 "mode": "mode", 364 "context": "ctx" 365 }` 366 367 testJSONMarshal(t, a, want) 368 } 369 370 func TestCodeOfConduct_Marshal(t *testing.T) { 371 testJSONMarshal(t, &CodeOfConduct{}, "{}") 372 373 a := &CodeOfConduct{ 374 Name: String("name"), 375 Key: String("key"), 376 URL: String("url"), 377 Body: String("body"), 378 } 379 380 want := `{ 381 "name": "name", 382 "key": "key", 383 "url": "url", 384 "body": "body" 385 }` 386 387 testJSONMarshal(t, a, want) 388 } 389 390 func TestServiceHook_Marshal(t *testing.T) { 391 testJSONMarshal(t, &ServiceHook{}, "{}") 392 393 a := &ServiceHook{ 394 Name: String("name"), 395 Events: []string{"e"}, 396 SupportedEvents: []string{"se"}, 397 Schema: [][]string{{"g"}}, 398 } 399 want := `{ 400 "name": "name", 401 "events": [ 402 "e" 403 ], 404 "supported_events": [ 405 "se" 406 ], 407 "schema": [ 408 [ 409 "g" 410 ] 411 ] 412 }` 413 414 testJSONMarshal(t, a, want) 415 }