...

Source file src/github.com/google/go-github/v47/github/gists_test.go

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

     1  // Copyright 2013 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  	"time"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  func TestGist_Marshal(t *testing.T) {
    20  	testJSONMarshal(t, &Gist{}, "{}")
    21  
    22  	createdAt := time.Date(2010, time.February, 10, 10, 10, 0, 0, time.UTC)
    23  	updatedAt := time.Date(2010, time.February, 10, 10, 10, 0, 0, time.UTC)
    24  
    25  	u := &Gist{
    26  		ID:          String("i"),
    27  		Description: String("description"),
    28  		Public:      Bool(true),
    29  		Owner: &User{
    30  			Login:       String("ll"),
    31  			ID:          Int64(123),
    32  			AvatarURL:   String("a"),
    33  			GravatarID:  String("g"),
    34  			Name:        String("n"),
    35  			Company:     String("c"),
    36  			Blog:        String("b"),
    37  			Location:    String("l"),
    38  			Email:       String("e"),
    39  			Hireable:    Bool(true),
    40  			PublicRepos: Int(1),
    41  			Followers:   Int(1),
    42  			Following:   Int(1),
    43  			CreatedAt:   &Timestamp{referenceTime},
    44  			URL:         String("u"),
    45  		},
    46  		Files: map[GistFilename]GistFile{
    47  			"gistfile.py": {
    48  				Size:     Int(167),
    49  				Filename: String("gistfile.py"),
    50  				Language: String("Python"),
    51  				Type:     String("application/x-python"),
    52  				RawURL:   String("raw-url"),
    53  				Content:  String("c"),
    54  			},
    55  		},
    56  		Comments:   Int(1),
    57  		HTMLURL:    String("html-url"),
    58  		GitPullURL: String("gitpull-url"),
    59  		GitPushURL: String("gitpush-url"),
    60  		CreatedAt:  &createdAt,
    61  		UpdatedAt:  &updatedAt,
    62  		NodeID:     String("node"),
    63  	}
    64  
    65  	want := `{
    66  		"id": "i",
    67  		"description": "description",
    68  		"public": true,
    69  		"owner": {
    70  			"login": "ll",
    71  			"id": 123,
    72  			"avatar_url": "a",
    73  			"gravatar_id": "g",
    74  			"name": "n",
    75  			"company": "c",
    76  			"blog": "b",
    77  			"location": "l",
    78  			"email": "e",
    79  			"hireable": true,
    80  			"public_repos": 1,
    81  			"followers": 1,
    82  			"following": 1,
    83  			"created_at": ` + referenceTimeStr + `,
    84  			"url": "u"
    85  		},
    86  		"files": {
    87  			"gistfile.py": {
    88  				"size": 167,
    89  				"filename": "gistfile.py",
    90  				"language": "Python",
    91  				"type": "application/x-python",
    92  				"raw_url": "raw-url",
    93  				"content": "c"
    94  			}
    95  		},
    96  		"comments": 1,
    97  		"html_url": "html-url",
    98  		"git_pull_url": "gitpull-url",
    99  		"git_push_url": "gitpush-url",
   100  		"created_at": "2010-02-10T10:10:00Z",
   101  		"updated_at": "2010-02-10T10:10:00Z",
   102  		"node_id": "node"
   103  	}`
   104  
   105  	testJSONMarshal(t, u, want)
   106  }
   107  
   108  func TestGistCommit_Marshal(t *testing.T) {
   109  	testJSONMarshal(t, &GistCommit{}, "{}")
   110  
   111  	u := &GistCommit{
   112  		URL:     String("u"),
   113  		Version: String("v"),
   114  		User: &User{
   115  			Login:       String("ll"),
   116  			ID:          Int64(123),
   117  			AvatarURL:   String("a"),
   118  			GravatarID:  String("g"),
   119  			Name:        String("n"),
   120  			Company:     String("c"),
   121  			Blog:        String("b"),
   122  			Location:    String("l"),
   123  			Email:       String("e"),
   124  			Hireable:    Bool(true),
   125  			PublicRepos: Int(1),
   126  			Followers:   Int(1),
   127  			Following:   Int(1),
   128  			CreatedAt:   &Timestamp{referenceTime},
   129  			URL:         String("u"),
   130  		},
   131  		ChangeStatus: &CommitStats{
   132  			Additions: Int(1),
   133  			Deletions: Int(1),
   134  			Total:     Int(2),
   135  		},
   136  		CommittedAt: &Timestamp{referenceTime},
   137  		NodeID:      String("node"),
   138  	}
   139  
   140  	want := `{
   141  		"url": "u",
   142  		"version": "v",
   143  		"user": {
   144  			"login": "ll",
   145  			"id": 123,
   146  			"avatar_url": "a",
   147  			"gravatar_id": "g",
   148  			"name": "n",
   149  			"company": "c",
   150  			"blog": "b",
   151  			"location": "l",
   152  			"email": "e",
   153  			"hireable": true,
   154  			"public_repos": 1,
   155  			"followers": 1,
   156  			"following": 1,
   157  			"created_at": ` + referenceTimeStr + `,
   158  			"url": "u"
   159  		},
   160  		"change_status": {
   161  			"additions": 1,
   162  			"deletions": 1,
   163  			"total": 2
   164  		},
   165  		"committed_at": ` + referenceTimeStr + `,
   166  		"node_id": "node"
   167  	}`
   168  
   169  	testJSONMarshal(t, u, want)
   170  }
   171  
   172  func TestGistFork_Marshal(t *testing.T) {
   173  	testJSONMarshal(t, &GistFork{}, "{}")
   174  
   175  	u := &GistFork{
   176  		URL: String("u"),
   177  		User: &User{
   178  			Login:       String("ll"),
   179  			ID:          Int64(123),
   180  			AvatarURL:   String("a"),
   181  			GravatarID:  String("g"),
   182  			Name:        String("n"),
   183  			Company:     String("c"),
   184  			Blog:        String("b"),
   185  			Location:    String("l"),
   186  			Email:       String("e"),
   187  			Hireable:    Bool(true),
   188  			PublicRepos: Int(1),
   189  			Followers:   Int(1),
   190  			Following:   Int(1),
   191  			CreatedAt:   &Timestamp{referenceTime},
   192  			URL:         String("u"),
   193  		},
   194  		ID:        String("id"),
   195  		CreatedAt: &Timestamp{referenceTime},
   196  		UpdatedAt: &Timestamp{referenceTime},
   197  		NodeID:    String("node"),
   198  	}
   199  
   200  	want := `{
   201  		"url": "u",
   202  		"user": {
   203  			"login": "ll",
   204  			"id": 123,
   205  			"avatar_url": "a",
   206  			"gravatar_id": "g",
   207  			"name": "n",
   208  			"company": "c",
   209  			"blog": "b",
   210  			"location": "l",
   211  			"email": "e",
   212  			"hireable": true,
   213  			"public_repos": 1,
   214  			"followers": 1,
   215  			"following": 1,
   216  			"created_at": ` + referenceTimeStr + `,
   217  			"url": "u"
   218  		},
   219  		"id": "id",
   220  		"created_at": ` + referenceTimeStr + `,
   221  		"updated_at": ` + referenceTimeStr + `,
   222  		"node_id": "node"
   223  	}`
   224  
   225  	testJSONMarshal(t, u, want)
   226  }
   227  
   228  func TestGistsService_List_specifiedUser(t *testing.T) {
   229  	client, mux, _, teardown := setup()
   230  	defer teardown()
   231  
   232  	since := "2013-01-01T00:00:00Z"
   233  
   234  	mux.HandleFunc("/users/u/gists", func(w http.ResponseWriter, r *http.Request) {
   235  		testMethod(t, r, "GET")
   236  		testFormValues(t, r, values{
   237  			"since": since,
   238  		})
   239  		fmt.Fprint(w, `[{"id": "1"}]`)
   240  	})
   241  
   242  	opt := &GistListOptions{Since: time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC)}
   243  	ctx := context.Background()
   244  	gists, _, err := client.Gists.List(ctx, "u", opt)
   245  	if err != nil {
   246  		t.Errorf("Gists.List returned error: %v", err)
   247  	}
   248  
   249  	want := []*Gist{{ID: String("1")}}
   250  	if !cmp.Equal(gists, want) {
   251  		t.Errorf("Gists.List returned %+v, want %+v", gists, want)
   252  	}
   253  
   254  	const methodName = "List"
   255  	testBadOptions(t, methodName, func() (err error) {
   256  		_, _, err = client.Gists.List(ctx, "\n", opt)
   257  		return err
   258  	})
   259  
   260  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   261  		got, resp, err := client.Gists.List(ctx, "u", opt)
   262  		if got != nil {
   263  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   264  		}
   265  		return resp, err
   266  	})
   267  }
   268  
   269  func TestGistsService_List_authenticatedUser(t *testing.T) {
   270  	client, mux, _, teardown := setup()
   271  	defer teardown()
   272  
   273  	mux.HandleFunc("/gists", func(w http.ResponseWriter, r *http.Request) {
   274  		testMethod(t, r, "GET")
   275  		fmt.Fprint(w, `[{"id": "1"}]`)
   276  	})
   277  
   278  	ctx := context.Background()
   279  	gists, _, err := client.Gists.List(ctx, "", nil)
   280  	if err != nil {
   281  		t.Errorf("Gists.List returned error: %v", err)
   282  	}
   283  
   284  	want := []*Gist{{ID: String("1")}}
   285  	if !cmp.Equal(gists, want) {
   286  		t.Errorf("Gists.List returned %+v, want %+v", gists, want)
   287  	}
   288  
   289  	const methodName = "List"
   290  	testBadOptions(t, methodName, func() (err error) {
   291  		_, _, err = client.Gists.List(ctx, "\n", nil)
   292  		return err
   293  	})
   294  
   295  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   296  		got, resp, err := client.Gists.List(ctx, "", nil)
   297  		if got != nil {
   298  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   299  		}
   300  		return resp, err
   301  	})
   302  }
   303  
   304  func TestGistsService_List_invalidUser(t *testing.T) {
   305  	client, _, _, teardown := setup()
   306  	defer teardown()
   307  
   308  	ctx := context.Background()
   309  	_, _, err := client.Gists.List(ctx, "%", nil)
   310  	testURLParseError(t, err)
   311  }
   312  
   313  func TestGistsService_ListAll(t *testing.T) {
   314  	client, mux, _, teardown := setup()
   315  	defer teardown()
   316  
   317  	since := "2013-01-01T00:00:00Z"
   318  
   319  	mux.HandleFunc("/gists/public", func(w http.ResponseWriter, r *http.Request) {
   320  		testMethod(t, r, "GET")
   321  		testFormValues(t, r, values{
   322  			"since": since,
   323  		})
   324  		fmt.Fprint(w, `[{"id": "1"}]`)
   325  	})
   326  
   327  	opt := &GistListOptions{Since: time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC)}
   328  	ctx := context.Background()
   329  	gists, _, err := client.Gists.ListAll(ctx, opt)
   330  	if err != nil {
   331  		t.Errorf("Gists.ListAll returned error: %v", err)
   332  	}
   333  
   334  	want := []*Gist{{ID: String("1")}}
   335  	if !cmp.Equal(gists, want) {
   336  		t.Errorf("Gists.ListAll returned %+v, want %+v", gists, want)
   337  	}
   338  
   339  	const methodName = "ListAll"
   340  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   341  		got, resp, err := client.Gists.ListAll(ctx, opt)
   342  		if got != nil {
   343  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   344  		}
   345  		return resp, err
   346  	})
   347  }
   348  
   349  func TestGistsService_ListStarred(t *testing.T) {
   350  	client, mux, _, teardown := setup()
   351  	defer teardown()
   352  
   353  	since := "2013-01-01T00:00:00Z"
   354  
   355  	mux.HandleFunc("/gists/starred", func(w http.ResponseWriter, r *http.Request) {
   356  		testMethod(t, r, "GET")
   357  		testFormValues(t, r, values{
   358  			"since": since,
   359  		})
   360  		fmt.Fprint(w, `[{"id": "1"}]`)
   361  	})
   362  
   363  	opt := &GistListOptions{Since: time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC)}
   364  	ctx := context.Background()
   365  	gists, _, err := client.Gists.ListStarred(ctx, opt)
   366  	if err != nil {
   367  		t.Errorf("Gists.ListStarred returned error: %v", err)
   368  	}
   369  
   370  	want := []*Gist{{ID: String("1")}}
   371  	if !cmp.Equal(gists, want) {
   372  		t.Errorf("Gists.ListStarred returned %+v, want %+v", gists, want)
   373  	}
   374  
   375  	const methodName = "ListStarred"
   376  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   377  		got, resp, err := client.Gists.ListStarred(ctx, opt)
   378  		if got != nil {
   379  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   380  		}
   381  		return resp, err
   382  	})
   383  }
   384  
   385  func TestGistsService_Get(t *testing.T) {
   386  	client, mux, _, teardown := setup()
   387  	defer teardown()
   388  
   389  	mux.HandleFunc("/gists/1", func(w http.ResponseWriter, r *http.Request) {
   390  		testMethod(t, r, "GET")
   391  		fmt.Fprint(w, `{"id": "1"}`)
   392  	})
   393  
   394  	ctx := context.Background()
   395  	gist, _, err := client.Gists.Get(ctx, "1")
   396  	if err != nil {
   397  		t.Errorf("Gists.Get returned error: %v", err)
   398  	}
   399  
   400  	want := &Gist{ID: String("1")}
   401  	if !cmp.Equal(gist, want) {
   402  		t.Errorf("Gists.Get returned %+v, want %+v", gist, want)
   403  	}
   404  
   405  	const methodName = "Get"
   406  	testBadOptions(t, methodName, func() (err error) {
   407  		_, _, err = client.Gists.Get(ctx, "\n")
   408  		return err
   409  	})
   410  
   411  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   412  		got, resp, err := client.Gists.Get(ctx, "1")
   413  		if got != nil {
   414  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   415  		}
   416  		return resp, err
   417  	})
   418  }
   419  
   420  func TestGistsService_Get_invalidID(t *testing.T) {
   421  	client, _, _, teardown := setup()
   422  	defer teardown()
   423  
   424  	ctx := context.Background()
   425  	_, _, err := client.Gists.Get(ctx, "%")
   426  	testURLParseError(t, err)
   427  }
   428  
   429  func TestGistsService_GetRevision(t *testing.T) {
   430  	client, mux, _, teardown := setup()
   431  	defer teardown()
   432  
   433  	mux.HandleFunc("/gists/1/s", func(w http.ResponseWriter, r *http.Request) {
   434  		testMethod(t, r, "GET")
   435  		fmt.Fprint(w, `{"id": "1"}`)
   436  	})
   437  
   438  	ctx := context.Background()
   439  	gist, _, err := client.Gists.GetRevision(ctx, "1", "s")
   440  	if err != nil {
   441  		t.Errorf("Gists.Get returned error: %v", err)
   442  	}
   443  
   444  	want := &Gist{ID: String("1")}
   445  	if !cmp.Equal(gist, want) {
   446  		t.Errorf("Gists.Get returned %+v, want %+v", gist, want)
   447  	}
   448  
   449  	const methodName = "GetRevision"
   450  	testBadOptions(t, methodName, func() (err error) {
   451  		_, _, err = client.Gists.GetRevision(ctx, "\n", "\n")
   452  		return err
   453  	})
   454  
   455  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   456  		got, resp, err := client.Gists.GetRevision(ctx, "1", "s")
   457  		if got != nil {
   458  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   459  		}
   460  		return resp, err
   461  	})
   462  }
   463  
   464  func TestGistsService_GetRevision_invalidID(t *testing.T) {
   465  	client, _, _, teardown := setup()
   466  	defer teardown()
   467  
   468  	ctx := context.Background()
   469  	_, _, err := client.Gists.GetRevision(ctx, "%", "%")
   470  	testURLParseError(t, err)
   471  }
   472  
   473  func TestGistsService_Create(t *testing.T) {
   474  	client, mux, _, teardown := setup()
   475  	defer teardown()
   476  
   477  	input := &Gist{
   478  		Description: String("Gist description"),
   479  		Public:      Bool(false),
   480  		Files: map[GistFilename]GistFile{
   481  			"test.txt": {Content: String("Gist file content")},
   482  		},
   483  	}
   484  
   485  	mux.HandleFunc("/gists", func(w http.ResponseWriter, r *http.Request) {
   486  		v := new(Gist)
   487  		json.NewDecoder(r.Body).Decode(v)
   488  
   489  		testMethod(t, r, "POST")
   490  		if !cmp.Equal(v, input) {
   491  			t.Errorf("Request body = %+v, want %+v", v, input)
   492  		}
   493  
   494  		fmt.Fprint(w,
   495  			`
   496  			{
   497  				"id": "1",
   498  				"description": "Gist description",
   499  				"public": false,
   500  				"files": {
   501  					"test.txt": {
   502  						"filename": "test.txt"
   503  					}
   504  				}
   505  			}`)
   506  	})
   507  
   508  	ctx := context.Background()
   509  	gist, _, err := client.Gists.Create(ctx, input)
   510  	if err != nil {
   511  		t.Errorf("Gists.Create returned error: %v", err)
   512  	}
   513  
   514  	want := &Gist{
   515  		ID:          String("1"),
   516  		Description: String("Gist description"),
   517  		Public:      Bool(false),
   518  		Files: map[GistFilename]GistFile{
   519  			"test.txt": {Filename: String("test.txt")},
   520  		},
   521  	}
   522  	if !cmp.Equal(gist, want) {
   523  		t.Errorf("Gists.Create returned %+v, want %+v", gist, want)
   524  	}
   525  
   526  	const methodName = "Create"
   527  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   528  		got, resp, err := client.Gists.Create(ctx, input)
   529  		if got != nil {
   530  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   531  		}
   532  		return resp, err
   533  	})
   534  }
   535  
   536  func TestGistsService_Edit(t *testing.T) {
   537  	client, mux, _, teardown := setup()
   538  	defer teardown()
   539  
   540  	input := &Gist{
   541  		Description: String("New description"),
   542  		Files: map[GistFilename]GistFile{
   543  			"new.txt": {Content: String("new file content")},
   544  		},
   545  	}
   546  
   547  	mux.HandleFunc("/gists/1", func(w http.ResponseWriter, r *http.Request) {
   548  		v := new(Gist)
   549  		json.NewDecoder(r.Body).Decode(v)
   550  
   551  		testMethod(t, r, "PATCH")
   552  		if !cmp.Equal(v, input) {
   553  			t.Errorf("Request body = %+v, want %+v", v, input)
   554  		}
   555  
   556  		fmt.Fprint(w,
   557  			`
   558  			{
   559  				"id": "1",
   560  				"description": "new description",
   561  				"public": false,
   562  				"files": {
   563  					"test.txt": {
   564  						"filename": "test.txt"
   565  					},
   566  					"new.txt": {
   567  						"filename": "new.txt"
   568  					}
   569  				}
   570  			}`)
   571  	})
   572  
   573  	ctx := context.Background()
   574  	gist, _, err := client.Gists.Edit(ctx, "1", input)
   575  	if err != nil {
   576  		t.Errorf("Gists.Edit returned error: %v", err)
   577  	}
   578  
   579  	want := &Gist{
   580  		ID:          String("1"),
   581  		Description: String("new description"),
   582  		Public:      Bool(false),
   583  		Files: map[GistFilename]GistFile{
   584  			"test.txt": {Filename: String("test.txt")},
   585  			"new.txt":  {Filename: String("new.txt")},
   586  		},
   587  	}
   588  	if !cmp.Equal(gist, want) {
   589  		t.Errorf("Gists.Edit returned %+v, want %+v", gist, want)
   590  	}
   591  
   592  	const methodName = "Edit"
   593  	testBadOptions(t, methodName, func() (err error) {
   594  		_, _, err = client.Gists.Edit(ctx, "\n", input)
   595  		return err
   596  	})
   597  
   598  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   599  		got, resp, err := client.Gists.Edit(ctx, "1", input)
   600  		if got != nil {
   601  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   602  		}
   603  		return resp, err
   604  	})
   605  }
   606  
   607  func TestGistsService_Edit_invalidID(t *testing.T) {
   608  	client, _, _, teardown := setup()
   609  	defer teardown()
   610  
   611  	ctx := context.Background()
   612  	_, _, err := client.Gists.Edit(ctx, "%", nil)
   613  	testURLParseError(t, err)
   614  }
   615  
   616  func TestGistsService_ListCommits(t *testing.T) {
   617  	client, mux, _, teardown := setup()
   618  	defer teardown()
   619  
   620  	mux.HandleFunc("/gists/1/commits", func(w http.ResponseWriter, r *http.Request) {
   621  		testMethod(t, r, "GET")
   622  		testFormValues(t, r, nil)
   623  		fmt.Fprint(w, `
   624  		  [
   625  		    {
   626  		      "url": "https://api.github.com/gists/1/1",
   627  		      "version": "1",
   628  		      "user": {
   629  		        "id": 1
   630  		      },
   631  		      "change_status": {
   632  		        "deletions": 0,
   633  		        "additions": 180,
   634  		        "total": 180
   635  		      },
   636  		      "committed_at": "2010-01-01T00:00:00Z"
   637  		    }
   638  		  ]
   639  		`)
   640  	})
   641  
   642  	ctx := context.Background()
   643  	gistCommits, _, err := client.Gists.ListCommits(ctx, "1", nil)
   644  	if err != nil {
   645  		t.Errorf("Gists.ListCommits returned error: %v", err)
   646  	}
   647  
   648  	want := []*GistCommit{{
   649  		URL:         String("https://api.github.com/gists/1/1"),
   650  		Version:     String("1"),
   651  		User:        &User{ID: Int64(1)},
   652  		CommittedAt: &Timestamp{time.Date(2010, time.January, 1, 00, 00, 00, 0, time.UTC)},
   653  		ChangeStatus: &CommitStats{
   654  			Additions: Int(180),
   655  			Deletions: Int(0),
   656  			Total:     Int(180),
   657  		}}}
   658  
   659  	if !cmp.Equal(gistCommits, want) {
   660  		t.Errorf("Gists.ListCommits returned %+v, want %+v", gistCommits, want)
   661  	}
   662  
   663  	const methodName = "ListCommits"
   664  	testBadOptions(t, methodName, func() (err error) {
   665  		_, _, err = client.Gists.ListCommits(ctx, "\n", nil)
   666  		return err
   667  	})
   668  
   669  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   670  		got, resp, err := client.Gists.ListCommits(ctx, "1", nil)
   671  		if got != nil {
   672  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   673  		}
   674  		return resp, err
   675  	})
   676  }
   677  
   678  func TestGistsService_ListCommits_withOptions(t *testing.T) {
   679  	client, mux, _, teardown := setup()
   680  	defer teardown()
   681  
   682  	mux.HandleFunc("/gists/1/commits", func(w http.ResponseWriter, r *http.Request) {
   683  		testMethod(t, r, "GET")
   684  		testFormValues(t, r, values{
   685  			"page": "2",
   686  		})
   687  		fmt.Fprint(w, `[]`)
   688  	})
   689  
   690  	ctx := context.Background()
   691  	_, _, err := client.Gists.ListCommits(ctx, "1", &ListOptions{Page: 2})
   692  	if err != nil {
   693  		t.Errorf("Gists.ListCommits returned error: %v", err)
   694  	}
   695  
   696  	const methodName = "ListCommits"
   697  	testBadOptions(t, methodName, func() (err error) {
   698  		_, _, err = client.Gists.ListCommits(ctx, "\n", &ListOptions{Page: 2})
   699  		return err
   700  	})
   701  
   702  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   703  		got, resp, err := client.Gists.ListCommits(ctx, "1", &ListOptions{Page: 2})
   704  		if got != nil {
   705  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   706  		}
   707  		return resp, err
   708  	})
   709  }
   710  
   711  func TestGistsService_ListCommits_invalidID(t *testing.T) {
   712  	client, _, _, teardown := setup()
   713  	defer teardown()
   714  
   715  	ctx := context.Background()
   716  	_, _, err := client.Gists.ListCommits(ctx, "%", nil)
   717  	testURLParseError(t, err)
   718  }
   719  
   720  func TestGistsService_Delete(t *testing.T) {
   721  	client, mux, _, teardown := setup()
   722  	defer teardown()
   723  
   724  	mux.HandleFunc("/gists/1", func(w http.ResponseWriter, r *http.Request) {
   725  		testMethod(t, r, "DELETE")
   726  	})
   727  
   728  	ctx := context.Background()
   729  	_, err := client.Gists.Delete(ctx, "1")
   730  	if err != nil {
   731  		t.Errorf("Gists.Delete returned error: %v", err)
   732  	}
   733  
   734  	const methodName = "Delete"
   735  	testBadOptions(t, methodName, func() (err error) {
   736  		_, err = client.Gists.Delete(ctx, "\n")
   737  		return err
   738  	})
   739  
   740  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   741  		return client.Gists.Delete(ctx, "1")
   742  	})
   743  }
   744  
   745  func TestGistsService_Delete_invalidID(t *testing.T) {
   746  	client, _, _, teardown := setup()
   747  	defer teardown()
   748  
   749  	ctx := context.Background()
   750  	_, err := client.Gists.Delete(ctx, "%")
   751  	testURLParseError(t, err)
   752  }
   753  
   754  func TestGistsService_Star(t *testing.T) {
   755  	client, mux, _, teardown := setup()
   756  	defer teardown()
   757  
   758  	mux.HandleFunc("/gists/1/star", func(w http.ResponseWriter, r *http.Request) {
   759  		testMethod(t, r, "PUT")
   760  	})
   761  
   762  	ctx := context.Background()
   763  	_, err := client.Gists.Star(ctx, "1")
   764  	if err != nil {
   765  		t.Errorf("Gists.Star returned error: %v", err)
   766  	}
   767  
   768  	const methodName = "Star"
   769  	testBadOptions(t, methodName, func() (err error) {
   770  		_, err = client.Gists.Star(ctx, "\n")
   771  		return err
   772  	})
   773  
   774  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   775  		return client.Gists.Star(ctx, "1")
   776  	})
   777  }
   778  
   779  func TestGistsService_Star_invalidID(t *testing.T) {
   780  	client, _, _, teardown := setup()
   781  	defer teardown()
   782  
   783  	ctx := context.Background()
   784  	_, err := client.Gists.Star(ctx, "%")
   785  	testURLParseError(t, err)
   786  }
   787  
   788  func TestGistsService_Unstar(t *testing.T) {
   789  	client, mux, _, teardown := setup()
   790  	defer teardown()
   791  
   792  	mux.HandleFunc("/gists/1/star", func(w http.ResponseWriter, r *http.Request) {
   793  		testMethod(t, r, "DELETE")
   794  	})
   795  
   796  	ctx := context.Background()
   797  	_, err := client.Gists.Unstar(ctx, "1")
   798  	if err != nil {
   799  		t.Errorf("Gists.Unstar returned error: %v", err)
   800  	}
   801  
   802  	const methodName = "Unstar"
   803  	testBadOptions(t, methodName, func() (err error) {
   804  		_, err = client.Gists.Unstar(ctx, "\n")
   805  		return err
   806  	})
   807  
   808  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   809  		return client.Gists.Unstar(ctx, "1")
   810  	})
   811  }
   812  
   813  func TestGistsService_Unstar_invalidID(t *testing.T) {
   814  	client, _, _, teardown := setup()
   815  	defer teardown()
   816  
   817  	ctx := context.Background()
   818  	_, err := client.Gists.Unstar(ctx, "%")
   819  	testURLParseError(t, err)
   820  }
   821  
   822  func TestGistsService_IsStarred_hasStar(t *testing.T) {
   823  	client, mux, _, teardown := setup()
   824  	defer teardown()
   825  
   826  	mux.HandleFunc("/gists/1/star", func(w http.ResponseWriter, r *http.Request) {
   827  		testMethod(t, r, "GET")
   828  		w.WriteHeader(http.StatusNoContent)
   829  	})
   830  
   831  	ctx := context.Background()
   832  	star, _, err := client.Gists.IsStarred(ctx, "1")
   833  	if err != nil {
   834  		t.Errorf("Gists.Starred returned error: %v", err)
   835  	}
   836  	if want := true; star != want {
   837  		t.Errorf("Gists.Starred returned %+v, want %+v", star, want)
   838  	}
   839  
   840  	const methodName = "IsStarred"
   841  	testBadOptions(t, methodName, func() (err error) {
   842  		_, _, err = client.Gists.IsStarred(ctx, "\n")
   843  		return err
   844  	})
   845  
   846  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   847  		got, resp, err := client.Gists.IsStarred(ctx, "1")
   848  		if got {
   849  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
   850  		}
   851  		return resp, err
   852  	})
   853  }
   854  
   855  func TestGistsService_IsStarred_noStar(t *testing.T) {
   856  	client, mux, _, teardown := setup()
   857  	defer teardown()
   858  
   859  	mux.HandleFunc("/gists/1/star", func(w http.ResponseWriter, r *http.Request) {
   860  		testMethod(t, r, "GET")
   861  		w.WriteHeader(http.StatusNotFound)
   862  	})
   863  
   864  	ctx := context.Background()
   865  	star, _, err := client.Gists.IsStarred(ctx, "1")
   866  	if err != nil {
   867  		t.Errorf("Gists.Starred returned error: %v", err)
   868  	}
   869  	if want := false; star != want {
   870  		t.Errorf("Gists.Starred returned %+v, want %+v", star, want)
   871  	}
   872  
   873  	const methodName = "IsStarred"
   874  	testBadOptions(t, methodName, func() (err error) {
   875  		_, _, err = client.Gists.IsStarred(ctx, "\n")
   876  		return err
   877  	})
   878  
   879  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   880  		got, resp, err := client.Gists.IsStarred(ctx, "1")
   881  		if got {
   882  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
   883  		}
   884  		return resp, err
   885  	})
   886  }
   887  
   888  func TestGistsService_IsStarred_invalidID(t *testing.T) {
   889  	client, _, _, teardown := setup()
   890  	defer teardown()
   891  
   892  	ctx := context.Background()
   893  	_, _, err := client.Gists.IsStarred(ctx, "%")
   894  	testURLParseError(t, err)
   895  }
   896  
   897  func TestGistsService_Fork(t *testing.T) {
   898  	client, mux, _, teardown := setup()
   899  	defer teardown()
   900  
   901  	mux.HandleFunc("/gists/1/forks", func(w http.ResponseWriter, r *http.Request) {
   902  		testMethod(t, r, "POST")
   903  		fmt.Fprint(w, `{"id": "2"}`)
   904  	})
   905  
   906  	ctx := context.Background()
   907  	gist, _, err := client.Gists.Fork(ctx, "1")
   908  	if err != nil {
   909  		t.Errorf("Gists.Fork returned error: %v", err)
   910  	}
   911  
   912  	want := &Gist{ID: String("2")}
   913  	if !cmp.Equal(gist, want) {
   914  		t.Errorf("Gists.Fork returned %+v, want %+v", gist, want)
   915  	}
   916  
   917  	const methodName = "Fork"
   918  	testBadOptions(t, methodName, func() (err error) {
   919  		_, _, err = client.Gists.Fork(ctx, "\n")
   920  		return err
   921  	})
   922  
   923  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   924  		got, resp, err := client.Gists.Fork(ctx, "1")
   925  		if got != nil {
   926  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   927  		}
   928  		return resp, err
   929  	})
   930  }
   931  
   932  func TestGistsService_ListForks(t *testing.T) {
   933  	client, mux, _, teardown := setup()
   934  	defer teardown()
   935  
   936  	mux.HandleFunc("/gists/1/forks", func(w http.ResponseWriter, r *http.Request) {
   937  		testMethod(t, r, "GET")
   938  		testFormValues(t, r, nil)
   939  		fmt.Fprint(w, `
   940  		  [
   941  		    {"url": "https://api.github.com/gists/1",
   942  		     "user": {"id": 1},
   943  		     "id": "1",
   944  		     "created_at": "2010-01-01T00:00:00Z",
   945  		     "updated_at": "2013-01-01T00:00:00Z"
   946  		    }
   947  		  ]
   948  		`)
   949  	})
   950  
   951  	ctx := context.Background()
   952  	gistForks, _, err := client.Gists.ListForks(ctx, "1", nil)
   953  	if err != nil {
   954  		t.Errorf("Gists.ListForks returned error: %v", err)
   955  	}
   956  
   957  	want := []*GistFork{{
   958  		URL:       String("https://api.github.com/gists/1"),
   959  		ID:        String("1"),
   960  		User:      &User{ID: Int64(1)},
   961  		CreatedAt: &Timestamp{time.Date(2010, time.January, 1, 00, 00, 00, 0, time.UTC)},
   962  		UpdatedAt: &Timestamp{time.Date(2013, time.January, 1, 00, 00, 00, 0, time.UTC)}}}
   963  
   964  	if !cmp.Equal(gistForks, want) {
   965  		t.Errorf("Gists.ListForks returned %+v, want %+v", gistForks, want)
   966  	}
   967  
   968  	const methodName = "ListForks"
   969  	testBadOptions(t, methodName, func() (err error) {
   970  		_, _, err = client.Gists.ListForks(ctx, "\n", nil)
   971  		return err
   972  	})
   973  
   974  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   975  		got, resp, err := client.Gists.ListForks(ctx, "1", nil)
   976  		if got != nil {
   977  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   978  		}
   979  		return resp, err
   980  	})
   981  }
   982  
   983  func TestGistsService_ListForks_withOptions(t *testing.T) {
   984  	client, mux, _, teardown := setup()
   985  	defer teardown()
   986  
   987  	mux.HandleFunc("/gists/1/forks", func(w http.ResponseWriter, r *http.Request) {
   988  		testMethod(t, r, "GET")
   989  		testFormValues(t, r, values{
   990  			"page": "2",
   991  		})
   992  		fmt.Fprint(w, `[]`)
   993  	})
   994  
   995  	ctx := context.Background()
   996  	gistForks, _, err := client.Gists.ListForks(ctx, "1", &ListOptions{Page: 2})
   997  	if err != nil {
   998  		t.Errorf("Gists.ListForks returned error: %v", err)
   999  	}
  1000  
  1001  	want := []*GistFork{}
  1002  	if !cmp.Equal(gistForks, want) {
  1003  		t.Errorf("Gists.ListForks returned %+v, want %+v", gistForks, want)
  1004  	}
  1005  
  1006  	const methodName = "ListForks"
  1007  	testBadOptions(t, methodName, func() (err error) {
  1008  		_, _, err = client.Gists.ListForks(ctx, "\n", &ListOptions{Page: 2})
  1009  		return err
  1010  	})
  1011  
  1012  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
  1013  		got, resp, err := client.Gists.ListForks(ctx, "1", &ListOptions{Page: 2})
  1014  		if got != nil {
  1015  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
  1016  		}
  1017  		return resp, err
  1018  	})
  1019  }
  1020  
  1021  func TestGistFile_Marshal(t *testing.T) {
  1022  	testJSONMarshal(t, &GistFile{}, "{}")
  1023  
  1024  	u := &GistFile{
  1025  		Size:     Int(1),
  1026  		Filename: String("fn"),
  1027  		Language: String("lan"),
  1028  		Type:     String("type"),
  1029  		RawURL:   String("rurl"),
  1030  		Content:  String("con"),
  1031  	}
  1032  
  1033  	want := `{
  1034  		"size": 1,
  1035  		"filename": "fn",
  1036  		"language": "lan",
  1037  		"type": "type",
  1038  		"raw_url": "rurl",
  1039  		"content": "con"
  1040  	}`
  1041  
  1042  	testJSONMarshal(t, u, want)
  1043  }
  1044  

View as plain text