github.com/google/go-github/v74@v74.0.0/github/markdown_test.go (about)

     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  	t.Parallel()
    20  	client, mux, _ := setup(t)
    21  
    22  	input := &markdownRenderRequest{
    23  		Text:    Ptr("# text #"),
    24  		Mode:    Ptr("gfm"),
    25  		Context: Ptr("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  	t.Parallel()
    66  	testJSONMarshal(t, &markdownRenderRequest{}, "{}")
    67  
    68  	a := &markdownRenderRequest{
    69  		Text:    Ptr("txt"),
    70  		Mode:    Ptr("mode"),
    71  		Context: Ptr("ctx"),
    72  	}
    73  
    74  	want := `{
    75  		"text": "txt",
    76  		"mode": "mode",
    77  		"context": "ctx"
    78  	}`
    79  
    80  	testJSONMarshal(t, a, want)
    81  }