github.com/google/go-github/v60@v60.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  	testJSONMarshal(t, &APIMeta{}, "{}")
    19  
    20  	a := &APIMeta{
    21  		Hooks:                            []string{"h"},
    22  		Git:                              []string{"g"},
    23  		VerifiablePasswordAuthentication: Bool(true),
    24  		Pages:                            []string{"p"},
    25  		Importer:                         []string{"i"},
    26  		Actions:                          []string{"a"},
    27  		Dependabot:                       []string{"d"},
    28  		SSHKeyFingerprints:               map[string]string{"a": "f"},
    29  		SSHKeys:                          []string{"k"},
    30  		API:                              []string{"a"},
    31  		Web:                              []string{"w"},
    32  	}
    33  	want := `{
    34  		"hooks":["h"],
    35  		"git":["g"],
    36  		"verifiable_password_authentication":true,
    37  		"pages":["p"],
    38  		"importer":["i"],
    39  		"actions":["a"],
    40  		"dependabot":["d"],
    41  		"ssh_key_fingerprints":{"a":"f"},
    42  		"ssh_keys":["k"],
    43  		"api":["a"],
    44  		"web":["w"]
    45  	}`
    46  
    47  	testJSONMarshal(t, a, want)
    48  }
    49  
    50  func TestMetaService_Get(t *testing.T) {
    51  	client, mux, _, teardown := setup()
    52  	defer teardown()
    53  
    54  	mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) {
    55  		testMethod(t, r, "GET")
    56  		fmt.Fprint(w, `{"web":["w"],"api":["a"],"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true}`)
    57  	})
    58  
    59  	ctx := context.Background()
    60  	meta, _, err := client.Meta.Get(ctx)
    61  	if err != nil {
    62  		t.Errorf("Get returned error: %v", err)
    63  	}
    64  
    65  	want := &APIMeta{
    66  		Hooks:      []string{"h"},
    67  		Git:        []string{"g"},
    68  		Pages:      []string{"p"},
    69  		Importer:   []string{"i"},
    70  		Actions:    []string{"a"},
    71  		Dependabot: []string{"d"},
    72  		API:        []string{"a"},
    73  		Web:        []string{"w"},
    74  
    75  		VerifiablePasswordAuthentication: Bool(true),
    76  	}
    77  	if !cmp.Equal(want, meta) {
    78  		t.Errorf("Get returned %+v, want %+v", meta, want)
    79  	}
    80  
    81  	const methodName = "Get"
    82  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    83  		got, resp, err := client.Meta.Get(ctx)
    84  		if got != nil {
    85  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    86  		}
    87  		return resp, err
    88  	})
    89  }
    90  
    91  func TestMetaService_Octocat(t *testing.T) {
    92  	client, mux, _, teardown := setup()
    93  	defer teardown()
    94  
    95  	input := "input"
    96  	output := "sample text"
    97  
    98  	mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) {
    99  		testMethod(t, r, "GET")
   100  		testFormValues(t, r, values{"s": input})
   101  		w.Header().Set("Content-Type", "application/octocat-stream")
   102  		fmt.Fprint(w, output)
   103  	})
   104  
   105  	ctx := context.Background()
   106  	got, _, err := client.Meta.Octocat(ctx, input)
   107  	if err != nil {
   108  		t.Errorf("Octocat returned error: %v", err)
   109  	}
   110  
   111  	if want := output; got != want {
   112  		t.Errorf("Octocat returned %+v, want %+v", got, want)
   113  	}
   114  
   115  	const methodName = "Octocat"
   116  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   117  		got, resp, err := client.Meta.Octocat(ctx, input)
   118  		if got != "" {
   119  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   120  		}
   121  		return resp, err
   122  	})
   123  }
   124  
   125  func TestMetaService_Zen(t *testing.T) {
   126  	client, mux, _, teardown := setup()
   127  	defer teardown()
   128  
   129  	output := "sample text"
   130  
   131  	mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) {
   132  		testMethod(t, r, "GET")
   133  		w.Header().Set("Content-Type", "text/plain;charset=utf-8")
   134  		fmt.Fprint(w, output)
   135  	})
   136  
   137  	ctx := context.Background()
   138  	got, _, err := client.Meta.Zen(ctx)
   139  	if err != nil {
   140  		t.Errorf("Zen returned error: %v", err)
   141  	}
   142  
   143  	if want := output; got != want {
   144  		t.Errorf("Zen returned %+v, want %+v", got, want)
   145  	}
   146  
   147  	const methodName = "Zen"
   148  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   149  		got, resp, err := client.Meta.Zen(ctx)
   150  		if got != "" {
   151  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   152  		}
   153  		return resp, err
   154  	})
   155  }