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