Debian Codegraph @

github/markdown_test.go

raw
1 // Copyright 2023 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 "testing"
14
15 "github.com/google/go-cmp/cmp"
16 )
17
18 func TestMarkdownService_Markdown(t *testing.T) {
19 client, mux, _, teardown := setup()
20 defer teardown()
21
22 input := &markdownRenderRequest{
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(markdownRenderRequest)
29 assertNilError(t, 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.Render(ctx, "# text #", &MarkdownOptions{
40 Mode: "gfm",
41 Context: "google/go-github",
42 })
43 if err != nil {
44 t.Errorf("Render returned error: %v", err)
45 }
46
47 if want := "<h1>text</h1>"; want != md {
48 t.Errorf("Render returned %+v, want %+v", md, want)
49 }
50
51 const methodName = "Render"
52 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
53 got, resp, err := client.Markdown.Render(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 TestMarkdownRenderRequest_Marshal(t *testing.T) {
65 testJSONMarshal(t, &markdownRenderRequest{}, "{}")
66
67 a := &markdownRenderRequest{
68 Text: String("txt"),
69 Mode: String("mode"),
70 Context: String("ctx"),
71 }
72
73 want := `{
74 "text": "txt",
75 "mode": "mode",
76 "context": "ctx"
77 }`
78
79 testJSONMarshal(t, a, want)
80 }