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