github.com/google/go-github/v65@v65.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: &APIMetaDomains{ 34 Website: []string{ 35 "*.github.com", 36 "*.github.dev", 37 "*.github.io", 38 "*.githubassets.com", 39 "*.githubusercontent.com", 40 }, 41 ArtifactAttestations: &APIMetaArtifactAttestations{ 42 TrustDomain: "", 43 Services: []string{ 44 "*.actions.githubusercontent.com", 45 "tuf-repo.github.com", 46 "fulcio.githubapp.com", 47 "timestamp.githubapp.com", 48 }, 49 }, 50 }, 51 } 52 want := `{ 53 "hooks":["h"], 54 "git":["g"], 55 "verifiable_password_authentication":true, 56 "pages":["p"], 57 "importer":["i"], 58 "github_enterprise_importer":["gei"], 59 "actions":["a"], 60 "dependabot":["d"], 61 "ssh_key_fingerprints":{"a":"f"}, 62 "ssh_keys":["k"], 63 "api":["a"], 64 "web":["w"], 65 "domains":{"website":["*.github.com","*.github.dev","*.github.io","*.githubassets.com","*.githubusercontent.com"],"artifact_attestations":{"trust_domain":"","services":["*.actions.githubusercontent.com","tuf-repo.github.com","fulcio.githubapp.com","timestamp.githubapp.com"]}} 66 }` 67 68 testJSONMarshal(t, a, want) 69 } 70 71 func TestMetaService_Get(t *testing.T) { 72 client, mux, _, teardown := setup() 73 defer teardown() 74 75 mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) { 76 testMethod(t, r, "GET") 77 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":{"website":["*.github.com","*.github.dev","*.github.io","*.githubassets.com","*.githubusercontent.com"],"artifact_attestations":{"trust_domain":"","services":["*.actions.githubusercontent.com","tuf-repo.github.com","fulcio.githubapp.com","timestamp.githubapp.com"]}}}`) 78 }) 79 80 ctx := context.Background() 81 meta, _, err := client.Meta.Get(ctx) 82 if err != nil { 83 t.Errorf("Get returned error: %v", err) 84 } 85 86 want := &APIMeta{ 87 Hooks: []string{"h"}, 88 Git: []string{"g"}, 89 Pages: []string{"p"}, 90 Importer: []string{"i"}, 91 GithubEnterpriseImporter: []string{"gei"}, 92 Actions: []string{"a"}, 93 Dependabot: []string{"d"}, 94 API: []string{"a"}, 95 Web: []string{"w"}, 96 Domains: &APIMetaDomains{ 97 Website: []string{ 98 "*.github.com", 99 "*.github.dev", 100 "*.github.io", 101 "*.githubassets.com", 102 "*.githubusercontent.com", 103 }, 104 ArtifactAttestations: &APIMetaArtifactAttestations{ 105 TrustDomain: "", 106 Services: []string{ 107 "*.actions.githubusercontent.com", 108 "tuf-repo.github.com", 109 "fulcio.githubapp.com", 110 "timestamp.githubapp.com", 111 }, 112 }, 113 }, 114 115 VerifiablePasswordAuthentication: Bool(true), 116 } 117 if !cmp.Equal(want, meta) { 118 t.Errorf("Get returned %+v, want %+v", meta, want) 119 } 120 121 const methodName = "Get" 122 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 123 got, resp, err := client.Meta.Get(ctx) 124 if got != nil { 125 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 126 } 127 return resp, err 128 }) 129 } 130 131 func TestMetaService_Octocat(t *testing.T) { 132 client, mux, _, teardown := setup() 133 defer teardown() 134 135 input := "input" 136 output := "sample text" 137 138 mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { 139 testMethod(t, r, "GET") 140 testFormValues(t, r, values{"s": input}) 141 w.Header().Set("Content-Type", "application/octocat-stream") 142 fmt.Fprint(w, output) 143 }) 144 145 ctx := context.Background() 146 got, _, err := client.Meta.Octocat(ctx, input) 147 if err != nil { 148 t.Errorf("Octocat returned error: %v", err) 149 } 150 151 if want := output; got != want { 152 t.Errorf("Octocat returned %+v, want %+v", got, want) 153 } 154 155 const methodName = "Octocat" 156 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 157 got, resp, err := client.Meta.Octocat(ctx, input) 158 if got != "" { 159 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 160 } 161 return resp, err 162 }) 163 } 164 165 func TestMetaService_Zen(t *testing.T) { 166 client, mux, _, teardown := setup() 167 defer teardown() 168 169 output := "sample text" 170 171 mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { 172 testMethod(t, r, "GET") 173 w.Header().Set("Content-Type", "text/plain;charset=utf-8") 174 fmt.Fprint(w, output) 175 }) 176 177 ctx := context.Background() 178 got, _, err := client.Meta.Zen(ctx) 179 if err != nil { 180 t.Errorf("Zen returned error: %v", err) 181 } 182 183 if want := output; got != want { 184 t.Errorf("Zen returned %+v, want %+v", got, want) 185 } 186 187 const methodName = "Zen" 188 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 189 got, resp, err := client.Meta.Zen(ctx) 190 if got != "" { 191 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 192 } 193 return resp, err 194 }) 195 }