1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "fmt"
12 "net/http"
13 "testing"
14
15 "github.com/google/go-cmp/cmp"
16 )
17
18 func TestMarkdown(t *testing.T) {
19 client, mux, _, teardown := setup()
20 defer teardown()
21
22 input := &markdownRequest{
23 Text: String("# text #"),
24 Mode: String("gfm"),
25 Context: String("google/go-github"),
26 }
27 mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) {
28 v := new(markdownRequest)
29 json.NewDecoder(r.Body).Decode(v)
30
31 testMethod(t, r, "POST")
32 if !cmp.Equal(v, input) {
33 t.Errorf("Request body = %+v, want %+v", v, input)
34 }
35 fmt.Fprint(w, `<h1>text</h1>`)
36 })
37
38 ctx := context.Background()
39 md, _, err := client.Markdown(ctx, "# text #", &MarkdownOptions{
40 Mode: "gfm",
41 Context: "google/go-github",
42 })
43 if err != nil {
44 t.Errorf("Markdown returned error: %v", err)
45 }
46
47 if want := "<h1>text</h1>"; want != md {
48 t.Errorf("Markdown returned %+v, want %+v", md, want)
49 }
50
51 const methodName = "Markdown"
52 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
53 got, resp, err := client.Markdown(ctx, "# text #", &MarkdownOptions{
54 Mode: "gfm",
55 Context: "google/go-github",
56 })
57 if got != "" {
58 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
59 }
60 return resp, err
61 })
62 }
63
64 func TestListEmojis(t *testing.T) {
65 client, mux, _, teardown := setup()
66 defer teardown()
67
68 mux.HandleFunc("/emojis", func(w http.ResponseWriter, r *http.Request) {
69 testMethod(t, r, "GET")
70 fmt.Fprint(w, `{"+1": "+1.png"}`)
71 })
72
73 ctx := context.Background()
74 emoji, _, err := client.ListEmojis(ctx)
75 if err != nil {
76 t.Errorf("ListEmojis returned error: %v", err)
77 }
78
79 want := map[string]string{"+1": "+1.png"}
80 if !cmp.Equal(want, emoji) {
81 t.Errorf("ListEmojis returned %+v, want %+v", emoji, want)
82 }
83
84 const methodName = "ListEmojis"
85 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
86 got, resp, err := client.ListEmojis(ctx)
87 if got != nil {
88 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
89 }
90 return resp, err
91 })
92 }
93
94 func TestListCodesOfConduct(t *testing.T) {
95 client, mux, _, teardown := setup()
96 defer teardown()
97
98 mux.HandleFunc("/codes_of_conduct", func(w http.ResponseWriter, r *http.Request) {
99 testMethod(t, r, "GET")
100 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview)
101 fmt.Fprint(w, `[{
102 "key": "key",
103 "name": "name",
104 "url": "url"}
105 ]`)
106 })
107
108 ctx := context.Background()
109 cs, _, err := client.ListCodesOfConduct(ctx)
110 if err != nil {
111 t.Errorf("ListCodesOfConduct returned error: %v", err)
112 }
113
114 want := []*CodeOfConduct{
115 {
116 Key: String("key"),
117 Name: String("name"),
118 URL: String("url"),
119 }}
120 if !cmp.Equal(want, cs) {
121 t.Errorf("ListCodesOfConduct returned %+v, want %+v", cs, want)
122 }
123
124 const methodName = "ListCodesOfConduct"
125 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
126 got, resp, err := client.ListCodesOfConduct(ctx)
127 if got != nil {
128 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
129 }
130 return resp, err
131 })
132 }
133
134 func TestGetCodeOfConduct(t *testing.T) {
135 client, mux, _, teardown := setup()
136 defer teardown()
137
138 mux.HandleFunc("/codes_of_conduct/k", func(w http.ResponseWriter, r *http.Request) {
139 testMethod(t, r, "GET")
140 testHeader(t, r, "Accept", mediaTypeCodesOfConductPreview)
141 fmt.Fprint(w, `{
142 "key": "key",
143 "name": "name",
144 "url": "url",
145 "body": "body"}`,
146 )
147 })
148
149 ctx := context.Background()
150 coc, _, err := client.GetCodeOfConduct(ctx, "k")
151 if err != nil {
152 t.Errorf("ListCodesOfConduct returned error: %v", err)
153 }
154
155 want := &CodeOfConduct{
156 Key: String("key"),
157 Name: String("name"),
158 URL: String("url"),
159 Body: String("body"),
160 }
161 if !cmp.Equal(want, coc) {
162 t.Errorf("GetCodeOfConductByKey returned %+v, want %+v", coc, want)
163 }
164
165 const methodName = "GetCodeOfConduct"
166 testBadOptions(t, methodName, func() (err error) {
167 _, _, err = client.GetCodeOfConduct(ctx, "\n")
168 return err
169 })
170
171 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
172 got, resp, err := client.GetCodeOfConduct(ctx, "k")
173 if got != nil {
174 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
175 }
176 return resp, err
177 })
178 }
179
180 func TestAPIMeta_Marshal(t *testing.T) {
181 testJSONMarshal(t, &APIMeta{}, "{}")
182
183 a := &APIMeta{
184 Hooks: []string{"h"},
185 Git: []string{"g"},
186 VerifiablePasswordAuthentication: Bool(true),
187 Pages: []string{"p"},
188 Importer: []string{"i"},
189 Actions: []string{"a"},
190 Dependabot: []string{"d"},
191 SSHKeyFingerprints: map[string]string{"a": "f"},
192 SSHKeys: []string{"k"},
193 }
194 want := `{
195 "hooks":["h"],
196 "git":["g"],
197 "verifiable_password_authentication":true,
198 "pages":["p"],
199 "importer":["i"],
200 "actions":["a"],
201 "dependabot":["d"],
202 "ssh_key_fingerprints":{"a":"f"},
203 "ssh_keys":["k"]
204 }`
205
206 testJSONMarshal(t, a, want)
207 }
208
209 func TestAPIMeta(t *testing.T) {
210 client, mux, _, teardown := setup()
211 defer teardown()
212
213 mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) {
214 testMethod(t, r, "GET")
215 fmt.Fprint(w, `{"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true}`)
216 })
217
218 ctx := context.Background()
219 meta, _, err := client.APIMeta(ctx)
220 if err != nil {
221 t.Errorf("APIMeta returned error: %v", err)
222 }
223
224 want := &APIMeta{
225 Hooks: []string{"h"},
226 Git: []string{"g"},
227 Pages: []string{"p"},
228 Importer: []string{"i"},
229 Actions: []string{"a"},
230 Dependabot: []string{"d"},
231
232 VerifiablePasswordAuthentication: Bool(true),
233 }
234 if !cmp.Equal(want, meta) {
235 t.Errorf("APIMeta returned %+v, want %+v", meta, want)
236 }
237
238 const methodName = "APIMeta"
239 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
240 got, resp, err := client.APIMeta(ctx)
241 if got != nil {
242 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
243 }
244 return resp, err
245 })
246 }
247
248 func TestOctocat(t *testing.T) {
249 client, mux, _, teardown := setup()
250 defer teardown()
251
252 input := "input"
253 output := "sample text"
254
255 mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) {
256 testMethod(t, r, "GET")
257 testFormValues(t, r, values{"s": input})
258 w.Header().Set("Content-Type", "application/octocat-stream")
259 fmt.Fprint(w, output)
260 })
261
262 ctx := context.Background()
263 got, _, err := client.Octocat(ctx, input)
264 if err != nil {
265 t.Errorf("Octocat returned error: %v", err)
266 }
267
268 if want := output; got != want {
269 t.Errorf("Octocat returned %+v, want %+v", got, want)
270 }
271
272 const methodName = "Octocat"
273 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
274 got, resp, err := client.Octocat(ctx, input)
275 if got != "" {
276 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
277 }
278 return resp, err
279 })
280 }
281
282 func TestZen(t *testing.T) {
283 client, mux, _, teardown := setup()
284 defer teardown()
285
286 output := "sample text"
287
288 mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) {
289 testMethod(t, r, "GET")
290 w.Header().Set("Content-Type", "text/plain;charset=utf-8")
291 fmt.Fprint(w, output)
292 })
293
294 ctx := context.Background()
295 got, _, err := client.Zen(ctx)
296 if err != nil {
297 t.Errorf("Zen returned error: %v", err)
298 }
299
300 if want := output; got != want {
301 t.Errorf("Zen returned %+v, want %+v", got, want)
302 }
303
304 const methodName = "Zen"
305 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
306 got, resp, err := client.Zen(ctx)
307 if got != "" {
308 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
309 }
310 return resp, err
311 })
312 }
313
314 func TestListServiceHooks(t *testing.T) {
315 client, mux, _, teardown := setup()
316 defer teardown()
317
318 mux.HandleFunc("/hooks", func(w http.ResponseWriter, r *http.Request) {
319 testMethod(t, r, "GET")
320 fmt.Fprint(w, `[{
321 "name":"n",
322 "events":["e"],
323 "supported_events":["s"],
324 "schema":[
325 ["a", "b"]
326 ]
327 }]`)
328 })
329
330 ctx := context.Background()
331 hooks, _, err := client.ListServiceHooks(ctx)
332 if err != nil {
333 t.Errorf("ListServiceHooks returned error: %v", err)
334 }
335
336 want := []*ServiceHook{{
337 Name: String("n"),
338 Events: []string{"e"},
339 SupportedEvents: []string{"s"},
340 Schema: [][]string{{"a", "b"}},
341 }}
342 if !cmp.Equal(hooks, want) {
343 t.Errorf("ListServiceHooks returned %+v, want %+v", hooks, want)
344 }
345
346 const methodName = "ListServiceHooks"
347 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
348 got, resp, err := client.ListServiceHooks(ctx)
349 if got != nil {
350 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
351 }
352 return resp, err
353 })
354 }
355
356 func TestMarkdownRequest_Marshal(t *testing.T) {
357 testJSONMarshal(t, &markdownRequest{}, "{}")
358
359 a := &markdownRequest{
360 Text: String("txt"),
361 Mode: String("mode"),
362 Context: String("ctx"),
363 }
364
365 want := `{
366 "text": "txt",
367 "mode": "mode",
368 "context": "ctx"
369 }`
370
371 testJSONMarshal(t, a, want)
372 }
373
374 func TestCodeOfConduct_Marshal(t *testing.T) {
375 testJSONMarshal(t, &CodeOfConduct{}, "{}")
376
377 a := &CodeOfConduct{
378 Name: String("name"),
379 Key: String("key"),
380 URL: String("url"),
381 Body: String("body"),
382 }
383
384 want := `{
385 "name": "name",
386 "key": "key",
387 "url": "url",
388 "body": "body"
389 }`
390
391 testJSONMarshal(t, a, want)
392 }
393
394 func TestServiceHook_Marshal(t *testing.T) {
395 testJSONMarshal(t, &ServiceHook{}, "{}")
396
397 a := &ServiceHook{
398 Name: String("name"),
399 Events: []string{"e"},
400 SupportedEvents: []string{"se"},
401 Schema: [][]string{{"g"}},
402 }
403 want := `{
404 "name": "name",
405 "events": [
406 "e"
407 ],
408 "supported_events": [
409 "se"
410 ],
411 "schema": [
412 [
413 "g"
414 ]
415 ]
416 }`
417
418 testJSONMarshal(t, a, want)
419 }
420
View as plain text