github.com/google/go-github/v33@v33.0.0/github/gists_test.go (about)

     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  	"reflect"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  func TestGist_marshall(t *testing.T) {
    19  	testJSONMarshal(t, &Gist{}, "{}")
    20  
    21  	createdAt := time.Date(2010, time.February, 10, 10, 10, 0, 0, time.UTC)
    22  	updatedAt := time.Date(2010, time.February, 10, 10, 10, 0, 0, time.UTC)
    23  
    24  	u := &Gist{
    25  		ID:          String("i"),
    26  		Description: String("description"),
    27  		Public:      Bool(true),
    28  		Owner: &User{
    29  			Login:       String("ll"),
    30  			ID:          Int64(123),
    31  			AvatarURL:   String("a"),
    32  			GravatarID:  String("g"),
    33  			Name:        String("n"),
    34  			Company:     String("c"),
    35  			Blog:        String("b"),
    36  			Location:    String("l"),
    37  			Email:       String("e"),
    38  			Hireable:    Bool(true),
    39  			PublicRepos: Int(1),
    40  			Followers:   Int(1),
    41  			Following:   Int(1),
    42  			CreatedAt:   &Timestamp{referenceTime},
    43  			URL:         String("u"),
    44  		},
    45  		Files: map[GistFilename]GistFile{
    46  			"gistfile.py": {
    47  				Size:     Int(167),
    48  				Filename: String("gistfile.py"),
    49  				Language: String("Python"),
    50  				Type:     String("application/x-python"),
    51  				RawURL:   String("raw-url"),
    52  				Content:  String("c"),
    53  			},
    54  		},
    55  		Comments:   Int(1),
    56  		HTMLURL:    String("html-url"),
    57  		GitPullURL: String("gitpull-url"),
    58  		GitPushURL: String("gitpush-url"),
    59  		CreatedAt:  &createdAt,
    60  		UpdatedAt:  &updatedAt,
    61  		NodeID:     String("node"),
    62  	}
    63  
    64  	want := `{
    65  		"id": "i",
    66  		"description": "description",
    67  		"public": true,
    68  		"owner": {
    69  			"login": "ll",
    70  			"id": 123,
    71  			"avatar_url": "a",
    72  			"gravatar_id": "g",
    73  			"name": "n",
    74  			"company": "c",
    75  			"blog": "b",
    76  			"location": "l",
    77  			"email": "e",
    78  			"hireable": true,
    79  			"public_repos": 1,
    80  			"followers": 1,
    81  			"following": 1,
    82  			"created_at": ` + referenceTimeStr + `,
    83  			"url": "u"
    84  		},
    85  		"files": {
    86  			"gistfile.py": {
    87  				"size": 167,
    88  				"filename": "gistfile.py",
    89  				"language": "Python",
    90  				"type": "application/x-python",
    91  				"raw_url": "raw-url",
    92  				"content": "c"
    93  			}
    94  		},
    95  		"comments": 1,
    96  		"html_url": "html-url",
    97  		"git_pull_url": "gitpull-url",
    98  		"git_push_url": "gitpush-url",
    99  		"created_at": "2010-02-10T10:10:00Z",
   100  		"updated_at": "2010-02-10T10:10:00Z",
   101  		"node_id": "node"
   102  	}`
   103  
   104  	testJSONMarshal(t, u, want)
   105  }
   106  
   107  func TestGistCommit_marshall(t *testing.T) {
   108  	testJSONMarshal(t, &GistCommit{}, "{}")
   109  
   110  	u := &GistCommit{
   111  		URL:     String("u"),
   112  		Version: String("v"),
   113  		User: &User{
   114  			Login:       String("ll"),
   115  			ID:          Int64(123),
   116  			AvatarURL:   String("a"),
   117  			GravatarID:  String("g"),
   118  			Name:        String("n"),
   119  			Company:     String("c"),
   120  			Blog:        String("b"),
   121  			Location:    String("l"),
   122  			Email:       String("e"),
   123  			Hireable:    Bool(true),
   124  			PublicRepos: Int(1),
   125  			Followers:   Int(1),
   126  			Following:   Int(1),
   127  			CreatedAt:   &Timestamp{referenceTime},
   128  			URL:         String("u"),
   129  		},
   130  		ChangeStatus: &CommitStats{
   131  			Additions: Int(1),
   132  			Deletions: Int(1),
   133  			Total:     Int(2),
   134  		},
   135  		CommittedAt: &Timestamp{referenceTime},
   136  		NodeID:      String("node"),
   137  	}
   138  
   139  	want := `{
   140  		"url": "u",
   141  		"version": "v",
   142  		"user": {
   143  			"login": "ll",
   144  			"id": 123,
   145  			"avatar_url": "a",
   146  			"gravatar_id": "g",
   147  			"name": "n",
   148  			"company": "c",
   149  			"blog": "b",
   150  			"location": "l",
   151  			"email": "e",
   152  			"hireable": true,
   153  			"public_repos": 1,
   154  			"followers": 1,
   155  			"following": 1,
   156  			"created_at": ` + referenceTimeStr + `,
   157  			"url": "u"
   158  		},
   159  		"change_status": {
   160  			"additions": 1,
   161  			"deletions": 1,
   162  			"total": 2
   163  		},
   164  		"committed_at": ` + referenceTimeStr + `,
   165  		"node_id": "node"
   166  	}`
   167  
   168  	testJSONMarshal(t, u, want)
   169  }
   170  
   171  func TestGistFork_marshall(t *testing.T) {
   172  	testJSONMarshal(t, &GistFork{}, "{}")
   173  
   174  	u := &GistFork{
   175  		URL: String("u"),
   176  		User: &User{
   177  			Login:       String("ll"),
   178  			ID:          Int64(123),
   179  			AvatarURL:   String("a"),
   180  			GravatarID:  String("g"),
   181  			Name:        String("n"),
   182  			Company:     String("c"),
   183  			Blog:        String("b"),
   184  			Location:    String("l"),
   185  			Email:       String("e"),
   186  			Hireable:    Bool(true),
   187  			PublicRepos: Int(1),
   188  			Followers:   Int(1),
   189  			Following:   Int(1),
   190  			CreatedAt:   &Timestamp{referenceTime},
   191  			URL:         String("u"),
   192  		},
   193  		ID:        String("id"),
   194  		CreatedAt: &Timestamp{referenceTime},
   195  		UpdatedAt: &Timestamp{referenceTime},
   196  		NodeID:    String("node"),
   197  	}
   198  
   199  	want := `{
   200  		"url": "u",
   201  		"user": {
   202  			"login": "ll",
   203  			"id": 123,
   204  			"avatar_url": "a",
   205  			"gravatar_id": "g",
   206  			"name": "n",
   207  			"company": "c",
   208  			"blog": "b",
   209  			"location": "l",
   210  			"email": "e",
   211  			"hireable": true,
   212  			"public_repos": 1,
   213  			"followers": 1,
   214  			"following": 1,
   215  			"created_at": ` + referenceTimeStr + `,
   216  			"url": "u"
   217  		},
   218  		"id": "id",
   219  		"created_at": ` + referenceTimeStr + `,
   220  		"updated_at": ` + referenceTimeStr + `,
   221  		"node_id": "node"
   222  	}`
   223  
   224  	testJSONMarshal(t, u, want)
   225  }
   226  
   227  func TestGistsService_List_specifiedUser(t *testing.T) {
   228  	client, mux, _, teardown := setup()
   229  	defer teardown()
   230  
   231  	since := "2013-01-01T00:00:00Z"
   232  
   233  	mux.HandleFunc("/users/u/gists", func(w http.ResponseWriter, r *http.Request) {
   234  		testMethod(t, r, "GET")
   235  		testFormValues(t, r, values{
   236  			"since": since,
   237  		})
   238  		fmt.Fprint(w, `[{"id": "1"}]`)
   239  	})
   240  
   241  	opt := &GistListOptions{Since: time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC)}
   242  	gists, _, err := client.Gists.List(context.Background(), "u", opt)
   243  	if err != nil {
   244  		t.Errorf("Gists.List returned error: %v", err)
   245  	}
   246  
   247  	want := []*Gist{{ID: String("1")}}
   248  	if !reflect.DeepEqual(gists, want) {
   249  		t.Errorf("Gists.List returned %+v, want %+v", gists, want)
   250  	}
   251  }
   252  
   253  func TestGistsService_List_authenticatedUser(t *testing.T) {
   254  	client, mux, _, teardown := setup()
   255  	defer teardown()
   256  
   257  	mux.HandleFunc("/gists", func(w http.ResponseWriter, r *http.Request) {
   258  		testMethod(t, r, "GET")
   259  		fmt.Fprint(w, `[{"id": "1"}]`)
   260  	})
   261  
   262  	gists, _, err := client.Gists.List(context.Background(), "", nil)
   263  	if err != nil {
   264  		t.Errorf("Gists.List returned error: %v", err)
   265  	}
   266  
   267  	want := []*Gist{{ID: String("1")}}
   268  	if !reflect.DeepEqual(gists, want) {
   269  		t.Errorf("Gists.List returned %+v, want %+v", gists, want)
   270  	}
   271  }
   272  
   273  func TestGistsService_List_invalidUser(t *testing.T) {
   274  	client, _, _, teardown := setup()
   275  	defer teardown()
   276  
   277  	_, _, err := client.Gists.List(context.Background(), "%", nil)
   278  	testURLParseError(t, err)
   279  }
   280  
   281  func TestGistsService_ListAll(t *testing.T) {
   282  	client, mux, _, teardown := setup()
   283  	defer teardown()
   284  
   285  	since := "2013-01-01T00:00:00Z"
   286  
   287  	mux.HandleFunc("/gists/public", func(w http.ResponseWriter, r *http.Request) {
   288  		testMethod(t, r, "GET")
   289  		testFormValues(t, r, values{
   290  			"since": since,
   291  		})
   292  		fmt.Fprint(w, `[{"id": "1"}]`)
   293  	})
   294  
   295  	opt := &GistListOptions{Since: time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC)}
   296  	gists, _, err := client.Gists.ListAll(context.Background(), opt)
   297  	if err != nil {
   298  		t.Errorf("Gists.ListAll returned error: %v", err)
   299  	}
   300  
   301  	want := []*Gist{{ID: String("1")}}
   302  	if !reflect.DeepEqual(gists, want) {
   303  		t.Errorf("Gists.ListAll returned %+v, want %+v", gists, want)
   304  	}
   305  }
   306  
   307  func TestGistsService_ListStarred(t *testing.T) {
   308  	client, mux, _, teardown := setup()
   309  	defer teardown()
   310  
   311  	since := "2013-01-01T00:00:00Z"
   312  
   313  	mux.HandleFunc("/gists/starred", func(w http.ResponseWriter, r *http.Request) {
   314  		testMethod(t, r, "GET")
   315  		testFormValues(t, r, values{
   316  			"since": since,
   317  		})
   318  		fmt.Fprint(w, `[{"id": "1"}]`)
   319  	})
   320  
   321  	opt := &GistListOptions{Since: time.Date(2013, time.January, 1, 0, 0, 0, 0, time.UTC)}
   322  	gists, _, err := client.Gists.ListStarred(context.Background(), opt)
   323  	if err != nil {
   324  		t.Errorf("Gists.ListStarred returned error: %v", err)
   325  	}
   326  
   327  	want := []*Gist{{ID: String("1")}}
   328  	if !reflect.DeepEqual(gists, want) {
   329  		t.Errorf("Gists.ListStarred returned %+v, want %+v", gists, want)
   330  	}
   331  }
   332  
   333  func TestGistsService_Get(t *testing.T) {
   334  	client, mux, _, teardown := setup()
   335  	defer teardown()
   336  
   337  	mux.HandleFunc("/gists/1", func(w http.ResponseWriter, r *http.Request) {
   338  		testMethod(t, r, "GET")
   339  		fmt.Fprint(w, `{"id": "1"}`)
   340  	})
   341  
   342  	gist, _, err := client.Gists.Get(context.Background(), "1")
   343  	if err != nil {
   344  		t.Errorf("Gists.Get returned error: %v", err)
   345  	}
   346  
   347  	want := &Gist{ID: String("1")}
   348  	if !reflect.DeepEqual(gist, want) {
   349  		t.Errorf("Gists.Get returned %+v, want %+v", gist, want)
   350  	}
   351  }
   352  
   353  func TestGistsService_Get_invalidID(t *testing.T) {
   354  	client, _, _, teardown := setup()
   355  	defer teardown()
   356  
   357  	_, _, err := client.Gists.Get(context.Background(), "%")
   358  	testURLParseError(t, err)
   359  }
   360  
   361  func TestGistsService_GetRevision(t *testing.T) {
   362  	client, mux, _, teardown := setup()
   363  	defer teardown()
   364  
   365  	mux.HandleFunc("/gists/1/s", func(w http.ResponseWriter, r *http.Request) {
   366  		testMethod(t, r, "GET")
   367  		fmt.Fprint(w, `{"id": "1"}`)
   368  	})
   369  
   370  	gist, _, err := client.Gists.GetRevision(context.Background(), "1", "s")
   371  	if err != nil {
   372  		t.Errorf("Gists.Get returned error: %v", err)
   373  	}
   374  
   375  	want := &Gist{ID: String("1")}
   376  	if !reflect.DeepEqual(gist, want) {
   377  		t.Errorf("Gists.Get returned %+v, want %+v", gist, want)
   378  	}
   379  }
   380  
   381  func TestGistsService_GetRevision_invalidID(t *testing.T) {
   382  	client, _, _, teardown := setup()
   383  	defer teardown()
   384  
   385  	_, _, err := client.Gists.GetRevision(context.Background(), "%", "%")
   386  	testURLParseError(t, err)
   387  }
   388  
   389  func TestGistsService_Create(t *testing.T) {
   390  	client, mux, _, teardown := setup()
   391  	defer teardown()
   392  
   393  	input := &Gist{
   394  		Description: String("Gist description"),
   395  		Public:      Bool(false),
   396  		Files: map[GistFilename]GistFile{
   397  			"test.txt": {Content: String("Gist file content")},
   398  		},
   399  	}
   400  
   401  	mux.HandleFunc("/gists", func(w http.ResponseWriter, r *http.Request) {
   402  		v := new(Gist)
   403  		json.NewDecoder(r.Body).Decode(v)
   404  
   405  		testMethod(t, r, "POST")
   406  		if !reflect.DeepEqual(v, input) {
   407  			t.Errorf("Request body = %+v, want %+v", v, input)
   408  		}
   409  
   410  		fmt.Fprint(w,
   411  			`
   412  			{
   413  				"id": "1",
   414  				"description": "Gist description",
   415  				"public": false,
   416  				"files": {
   417  					"test.txt": {
   418  						"filename": "test.txt"
   419  					}
   420  				}
   421  			}`)
   422  	})
   423  
   424  	gist, _, err := client.Gists.Create(context.Background(), input)
   425  	if err != nil {
   426  		t.Errorf("Gists.Create returned error: %v", err)
   427  	}
   428  
   429  	want := &Gist{
   430  		ID:          String("1"),
   431  		Description: String("Gist description"),
   432  		Public:      Bool(false),
   433  		Files: map[GistFilename]GistFile{
   434  			"test.txt": {Filename: String("test.txt")},
   435  		},
   436  	}
   437  	if !reflect.DeepEqual(gist, want) {
   438  		t.Errorf("Gists.Create returned %+v, want %+v", gist, want)
   439  	}
   440  }
   441  
   442  func TestGistsService_Edit(t *testing.T) {
   443  	client, mux, _, teardown := setup()
   444  	defer teardown()
   445  
   446  	input := &Gist{
   447  		Description: String("New description"),
   448  		Files: map[GistFilename]GistFile{
   449  			"new.txt": {Content: String("new file content")},
   450  		},
   451  	}
   452  
   453  	mux.HandleFunc("/gists/1", func(w http.ResponseWriter, r *http.Request) {
   454  		v := new(Gist)
   455  		json.NewDecoder(r.Body).Decode(v)
   456  
   457  		testMethod(t, r, "PATCH")
   458  		if !reflect.DeepEqual(v, input) {
   459  			t.Errorf("Request body = %+v, want %+v", v, input)
   460  		}
   461  
   462  		fmt.Fprint(w,
   463  			`
   464  			{
   465  				"id": "1",
   466  				"description": "new description",
   467  				"public": false,
   468  				"files": {
   469  					"test.txt": {
   470  						"filename": "test.txt"
   471  					},
   472  					"new.txt": {
   473  						"filename": "new.txt"
   474  					}
   475  				}
   476  			}`)
   477  	})
   478  
   479  	gist, _, err := client.Gists.Edit(context.Background(), "1", input)
   480  	if err != nil {
   481  		t.Errorf("Gists.Edit returned error: %v", err)
   482  	}
   483  
   484  	want := &Gist{
   485  		ID:          String("1"),
   486  		Description: String("new description"),
   487  		Public:      Bool(false),
   488  		Files: map[GistFilename]GistFile{
   489  			"test.txt": {Filename: String("test.txt")},
   490  			"new.txt":  {Filename: String("new.txt")},
   491  		},
   492  	}
   493  	if !reflect.DeepEqual(gist, want) {
   494  		t.Errorf("Gists.Edit returned %+v, want %+v", gist, want)
   495  	}
   496  }
   497  
   498  func TestGistsService_Edit_invalidID(t *testing.T) {
   499  	client, _, _, teardown := setup()
   500  	defer teardown()
   501  
   502  	_, _, err := client.Gists.Edit(context.Background(), "%", nil)
   503  	testURLParseError(t, err)
   504  }
   505  
   506  func TestGistsService_ListCommits(t *testing.T) {
   507  	client, mux, _, teardown := setup()
   508  	defer teardown()
   509  
   510  	mux.HandleFunc("/gists/1/commits", func(w http.ResponseWriter, r *http.Request) {
   511  		testMethod(t, r, "GET")
   512  		testFormValues(t, r, nil)
   513  		fmt.Fprint(w, `
   514  		  [
   515  		    {
   516  		      "url": "https://api.github.com/gists/1/1",
   517  		      "version": "1",
   518  		      "user": {
   519  		        "id": 1
   520  		      },
   521  		      "change_status": {
   522  		        "deletions": 0,
   523  		        "additions": 180,
   524  		        "total": 180
   525  		      },
   526  		      "committed_at": "2010-01-01T00:00:00Z"
   527  		    }
   528  		  ]
   529  		`)
   530  	})
   531  
   532  	gistCommits, _, err := client.Gists.ListCommits(context.Background(), "1", nil)
   533  	if err != nil {
   534  		t.Errorf("Gists.ListCommits returned error: %v", err)
   535  	}
   536  
   537  	want := []*GistCommit{{
   538  		URL:         String("https://api.github.com/gists/1/1"),
   539  		Version:     String("1"),
   540  		User:        &User{ID: Int64(1)},
   541  		CommittedAt: &Timestamp{time.Date(2010, time.January, 1, 00, 00, 00, 0, time.UTC)},
   542  		ChangeStatus: &CommitStats{
   543  			Additions: Int(180),
   544  			Deletions: Int(0),
   545  			Total:     Int(180),
   546  		}}}
   547  
   548  	if !reflect.DeepEqual(gistCommits, want) {
   549  		t.Errorf("Gists.ListCommits returned %+v, want %+v", gistCommits, want)
   550  	}
   551  }
   552  
   553  func TestGistsService_ListCommits_withOptions(t *testing.T) {
   554  	client, mux, _, teardown := setup()
   555  	defer teardown()
   556  
   557  	mux.HandleFunc("/gists/1/commits", func(w http.ResponseWriter, r *http.Request) {
   558  		testMethod(t, r, "GET")
   559  		testFormValues(t, r, values{
   560  			"page": "2",
   561  		})
   562  		fmt.Fprint(w, `[]`)
   563  	})
   564  
   565  	_, _, err := client.Gists.ListCommits(context.Background(), "1", &ListOptions{Page: 2})
   566  	if err != nil {
   567  		t.Errorf("Gists.ListCommits returned error: %v", err)
   568  	}
   569  }
   570  
   571  func TestGistsService_ListCommits_invalidID(t *testing.T) {
   572  	client, _, _, teardown := setup()
   573  	defer teardown()
   574  
   575  	_, _, err := client.Gists.ListCommits(context.Background(), "%", nil)
   576  	testURLParseError(t, err)
   577  }
   578  
   579  func TestGistsService_Delete(t *testing.T) {
   580  	client, mux, _, teardown := setup()
   581  	defer teardown()
   582  
   583  	mux.HandleFunc("/gists/1", func(w http.ResponseWriter, r *http.Request) {
   584  		testMethod(t, r, "DELETE")
   585  	})
   586  
   587  	_, err := client.Gists.Delete(context.Background(), "1")
   588  	if err != nil {
   589  		t.Errorf("Gists.Delete returned error: %v", err)
   590  	}
   591  }
   592  
   593  func TestGistsService_Delete_invalidID(t *testing.T) {
   594  	client, _, _, teardown := setup()
   595  	defer teardown()
   596  
   597  	_, err := client.Gists.Delete(context.Background(), "%")
   598  	testURLParseError(t, err)
   599  }
   600  
   601  func TestGistsService_Star(t *testing.T) {
   602  	client, mux, _, teardown := setup()
   603  	defer teardown()
   604  
   605  	mux.HandleFunc("/gists/1/star", func(w http.ResponseWriter, r *http.Request) {
   606  		testMethod(t, r, "PUT")
   607  	})
   608  
   609  	_, err := client.Gists.Star(context.Background(), "1")
   610  	if err != nil {
   611  		t.Errorf("Gists.Star returned error: %v", err)
   612  	}
   613  }
   614  
   615  func TestGistsService_Star_invalidID(t *testing.T) {
   616  	client, _, _, teardown := setup()
   617  	defer teardown()
   618  
   619  	_, err := client.Gists.Star(context.Background(), "%")
   620  	testURLParseError(t, err)
   621  }
   622  
   623  func TestGistsService_Unstar(t *testing.T) {
   624  	client, mux, _, teardown := setup()
   625  	defer teardown()
   626  
   627  	mux.HandleFunc("/gists/1/star", func(w http.ResponseWriter, r *http.Request) {
   628  		testMethod(t, r, "DELETE")
   629  	})
   630  
   631  	_, err := client.Gists.Unstar(context.Background(), "1")
   632  	if err != nil {
   633  		t.Errorf("Gists.Unstar returned error: %v", err)
   634  	}
   635  }
   636  
   637  func TestGistsService_Unstar_invalidID(t *testing.T) {
   638  	client, _, _, teardown := setup()
   639  	defer teardown()
   640  
   641  	_, err := client.Gists.Unstar(context.Background(), "%")
   642  	testURLParseError(t, err)
   643  }
   644  
   645  func TestGistsService_IsStarred_hasStar(t *testing.T) {
   646  	client, mux, _, teardown := setup()
   647  	defer teardown()
   648  
   649  	mux.HandleFunc("/gists/1/star", func(w http.ResponseWriter, r *http.Request) {
   650  		testMethod(t, r, "GET")
   651  		w.WriteHeader(http.StatusNoContent)
   652  	})
   653  
   654  	star, _, err := client.Gists.IsStarred(context.Background(), "1")
   655  	if err != nil {
   656  		t.Errorf("Gists.Starred returned error: %v", err)
   657  	}
   658  	if want := true; star != want {
   659  		t.Errorf("Gists.Starred returned %+v, want %+v", star, want)
   660  	}
   661  }
   662  
   663  func TestGistsService_IsStarred_noStar(t *testing.T) {
   664  	client, mux, _, teardown := setup()
   665  	defer teardown()
   666  
   667  	mux.HandleFunc("/gists/1/star", func(w http.ResponseWriter, r *http.Request) {
   668  		testMethod(t, r, "GET")
   669  		w.WriteHeader(http.StatusNotFound)
   670  	})
   671  
   672  	star, _, err := client.Gists.IsStarred(context.Background(), "1")
   673  	if err != nil {
   674  		t.Errorf("Gists.Starred returned error: %v", err)
   675  	}
   676  	if want := false; star != want {
   677  		t.Errorf("Gists.Starred returned %+v, want %+v", star, want)
   678  	}
   679  }
   680  
   681  func TestGistsService_IsStarred_invalidID(t *testing.T) {
   682  	client, _, _, teardown := setup()
   683  	defer teardown()
   684  
   685  	_, _, err := client.Gists.IsStarred(context.Background(), "%")
   686  	testURLParseError(t, err)
   687  }
   688  
   689  func TestGistsService_Fork(t *testing.T) {
   690  	client, mux, _, teardown := setup()
   691  	defer teardown()
   692  
   693  	mux.HandleFunc("/gists/1/forks", func(w http.ResponseWriter, r *http.Request) {
   694  		testMethod(t, r, "POST")
   695  		fmt.Fprint(w, `{"id": "2"}`)
   696  	})
   697  
   698  	gist, _, err := client.Gists.Fork(context.Background(), "1")
   699  	if err != nil {
   700  		t.Errorf("Gists.Fork returned error: %v", err)
   701  	}
   702  
   703  	want := &Gist{ID: String("2")}
   704  	if !reflect.DeepEqual(gist, want) {
   705  		t.Errorf("Gists.Fork returned %+v, want %+v", gist, want)
   706  	}
   707  }
   708  
   709  func TestGistsService_ListForks(t *testing.T) {
   710  	client, mux, _, teardown := setup()
   711  	defer teardown()
   712  
   713  	mux.HandleFunc("/gists/1/forks", func(w http.ResponseWriter, r *http.Request) {
   714  		testMethod(t, r, "GET")
   715  		testFormValues(t, r, nil)
   716  		fmt.Fprint(w, `
   717  		  [
   718  		    {"url": "https://api.github.com/gists/1",
   719  		     "user": {"id": 1},
   720  		     "id": "1",
   721  		     "created_at": "2010-01-01T00:00:00Z",
   722  		     "updated_at": "2013-01-01T00:00:00Z"
   723  		    }
   724  		  ]
   725  		`)
   726  	})
   727  
   728  	gistForks, _, err := client.Gists.ListForks(context.Background(), "1", nil)
   729  	if err != nil {
   730  		t.Errorf("Gists.ListForks returned error: %v", err)
   731  	}
   732  
   733  	want := []*GistFork{{
   734  		URL:       String("https://api.github.com/gists/1"),
   735  		ID:        String("1"),
   736  		User:      &User{ID: Int64(1)},
   737  		CreatedAt: &Timestamp{time.Date(2010, time.January, 1, 00, 00, 00, 0, time.UTC)},
   738  		UpdatedAt: &Timestamp{time.Date(2013, time.January, 1, 00, 00, 00, 0, time.UTC)}}}
   739  
   740  	if !reflect.DeepEqual(gistForks, want) {
   741  		t.Errorf("Gists.ListForks returned %+v, want %+v", gistForks, want)
   742  	}
   743  
   744  }
   745  
   746  func TestGistsService_ListForks_withOptions(t *testing.T) {
   747  	client, mux, _, teardown := setup()
   748  	defer teardown()
   749  
   750  	mux.HandleFunc("/gists/1/forks", func(w http.ResponseWriter, r *http.Request) {
   751  		testMethod(t, r, "GET")
   752  		testFormValues(t, r, values{
   753  			"page": "2",
   754  		})
   755  		fmt.Fprint(w, `[]`)
   756  	})
   757  
   758  	gistForks, _, err := client.Gists.ListForks(context.Background(), "1", &ListOptions{Page: 2})
   759  	if err != nil {
   760  		t.Errorf("Gists.ListForks returned error: %v", err)
   761  	}
   762  
   763  	want := []*GistFork{}
   764  	if !reflect.DeepEqual(gistForks, want) {
   765  		t.Errorf("Gists.ListForks returned %+v, want %+v", gistForks, want)
   766  	}
   767  
   768  	// Test addOptions failure
   769  	_, _, err = client.Gists.ListForks(context.Background(), "%", &ListOptions{})
   770  	if err == nil {
   771  		t.Error("Gists.ListForks returned err = nil")
   772  	}
   773  
   774  	// Test client.NewRequest failure
   775  	got, resp, err := client.Gists.ListForks(context.Background(), "%", nil)
   776  	if got != nil {
   777  		t.Errorf("Gists.ListForks = %#v, want nil", got)
   778  	}
   779  	if resp != nil {
   780  		t.Errorf("Gists.ListForks resp = %#v, want nil", resp)
   781  	}
   782  	if err == nil {
   783  		t.Error("Gists.ListForks err = nil, want error")
   784  	}
   785  
   786  	// Test client.Do failure
   787  	client.rateLimits[0].Reset.Time = time.Now().Add(10 * time.Minute)
   788  	got, resp, err = client.Gists.ListForks(context.Background(), "1", &ListOptions{Page: 2})
   789  	if got != nil {
   790  		t.Errorf("Gists.ListForks returned = %#v, want nil", got)
   791  	}
   792  	if want := http.StatusForbidden; resp == nil || resp.Response.StatusCode != want {
   793  		t.Errorf("Gists.ListForks returned resp = %#v, want StatusCode=%v", resp.Response, want)
   794  	}
   795  	if err == nil {
   796  		t.Error("rGists.ListForks returned err = nil, want error")
   797  	}
   798  }