github.com/google/go-github/v64@v64.0.0/github/meta_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 "fmt" 11 "net/http" 12 "testing" 13 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestAPIMeta_Marshal(t *testing.T) { 18 testJSONMarshal(t, &APIMeta{}, "{}") 19 20 a := &APIMeta{ 21 Hooks: []string{"h"}, 22 Git: []string{"g"}, 23 VerifiablePasswordAuthentication: Bool(true), 24 Pages: []string{"p"}, 25 Importer: []string{"i"}, 26 GithubEnterpriseImporter: []string{"gei"}, 27 Actions: []string{"a"}, 28 Dependabot: []string{"d"}, 29 SSHKeyFingerprints: map[string]string{"a": "f"}, 30 SSHKeys: []string{"k"}, 31 API: []string{"a"}, 32 Web: []string{"w"}, 33 Domains: map[string][]string{ 34 "example": {"example.com", "*.example.com"}, 35 }, 36 } 37 want := `{ 38 "hooks":["h"], 39 "git":["g"], 40 "verifiable_password_authentication":true, 41 "pages":["p"], 42 "importer":["i"], 43 "github_enterprise_importer":["gei"], 44 "actions":["a"], 45 "dependabot":["d"], 46 "ssh_key_fingerprints":{"a":"f"}, 47 "ssh_keys":["k"], 48 "api":["a"], 49 "web":["w"], 50 "domains":{"example":["example.com","*.example.com"]} 51 }` 52 53 testJSONMarshal(t, a, want) 54 } 55 56 func TestMetaService_Get(t *testing.T) { 57 client, mux, _, teardown := setup() 58 defer teardown() 59 60 mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) { 61 testMethod(t, r, "GET") 62 fmt.Fprint(w, `{"web":["w"],"api":["a"],"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "github_enterprise_importer": ["gei"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true, "domains":{"example":["example.com","*.example.com"]}}`) 63 }) 64 65 ctx := context.Background() 66 meta, _, err := client.Meta.Get(ctx) 67 if err != nil { 68 t.Errorf("Get returned error: %v", err) 69 } 70 71 want := &APIMeta{ 72 Hooks: []string{"h"}, 73 Git: []string{"g"}, 74 Pages: []string{"p"}, 75 Importer: []string{"i"}, 76 GithubEnterpriseImporter: []string{"gei"}, 77 Actions: []string{"a"}, 78 Dependabot: []string{"d"}, 79 API: []string{"a"}, 80 Web: []string{"w"}, 81 Domains: map[string][]string{ 82 "example": {"example.com", "*.example.com"}, 83 }, 84 85 VerifiablePasswordAuthentication: Bool(true), 86 } 87 if !cmp.Equal(want, meta) { 88 t.Errorf("Get returned %+v, want %+v", meta, want) 89 } 90 91 const methodName = "Get" 92 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 93 got, resp, err := client.Meta.Get(ctx) 94 if got != nil { 95 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 96 } 97 return resp, err 98 }) 99 } 100 101 func TestMetaService_Octocat(t *testing.T) { 102 client, mux, _, teardown := setup() 103 defer teardown() 104 105 input := "input" 106 output := "sample text" 107 108 mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { 109 testMethod(t, r, "GET") 110 testFormValues(t, r, values{"s": input}) 111 w.Header().Set("Content-Type", "application/octocat-stream") 112 fmt.Fprint(w, output) 113 }) 114 115 ctx := context.Background() 116 got, _, err := client.Meta.Octocat(ctx, input) 117 if err != nil { 118 t.Errorf("Octocat returned error: %v", err) 119 } 120 121 if want := output; got != want { 122 t.Errorf("Octocat returned %+v, want %+v", got, want) 123 } 124 125 const methodName = "Octocat" 126 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 127 got, resp, err := client.Meta.Octocat(ctx, input) 128 if got != "" { 129 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 130 } 131 return resp, err 132 }) 133 } 134 135 func TestMetaService_Zen(t *testing.T) { 136 client, mux, _, teardown := setup() 137 defer teardown() 138 139 output := "sample text" 140 141 mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { 142 testMethod(t, r, "GET") 143 w.Header().Set("Content-Type", "text/plain;charset=utf-8") 144 fmt.Fprint(w, output) 145 }) 146 147 ctx := context.Background() 148 got, _, err := client.Meta.Zen(ctx) 149 if err != nil { 150 t.Errorf("Zen returned error: %v", err) 151 } 152 153 if want := output; got != want { 154 t.Errorf("Zen returned %+v, want %+v", got, want) 155 } 156 157 const methodName = "Zen" 158 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 159 got, resp, err := client.Meta.Zen(ctx) 160 if got != "" { 161 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 162 } 163 return resp, err 164 }) 165 }