...

Source file src/github.com/google/go-github/v55/github/misc_test.go

Documentation: github.com/google/go-github/v55/github

     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  	"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  		API:                              []string{"a"},
   194  		Web:                              []string{"w"},
   195  	}
   196  	want := `{
   197  		"hooks":["h"],
   198  		"git":["g"],
   199  		"verifiable_password_authentication":true,
   200  		"pages":["p"],
   201  		"importer":["i"],
   202  		"actions":["a"],
   203  		"dependabot":["d"],
   204  		"ssh_key_fingerprints":{"a":"f"},
   205  		"ssh_keys":["k"],
   206  		"api":["a"],
   207  		"web":["w"]
   208  	}`
   209  
   210  	testJSONMarshal(t, a, want)
   211  }
   212  
   213  func TestAPIMeta(t *testing.T) {
   214  	client, mux, _, teardown := setup()
   215  	defer teardown()
   216  
   217  	mux.HandleFunc("/meta", func(w http.ResponseWriter, r *http.Request) {
   218  		testMethod(t, r, "GET")
   219  		fmt.Fprint(w, `{"web":["w"],"api":["a"],"hooks":["h"], "git":["g"], "pages":["p"], "importer":["i"], "actions":["a"], "dependabot":["d"], "verifiable_password_authentication": true}`)
   220  	})
   221  
   222  	ctx := context.Background()
   223  	meta, _, err := client.APIMeta(ctx)
   224  	if err != nil {
   225  		t.Errorf("APIMeta returned error: %v", err)
   226  	}
   227  
   228  	want := &APIMeta{
   229  		Hooks:      []string{"h"},
   230  		Git:        []string{"g"},
   231  		Pages:      []string{"p"},
   232  		Importer:   []string{"i"},
   233  		Actions:    []string{"a"},
   234  		Dependabot: []string{"d"},
   235  		API:        []string{"a"},
   236  		Web:        []string{"w"},
   237  
   238  		VerifiablePasswordAuthentication: Bool(true),
   239  	}
   240  	if !cmp.Equal(want, meta) {
   241  		t.Errorf("APIMeta returned %+v, want %+v", meta, want)
   242  	}
   243  
   244  	const methodName = "APIMeta"
   245  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   246  		got, resp, err := client.APIMeta(ctx)
   247  		if got != nil {
   248  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   249  		}
   250  		return resp, err
   251  	})
   252  }
   253  
   254  func TestOctocat(t *testing.T) {
   255  	client, mux, _, teardown := setup()
   256  	defer teardown()
   257  
   258  	input := "input"
   259  	output := "sample text"
   260  
   261  	mux.HandleFunc("/octocat", func(w http.ResponseWriter, r *http.Request) {
   262  		testMethod(t, r, "GET")
   263  		testFormValues(t, r, values{"s": input})
   264  		w.Header().Set("Content-Type", "application/octocat-stream")
   265  		fmt.Fprint(w, output)
   266  	})
   267  
   268  	ctx := context.Background()
   269  	got, _, err := client.Octocat(ctx, input)
   270  	if err != nil {
   271  		t.Errorf("Octocat returned error: %v", err)
   272  	}
   273  
   274  	if want := output; got != want {
   275  		t.Errorf("Octocat returned %+v, want %+v", got, want)
   276  	}
   277  
   278  	const methodName = "Octocat"
   279  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   280  		got, resp, err := client.Octocat(ctx, input)
   281  		if got != "" {
   282  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   283  		}
   284  		return resp, err
   285  	})
   286  }
   287  
   288  func TestZen(t *testing.T) {
   289  	client, mux, _, teardown := setup()
   290  	defer teardown()
   291  
   292  	output := "sample text"
   293  
   294  	mux.HandleFunc("/zen", func(w http.ResponseWriter, r *http.Request) {
   295  		testMethod(t, r, "GET")
   296  		w.Header().Set("Content-Type", "text/plain;charset=utf-8")
   297  		fmt.Fprint(w, output)
   298  	})
   299  
   300  	ctx := context.Background()
   301  	got, _, err := client.Zen(ctx)
   302  	if err != nil {
   303  		t.Errorf("Zen returned error: %v", err)
   304  	}
   305  
   306  	if want := output; got != want {
   307  		t.Errorf("Zen returned %+v, want %+v", got, want)
   308  	}
   309  
   310  	const methodName = "Zen"
   311  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   312  		got, resp, err := client.Zen(ctx)
   313  		if got != "" {
   314  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   315  		}
   316  		return resp, err
   317  	})
   318  }
   319  
   320  func TestListServiceHooks(t *testing.T) {
   321  	client, mux, _, teardown := setup()
   322  	defer teardown()
   323  
   324  	mux.HandleFunc("/hooks", func(w http.ResponseWriter, r *http.Request) {
   325  		testMethod(t, r, "GET")
   326  		fmt.Fprint(w, `[{
   327  			"name":"n",
   328  			"events":["e"],
   329  			"supported_events":["s"],
   330  			"schema":[
   331  			  ["a", "b"]
   332  			]
   333  		}]`)
   334  	})
   335  
   336  	ctx := context.Background()
   337  	hooks, _, err := client.ListServiceHooks(ctx)
   338  	if err != nil {
   339  		t.Errorf("ListServiceHooks returned error: %v", err)
   340  	}
   341  
   342  	want := []*ServiceHook{{
   343  		Name:            String("n"),
   344  		Events:          []string{"e"},
   345  		SupportedEvents: []string{"s"},
   346  		Schema:          [][]string{{"a", "b"}},
   347  	}}
   348  	if !cmp.Equal(hooks, want) {
   349  		t.Errorf("ListServiceHooks returned %+v, want %+v", hooks, want)
   350  	}
   351  
   352  	const methodName = "ListServiceHooks"
   353  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   354  		got, resp, err := client.ListServiceHooks(ctx)
   355  		if got != nil {
   356  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   357  		}
   358  		return resp, err
   359  	})
   360  }
   361  
   362  func TestMarkdownRequest_Marshal(t *testing.T) {
   363  	testJSONMarshal(t, &markdownRequest{}, "{}")
   364  
   365  	a := &markdownRequest{
   366  		Text:    String("txt"),
   367  		Mode:    String("mode"),
   368  		Context: String("ctx"),
   369  	}
   370  
   371  	want := `{
   372  		"text": "txt",
   373  		"mode": "mode",
   374  		"context": "ctx"
   375  	}`
   376  
   377  	testJSONMarshal(t, a, want)
   378  }
   379  
   380  func TestCodeOfConduct_Marshal(t *testing.T) {
   381  	testJSONMarshal(t, &CodeOfConduct{}, "{}")
   382  
   383  	a := &CodeOfConduct{
   384  		Name: String("name"),
   385  		Key:  String("key"),
   386  		URL:  String("url"),
   387  		Body: String("body"),
   388  	}
   389  
   390  	want := `{
   391  		"name": "name",
   392  		"key": "key",
   393  		"url": "url",
   394  		"body": "body"
   395  	}`
   396  
   397  	testJSONMarshal(t, a, want)
   398  }
   399  
   400  func TestServiceHook_Marshal(t *testing.T) {
   401  	testJSONMarshal(t, &ServiceHook{}, "{}")
   402  
   403  	a := &ServiceHook{
   404  		Name:            String("name"),
   405  		Events:          []string{"e"},
   406  		SupportedEvents: []string{"se"},
   407  		Schema:          [][]string{{"g"}},
   408  	}
   409  	want := `{
   410  		"name": "name",
   411  		"events": [
   412  			"e"
   413  		],
   414  		"supported_events": [
   415  			"se"
   416  		],
   417  		"schema": [
   418  			[
   419  				"g"
   420  			]
   421  		]
   422  	}`
   423  
   424  	testJSONMarshal(t, a, want)
   425  }
   426  

View as plain text