github.com/google/go-github/v70@v70.0.0/github/codesofconduct_test.go (about) 1 // Copyright 2023 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 "fmt" 11 "net/http" 12 "testing" 13 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestCodesOfConductService_List(t *testing.T) { 18 t.Parallel() 19 client, mux, _ := setup(t) 20 21 mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) 24 fmt.Fprint(w, `[{ 25 "key": "key", 26 "name": "name", 27 "url": "url"} 28 ]`) 29 }) 30 31 ctx := context.Background() 32 cs, _, err := client.ListCodesOfConduct(ctx) 33 assertNilError(t, err) 34 35 want := []*CodeOfConduct{ 36 { 37 Key: Ptr("key"), 38 Name: Ptr("name"), 39 URL: Ptr("url"), 40 }} 41 if !cmp.Equal(want, cs) { 42 t.Errorf("returned %+v, want %+v", cs, want) 43 } 44 45 const methodName = "List" 46 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 47 got, resp, err := client.CodesOfConduct.List(ctx) 48 if got != nil { 49 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 50 } 51 return resp, err 52 }) 53 } 54 55 func TestCodesOfConductService_Get(t *testing.T) { 56 t.Parallel() 57 client, mux, _ := setup(t) 58 59 mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) { 60 testMethod(t, r, "GET") 61 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) 62 fmt.Fprint(w, `{ 63 "key": "key", 64 "name": "name", 65 "url": "url", 66 "body": "body"}`, 67 ) 68 }) 69 70 ctx := context.Background() 71 coc, _, err := client.GetCodeOfConduct(ctx, "k") 72 assertNilError(t, err) 73 74 want := &CodeOfConduct{ 75 Key: Ptr("key"), 76 Name: Ptr("name"), 77 URL: Ptr("url"), 78 Body: Ptr("body"), 79 } 80 if !cmp.Equal(want, coc) { 81 t.Errorf("returned %+v, want %+v", coc, want) 82 } 83 84 const methodName = "Get" 85 testBadOptions(t, methodName, func() (err error) { 86 _, _, err = client.CodesOfConduct.Get(ctx, "\n") 87 return err 88 }) 89 90 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 91 got, resp, err := client.CodesOfConduct.Get(ctx, "k") 92 if got != nil { 93 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 94 } 95 return resp, err 96 }) 97 } 98 99 func TestCodeOfConduct_Marshal(t *testing.T) { 100 t.Parallel() 101 testJSONMarshal(t, &CodeOfConduct{}, "{}") 102 103 a := &CodeOfConduct{ 104 Name: Ptr("name"), 105 Key: Ptr("key"), 106 URL: Ptr("url"), 107 Body: Ptr("body"), 108 } 109 110 want := `{ 111 "name": "name", 112 "key": "key", 113 "url": "url", 114 "body": "body" 115 }` 116 117 testJSONMarshal(t, a, want) 118 }