github.com/google/go-github/v33@v33.0.0/github/misc_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 "encoding/json" 11 "fmt" 12 "net/http" 13 "reflect" 14 "testing" 15 ) 16 17 func TestMarkdown(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 input := &markdownRequest{ 22 Text: String("# text #"), 23 Mode: String("gfm"), 24 Context: String("google/go-github"), 25 } 26 mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { 27 v := new(markdownRequest) 28 json.NewDecoder(r.Body).Decode(v) 29 30 testMethod(t, r, "POST") 31 if !reflect.DeepEqual(v, input) { 32 t.Errorf("Request body = %+v, want %+v", v, input) 33 } 34 fmt.Fprint(w, `<h1>text</h1>`) 35 }) 36 37 md, _, err := client.Markdown(context.Background(), "# text #", &MarkdownOptions{ 38 Mode: "gfm", 39 Context: "google/go-github", 40 }) 41 if err != nil { 42 t.Errorf("Markdown returned error: %v", err) 43 } 44 45 if want := "<h1>text</h1>"; want != md { 46 t.Errorf("Markdown returned %+v, want %+v", md, want) 47 } 48 } 49 50 func TestListEmojis(t *testing.T) { 51 client, mux, _, teardown := setup() 52 defer teardown() 53 54 mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) { 55 testMethod(t, r, "GET") 56 fmt.Fprint(w, `{"+1": "+1.png"}`) 57 }) 58 59 emoji, _, err := client.ListEmojis(context.Background()) 60 if err != nil { 61 t.Errorf("ListEmojis returned error: %v", err) 62 } 63 64 want := map[string]string{"+1": "+1.png"} 65 if !reflect.DeepEqual(want, emoji) { 66 t.Errorf("ListEmojis returned %+v, want %+v", emoji, want) 67 } 68 } 69 70 func TestListCodesOfConduct(t *testing.T) { 71 client, mux, _, teardown := setup() 72 defer teardown() 73 74 mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) { 75 testMethod(t, r, "GET") 76 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) 77 fmt.Fprint(w, `[{ 78 "key": "key", 79 "name": "name", 80 "url": "url"} 81 ]`) 82 }) 83 84 cs, _, err := client.ListCodesOfConduct(context.Background()) 85 if err != nil { 86 t.Errorf("ListCodesOfConduct returned error: %v", err) 87 } 88 89 want := []*CodeOfConduct{ 90 { 91 Key: String("key"), 92 Name: String("name"), 93 URL: String("url"), 94 }} 95 if !reflect.DeepEqual(want, cs) { 96 t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want) 97 } 98 } 99 100 func TestGetCodeOfConduct(t *testing.T) { 101 client, mux, _, teardown := setup() 102 defer teardown() 103 104 mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) { 105 testMethod(t, r, "GET") 106 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview) 107 fmt.Fprint(w, `{ 108 "key": "key", 109 "name": "name", 110 "url": "url", 111 "body": "body"}`, 112 ) 113 }) 114 115 coc, _, err := client.GetCodeOfConduct(context.Background(), "k") 116 if err != nil { 117 t.Errorf("ListCodesOfConduct returned error: %v", err) 118 } 119 120 want := &CodeOfConduct{ 121 Key: String("key"), 122 Name: String("name"), 123 URL: String("url"), 124 Body: String("body"), 125 } 126 if !reflect.DeepEqual(want, coc) { 127 t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want) 128 } 129 } 130 131 func TestAPIMeta_marshal(t *testing.T) { 132 testJSONMarshal(t, &APIMeta{}, "{}") 133 134 a := &APIMeta{ 135 Hooks: []string{"h"}, 136 Git: []string{"g"}, 137 VerifiablePasswordAuthentication: Bool(true), 138 Pages: []string{"p"}, 139 Importer: []string{"i"}, 140 } 141 want := `{ 142 "hooks":["h"], 143 "git":["g"], 144 "verifiable_password_authentication":true, 145 "pages":["p"],"importer":["i"] 146 }` 147 148 testJSONMarshal(t, a, want) 149 } 150 151 func TestAPIMeta(t *testing.T) { 152 client, mux, _, teardown := setup() 153 defer teardown() 154 155 mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) { 156 testMethod(t, r, "GET") 157 fmt.Fprint(w, `{"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "verifiable_password_authentication": true}`) 158 }) 159 160 meta, _, err := client.APIMeta(context.Background()) 161 if err != nil { 162 t.Errorf("APIMeta returned error: %v", err) 163 } 164 165 want := &APIMeta{ 166 Hooks: []string{"h"}, 167 Git: []string{"g"}, 168 Pages: []string{"p"}, 169 Importer: []string{"i"}, 170 171 VerifiablePasswordAuthentication: Bool(true), 172 } 173 if !reflect.DeepEqual(want, meta) { 174 t.Errorf("APIMeta returned %+v, want %+v", meta, want) 175 } 176 } 177 178 func TestOctocat(t *testing.T) { 179 client, mux, _, teardown := setup() 180 defer teardown() 181 182 input := "input" 183 output := "sample text" 184 185 mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) { 186 testMethod(t, r, "GET") 187 testFormValues(t, r, values{"s": input}) 188 w.Header().Set("Content-Type", "application/octocat-stream") 189 fmt.Fprint(w, output) 190 }) 191 192 got, _, err := client.Octocat(context.Background(), input) 193 if err != nil { 194 t.Errorf("Octocat returned error: %v", err) 195 } 196 197 if want := output; got != want { 198 t.Errorf("Octocat returned %+v, want %+v", got, want) 199 } 200 } 201 202 func TestZen(t *testing.T) { 203 client, mux, _, teardown := setup() 204 defer teardown() 205 206 output := "sample text" 207 208 mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) { 209 testMethod(t, r, "GET") 210 w.Header().Set("Content-Type", "text/plain;charset=utf-8") 211 fmt.Fprint(w, output) 212 }) 213 214 got, _, err := client.Zen(context.Background()) 215 if err != nil { 216 t.Errorf("Zen returned error: %v", err) 217 } 218 219 if want := output; got != want { 220 t.Errorf("Zen returned %+v, want %+v", got, want) 221 } 222 } 223 224 func TestListServiceHooks(t *testing.T) { 225 client, mux, _, teardown := setup() 226 defer teardown() 227 228 mux.HandleFunc("/hooks", func(w http.ResponseWriter, r *http.Request) { 229 testMethod(t, r, "GET") 230 fmt.Fprint(w, `[{ 231 "name":"n", 232 "events":["e"], 233 "supported_events":["s"], 234 "schema":[ 235 ["a", "b"] 236 ] 237 }]`) 238 }) 239 240 hooks, _, err := client.ListServiceHooks(context.Background()) 241 if err != nil { 242 t.Errorf("ListServiceHooks returned error: %v", err) 243 } 244 245 want := []*ServiceHook{{ 246 Name: String("n"), 247 Events: []string{"e"}, 248 SupportedEvents: []string{"s"}, 249 Schema: [][]string{{"a", "b"}}, 250 }} 251 if !reflect.DeepEqual(hooks, want) { 252 t.Errorf("ListServiceHooks returned %+v, want %+v", hooks, want) 253 } 254 }