github.com/google/go-github/v33@v33.0.0/github/pulls_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  	"io"
    13  	"net/http"
    14  	"reflect"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  func TestPullRequestsService_List(t *testing.T) {
    20  	client, mux, _, teardown := setup()
    21  	defer teardown()
    22  
    23  	mux.HandleFunc("/repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) {
    24  		testMethod(t, r, "GET")
    25  		testFormValues(t, r, values{
    26  			"state":     "closed",
    27  			"head":      "h",
    28  			"base":      "b",
    29  			"sort":      "created",
    30  			"direction": "desc",
    31  			"page":      "2",
    32  		})
    33  		fmt.Fprint(w, `[{"number":1}]`)
    34  	})
    35  
    36  	opts := &PullRequestListOptions{"closed", "h", "b", "created", "desc", ListOptions{Page: 2}}
    37  	pulls, _, err := client.PullRequests.List(context.Background(), "o", "r", opts)
    38  	if err != nil {
    39  		t.Errorf("PullRequests.List returned error: %v", err)
    40  	}
    41  
    42  	want := []*PullRequest{{Number: Int(1)}}
    43  	if !reflect.DeepEqual(pulls, want) {
    44  		t.Errorf("PullRequests.List returned %+v, want %+v", pulls, want)
    45  	}
    46  }
    47  
    48  func TestPullRequestsService_ListPullRequestsWithCommit(t *testing.T) {
    49  	client, mux, _, teardown := setup()
    50  	defer teardown()
    51  
    52  	mux.HandleFunc("/repos/o/r/commits/sha/pulls", func(w http.ResponseWriter, r *http.Request) {
    53  		testMethod(t, r, "GET")
    54  		testHeader(t, r, "Accept", mediaTypeListPullsOrBranchesForCommitPreview)
    55  		testFormValues(t, r, values{
    56  			"state":     "closed",
    57  			"head":      "h",
    58  			"base":      "b",
    59  			"sort":      "created",
    60  			"direction": "desc",
    61  			"page":      "2",
    62  		})
    63  		fmt.Fprint(w, `[{"number":1}]`)
    64  	})
    65  
    66  	opts := &PullRequestListOptions{"closed", "h", "b", "created", "desc", ListOptions{Page: 2}}
    67  	pulls, _, err := client.PullRequests.ListPullRequestsWithCommit(context.Background(), "o", "r", "sha", opts)
    68  	if err != nil {
    69  		t.Errorf("PullRequests.ListPullRequestsWithCommit returned error: %v", err)
    70  	}
    71  
    72  	want := []*PullRequest{{Number: Int(1)}}
    73  	if !reflect.DeepEqual(pulls, want) {
    74  		t.Errorf("PullRequests.ListPullRequestsWithCommit returned %+v, want %+v", pulls, want)
    75  	}
    76  }
    77  
    78  func TestPullRequestsService_List_invalidOwner(t *testing.T) {
    79  	client, _, _, teardown := setup()
    80  	defer teardown()
    81  
    82  	_, _, err := client.PullRequests.List(context.Background(), "%", "r", nil)
    83  	testURLParseError(t, err)
    84  }
    85  
    86  func TestPullRequestsService_Get(t *testing.T) {
    87  	client, mux, _, teardown := setup()
    88  	defer teardown()
    89  
    90  	mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
    91  		testMethod(t, r, "GET")
    92  		fmt.Fprint(w, `{"number":1}`)
    93  	})
    94  
    95  	pull, _, err := client.PullRequests.Get(context.Background(), "o", "r", 1)
    96  	if err != nil {
    97  		t.Errorf("PullRequests.Get returned error: %v", err)
    98  	}
    99  
   100  	want := &PullRequest{Number: Int(1)}
   101  	if !reflect.DeepEqual(pull, want) {
   102  		t.Errorf("PullRequests.Get returned %+v, want %+v", pull, want)
   103  	}
   104  }
   105  
   106  func TestPullRequestsService_GetRaw_diff(t *testing.T) {
   107  	client, mux, _, teardown := setup()
   108  	defer teardown()
   109  
   110  	const rawStr = "@@diff content"
   111  
   112  	mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
   113  		testMethod(t, r, "GET")
   114  		testHeader(t, r, "Accept", mediaTypeV3Diff)
   115  		fmt.Fprint(w, rawStr)
   116  	})
   117  
   118  	got, _, err := client.PullRequests.GetRaw(context.Background(), "o", "r", 1, RawOptions{Diff})
   119  	if err != nil {
   120  		t.Fatalf("PullRequests.GetRaw returned error: %v", err)
   121  	}
   122  	want := rawStr
   123  	if got != want {
   124  		t.Errorf("PullRequests.GetRaw returned %s want %s", got, want)
   125  	}
   126  }
   127  
   128  func TestPullRequestsService_GetRaw_patch(t *testing.T) {
   129  	client, mux, _, teardown := setup()
   130  	defer teardown()
   131  
   132  	const rawStr = "@@patch content"
   133  
   134  	mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
   135  		testMethod(t, r, "GET")
   136  		testHeader(t, r, "Accept", mediaTypeV3Patch)
   137  		fmt.Fprint(w, rawStr)
   138  	})
   139  
   140  	got, _, err := client.PullRequests.GetRaw(context.Background(), "o", "r", 1, RawOptions{Patch})
   141  	if err != nil {
   142  		t.Fatalf("PullRequests.GetRaw returned error: %v", err)
   143  	}
   144  	want := rawStr
   145  	if got != want {
   146  		t.Errorf("PullRequests.GetRaw returned %s want %s", got, want)
   147  	}
   148  }
   149  
   150  func TestPullRequestsService_GetRaw_invalid(t *testing.T) {
   151  	client, _, _, teardown := setup()
   152  	defer teardown()
   153  
   154  	_, _, err := client.PullRequests.GetRaw(context.Background(), "o", "r", 1, RawOptions{100})
   155  	if err == nil {
   156  		t.Fatal("PullRequests.GetRaw should return error")
   157  	}
   158  	if !strings.Contains(err.Error(), "unsupported raw type") {
   159  		t.Error("PullRequests.GetRaw should return unsupported raw type error")
   160  	}
   161  }
   162  
   163  func TestPullRequestsService_Get_links(t *testing.T) {
   164  	client, mux, _, teardown := setup()
   165  	defer teardown()
   166  
   167  	mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
   168  		testMethod(t, r, "GET")
   169  		fmt.Fprint(w, `{
   170  			"number":1,
   171  			"_links":{
   172  				"self":{"href":"https://api.github.com/repos/octocat/Hello-World/pulls/1347"},
   173  				"html":{"href":"https://github.com/octocat/Hello-World/pull/1347"},
   174  				"issue":{"href":"https://api.github.com/repos/octocat/Hello-World/issues/1347"},
   175  				"comments":{"href":"https://api.github.com/repos/octocat/Hello-World/issues/1347/comments"},
   176  				"review_comments":{"href":"https://api.github.com/repos/octocat/Hello-World/pulls/1347/comments"},
   177  				"review_comment":{"href":"https://api.github.com/repos/octocat/Hello-World/pulls/comments{/number}"},
   178  				"commits":{"href":"https://api.github.com/repos/octocat/Hello-World/pulls/1347/commits"},
   179  				"statuses":{"href":"https://api.github.com/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e"}
   180  				}
   181  			}`)
   182  	})
   183  
   184  	pull, _, err := client.PullRequests.Get(context.Background(), "o", "r", 1)
   185  	if err != nil {
   186  		t.Errorf("PullRequests.Get returned error: %v", err)
   187  	}
   188  
   189  	want := &PullRequest{
   190  		Number: Int(1),
   191  		Links: &PRLinks{
   192  			Self: &PRLink{
   193  				HRef: String("https://api.github.com/repos/octocat/Hello-World/pulls/1347"),
   194  			}, HTML: &PRLink{
   195  				HRef: String("https://github.com/octocat/Hello-World/pull/1347"),
   196  			}, Issue: &PRLink{
   197  				HRef: String("https://api.github.com/repos/octocat/Hello-World/issues/1347"),
   198  			}, Comments: &PRLink{
   199  				HRef: String("https://api.github.com/repos/octocat/Hello-World/issues/1347/comments"),
   200  			}, ReviewComments: &PRLink{
   201  				HRef: String("https://api.github.com/repos/octocat/Hello-World/pulls/1347/comments"),
   202  			}, ReviewComment: &PRLink{
   203  				HRef: String("https://api.github.com/repos/octocat/Hello-World/pulls/comments{/number}"),
   204  			}, Commits: &PRLink{
   205  				HRef: String("https://api.github.com/repos/octocat/Hello-World/pulls/1347/commits"),
   206  			}, Statuses: &PRLink{
   207  				HRef: String("https://api.github.com/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e"),
   208  			},
   209  		},
   210  	}
   211  	if !reflect.DeepEqual(pull, want) {
   212  		t.Errorf("PullRequests.Get returned %+v, want %+v", pull, want)
   213  	}
   214  }
   215  
   216  func TestPullRequestsService_Get_headAndBase(t *testing.T) {
   217  	client, mux, _, teardown := setup()
   218  	defer teardown()
   219  
   220  	mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
   221  		testMethod(t, r, "GET")
   222  		fmt.Fprint(w, `{"number":1,"head":{"ref":"r2","repo":{"id":2}},"base":{"ref":"r1","repo":{"id":1}}}`)
   223  	})
   224  
   225  	pull, _, err := client.PullRequests.Get(context.Background(), "o", "r", 1)
   226  	if err != nil {
   227  		t.Errorf("PullRequests.Get returned error: %v", err)
   228  	}
   229  
   230  	want := &PullRequest{
   231  		Number: Int(1),
   232  		Head: &PullRequestBranch{
   233  			Ref:  String("r2"),
   234  			Repo: &Repository{ID: Int64(2)},
   235  		},
   236  		Base: &PullRequestBranch{
   237  			Ref:  String("r1"),
   238  			Repo: &Repository{ID: Int64(1)},
   239  		},
   240  	}
   241  	if !reflect.DeepEqual(pull, want) {
   242  		t.Errorf("PullRequests.Get returned %+v, want %+v", pull, want)
   243  	}
   244  }
   245  
   246  func TestPullRequestsService_Get_urlFields(t *testing.T) {
   247  	client, mux, _, teardown := setup()
   248  	defer teardown()
   249  
   250  	mux.HandleFunc("/repos/o/r/pulls/1", func(w http.ResponseWriter, r *http.Request) {
   251  		testMethod(t, r, "GET")
   252  		fmt.Fprint(w, `{"number":1,
   253  			"url": "https://api.github.com/repos/octocat/Hello-World/pulls/1347",
   254  			"html_url": "https://github.com/octocat/Hello-World/pull/1347",
   255  			"issue_url": "https://api.github.com/repos/octocat/Hello-World/issues/1347",
   256  			"statuses_url": "https://api.github.com/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e",
   257  			"diff_url": "https://github.com/octocat/Hello-World/pull/1347.diff",
   258  			"patch_url": "https://github.com/octocat/Hello-World/pull/1347.patch",
   259  			"review_comments_url": "https://api.github.com/repos/octocat/Hello-World/pulls/1347/comments",
   260  			"review_comment_url": "https://api.github.com/repos/octocat/Hello-World/pulls/comments{/number}"}`)
   261  	})
   262  
   263  	pull, _, err := client.PullRequests.Get(context.Background(), "o", "r", 1)
   264  	if err != nil {
   265  		t.Errorf("PullRequests.Get returned error: %v", err)
   266  	}
   267  
   268  	want := &PullRequest{
   269  		Number:            Int(1),
   270  		URL:               String("https://api.github.com/repos/octocat/Hello-World/pulls/1347"),
   271  		HTMLURL:           String("https://github.com/octocat/Hello-World/pull/1347"),
   272  		IssueURL:          String("https://api.github.com/repos/octocat/Hello-World/issues/1347"),
   273  		StatusesURL:       String("https://api.github.com/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e"),
   274  		DiffURL:           String("https://github.com/octocat/Hello-World/pull/1347.diff"),
   275  		PatchURL:          String("https://github.com/octocat/Hello-World/pull/1347.patch"),
   276  		ReviewCommentsURL: String("https://api.github.com/repos/octocat/Hello-World/pulls/1347/comments"),
   277  		ReviewCommentURL:  String("https://api.github.com/repos/octocat/Hello-World/pulls/comments{/number}"),
   278  	}
   279  
   280  	if !reflect.DeepEqual(pull, want) {
   281  		t.Errorf("PullRequests.Get returned %+v, want %+v", pull, want)
   282  	}
   283  }
   284  
   285  func TestPullRequestsService_Get_invalidOwner(t *testing.T) {
   286  	client, _, _, teardown := setup()
   287  	defer teardown()
   288  
   289  	_, _, err := client.PullRequests.Get(context.Background(), "%", "r", 1)
   290  	testURLParseError(t, err)
   291  }
   292  
   293  func TestPullRequestsService_Create(t *testing.T) {
   294  	client, mux, _, teardown := setup()
   295  	defer teardown()
   296  
   297  	input := &NewPullRequest{Title: String("t")}
   298  
   299  	mux.HandleFunc("/repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) {
   300  		v := new(NewPullRequest)
   301  		json.NewDecoder(r.Body).Decode(v)
   302  
   303  		testMethod(t, r, "POST")
   304  		if !reflect.DeepEqual(v, input) {
   305  			t.Errorf("Request body = %+v, want %+v", v, input)
   306  		}
   307  
   308  		fmt.Fprint(w, `{"number":1}`)
   309  	})
   310  
   311  	pull, _, err := client.PullRequests.Create(context.Background(), "o", "r", input)
   312  	if err != nil {
   313  		t.Errorf("PullRequests.Create returned error: %v", err)
   314  	}
   315  
   316  	want := &PullRequest{Number: Int(1)}
   317  	if !reflect.DeepEqual(pull, want) {
   318  		t.Errorf("PullRequests.Create returned %+v, want %+v", pull, want)
   319  	}
   320  }
   321  
   322  func TestPullRequestsService_Create_invalidOwner(t *testing.T) {
   323  	client, _, _, teardown := setup()
   324  	defer teardown()
   325  
   326  	_, _, err := client.PullRequests.Create(context.Background(), "%", "r", nil)
   327  	testURLParseError(t, err)
   328  }
   329  
   330  func TestPullRequestsService_UpdateBranch(t *testing.T) {
   331  	client, mux, _, teardown := setup()
   332  	defer teardown()
   333  
   334  	mux.HandleFunc("/repos/o/r/pulls/1/update-branch", func(w http.ResponseWriter, r *http.Request) {
   335  		testMethod(t, r, "PUT")
   336  		testHeader(t, r, "Accept", mediaTypeUpdatePullRequestBranchPreview)
   337  		fmt.Fprint(w, `
   338  			{
   339  			  "message": "Updating pull request branch.",
   340  			  "url": "https://github.com/repos/o/r/pulls/1"
   341  			}`)
   342  	})
   343  
   344  	opts := &PullRequestBranchUpdateOptions{
   345  		ExpectedHeadSHA: String("s"),
   346  	}
   347  
   348  	pull, _, err := client.PullRequests.UpdateBranch(context.Background(), "o", "r", 1, opts)
   349  	if err != nil {
   350  		t.Errorf("PullRequests.UpdateBranch returned error: %v", err)
   351  	}
   352  
   353  	want := &PullRequestBranchUpdateResponse{
   354  		Message: String("Updating pull request branch."),
   355  		URL:     String("https://github.com/repos/o/r/pulls/1"),
   356  	}
   357  
   358  	if !reflect.DeepEqual(pull, want) {
   359  		t.Errorf("PullRequests.UpdateBranch returned %+v, want %+v", pull, want)
   360  	}
   361  }
   362  
   363  func TestPullRequestsService_Edit(t *testing.T) {
   364  	client, mux, _, teardown := setup()
   365  	defer teardown()
   366  
   367  	tests := []struct {
   368  		input        *PullRequest
   369  		sendResponse string
   370  
   371  		wantUpdate string
   372  		want       *PullRequest
   373  	}{
   374  		{
   375  			input:        &PullRequest{Title: String("t")},
   376  			sendResponse: `{"number":1}`,
   377  			wantUpdate:   `{"title":"t"}`,
   378  			want:         &PullRequest{Number: Int(1)},
   379  		},
   380  		{
   381  			// base update
   382  			input:        &PullRequest{Base: &PullRequestBranch{Ref: String("master")}},
   383  			sendResponse: `{"number":1,"base":{"ref":"master"}}`,
   384  			wantUpdate:   `{"base":"master"}`,
   385  			want: &PullRequest{
   386  				Number: Int(1),
   387  				Base:   &PullRequestBranch{Ref: String("master")},
   388  			},
   389  		},
   390  	}
   391  
   392  	for i, tt := range tests {
   393  		madeRequest := false
   394  		mux.HandleFunc(fmt.Sprintf("/repos/o/r/pulls/%v", i), func(w http.ResponseWriter, r *http.Request) {
   395  			testMethod(t, r, "PATCH")
   396  			testBody(t, r, tt.wantUpdate+"\n")
   397  			io.WriteString(w, tt.sendResponse)
   398  			madeRequest = true
   399  		})
   400  
   401  		pull, _, err := client.PullRequests.Edit(context.Background(), "o", "r", i, tt.input)
   402  		if err != nil {
   403  			t.Errorf("%d: PullRequests.Edit returned error: %v", i, err)
   404  		}
   405  
   406  		if !reflect.DeepEqual(pull, tt.want) {
   407  			t.Errorf("%d: PullRequests.Edit returned %+v, want %+v", i, pull, tt.want)
   408  		}
   409  
   410  		if !madeRequest {
   411  			t.Errorf("%d: PullRequest.Edit did not make the expected request", i)
   412  		}
   413  	}
   414  }
   415  
   416  func TestPullRequestsService_Edit_invalidOwner(t *testing.T) {
   417  	client, _, _, teardown := setup()
   418  	defer teardown()
   419  
   420  	_, _, err := client.PullRequests.Edit(context.Background(), "%", "r", 1, &PullRequest{})
   421  	testURLParseError(t, err)
   422  }
   423  
   424  func TestPullRequestsService_ListCommits(t *testing.T) {
   425  	client, mux, _, teardown := setup()
   426  	defer teardown()
   427  
   428  	mux.HandleFunc("/repos/o/r/pulls/1/commits", func(w http.ResponseWriter, r *http.Request) {
   429  		testMethod(t, r, "GET")
   430  		testFormValues(t, r, values{"page": "2"})
   431  		fmt.Fprint(w, `
   432  			[
   433  			  {
   434  			    "sha": "3",
   435  			    "parents": [
   436  			      {
   437  			        "sha": "2"
   438  			      }
   439  			    ]
   440  			  },
   441  			  {
   442  			    "sha": "2",
   443  			    "parents": [
   444  			      {
   445  			        "sha": "1"
   446  			      }
   447  			    ]
   448  			  }
   449  			]`)
   450  	})
   451  
   452  	opts := &ListOptions{Page: 2}
   453  	commits, _, err := client.PullRequests.ListCommits(context.Background(), "o", "r", 1, opts)
   454  	if err != nil {
   455  		t.Errorf("PullRequests.ListCommits returned error: %v", err)
   456  	}
   457  
   458  	want := []*RepositoryCommit{
   459  		{
   460  			SHA: String("3"),
   461  			Parents: []*Commit{
   462  				{
   463  					SHA: String("2"),
   464  				},
   465  			},
   466  		},
   467  		{
   468  			SHA: String("2"),
   469  			Parents: []*Commit{
   470  				{
   471  					SHA: String("1"),
   472  				},
   473  			},
   474  		},
   475  	}
   476  	if !reflect.DeepEqual(commits, want) {
   477  		t.Errorf("PullRequests.ListCommits returned %+v, want %+v", commits, want)
   478  	}
   479  }
   480  
   481  func TestPullRequestsService_ListFiles(t *testing.T) {
   482  	client, mux, _, teardown := setup()
   483  	defer teardown()
   484  
   485  	mux.HandleFunc("/repos/o/r/pulls/1/files", func(w http.ResponseWriter, r *http.Request) {
   486  		testMethod(t, r, "GET")
   487  		testFormValues(t, r, values{"page": "2"})
   488  		fmt.Fprint(w, `
   489  			[
   490  			  {
   491  			    "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
   492  			    "filename": "file1.txt",
   493  			    "status": "added",
   494  			    "additions": 103,
   495  			    "deletions": 21,
   496  			    "changes": 124,
   497  			    "patch": "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"
   498  			  },
   499  			  {
   500  			    "sha": "f61aebed695e2e4193db5e6dcb09b5b57875f334",
   501  			    "filename": "file2.txt",
   502  			    "status": "modified",
   503  			    "additions": 5,
   504  			    "deletions": 3,
   505  			    "changes": 103,
   506  			    "patch": "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"
   507  			  }
   508  			]`)
   509  	})
   510  
   511  	opts := &ListOptions{Page: 2}
   512  	commitFiles, _, err := client.PullRequests.ListFiles(context.Background(), "o", "r", 1, opts)
   513  	if err != nil {
   514  		t.Errorf("PullRequests.ListFiles returned error: %v", err)
   515  	}
   516  
   517  	want := []*CommitFile{
   518  		{
   519  			SHA:       String("6dcb09b5b57875f334f61aebed695e2e4193db5e"),
   520  			Filename:  String("file1.txt"),
   521  			Additions: Int(103),
   522  			Deletions: Int(21),
   523  			Changes:   Int(124),
   524  			Status:    String("added"),
   525  			Patch:     String("@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"),
   526  		},
   527  		{
   528  			SHA:       String("f61aebed695e2e4193db5e6dcb09b5b57875f334"),
   529  			Filename:  String("file2.txt"),
   530  			Additions: Int(5),
   531  			Deletions: Int(3),
   532  			Changes:   Int(103),
   533  			Status:    String("modified"),
   534  			Patch:     String("@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"),
   535  		},
   536  	}
   537  
   538  	if !reflect.DeepEqual(commitFiles, want) {
   539  		t.Errorf("PullRequests.ListFiles returned %+v, want %+v", commitFiles, want)
   540  	}
   541  }
   542  
   543  func TestPullRequestsService_IsMerged(t *testing.T) {
   544  	client, mux, _, teardown := setup()
   545  	defer teardown()
   546  
   547  	mux.HandleFunc("/repos/o/r/pulls/1/merge", func(w http.ResponseWriter, r *http.Request) {
   548  		testMethod(t, r, "GET")
   549  		w.WriteHeader(http.StatusNoContent)
   550  	})
   551  
   552  	isMerged, _, err := client.PullRequests.IsMerged(context.Background(), "o", "r", 1)
   553  	if err != nil {
   554  		t.Errorf("PullRequests.IsMerged returned error: %v", err)
   555  	}
   556  
   557  	want := true
   558  	if !reflect.DeepEqual(isMerged, want) {
   559  		t.Errorf("PullRequests.IsMerged returned %+v, want %+v", isMerged, want)
   560  	}
   561  }
   562  
   563  func TestPullRequestsService_Merge(t *testing.T) {
   564  	client, mux, _, teardown := setup()
   565  	defer teardown()
   566  
   567  	mux.HandleFunc("/repos/o/r/pulls/1/merge", func(w http.ResponseWriter, r *http.Request) {
   568  		testMethod(t, r, "PUT")
   569  		fmt.Fprint(w, `
   570  			{
   571  			  "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
   572  			  "merged": true,
   573  			  "message": "Pull Request successfully merged"
   574  			}`)
   575  	})
   576  
   577  	options := &PullRequestOptions{MergeMethod: "rebase"}
   578  	merge, _, err := client.PullRequests.Merge(context.Background(), "o", "r", 1, "merging pull request", options)
   579  	if err != nil {
   580  		t.Errorf("PullRequests.Merge returned error: %v", err)
   581  	}
   582  
   583  	want := &PullRequestMergeResult{
   584  		SHA:     String("6dcb09b5b57875f334f61aebed695e2e4193db5e"),
   585  		Merged:  Bool(true),
   586  		Message: String("Pull Request successfully merged"),
   587  	}
   588  	if !reflect.DeepEqual(merge, want) {
   589  		t.Errorf("PullRequests.Merge returned %+v, want %+v", merge, want)
   590  	}
   591  }
   592  
   593  // Test that different merge options produce expected PUT requests. See issue https://github.com/google/go-github/issues/500.
   594  func TestPullRequestsService_Merge_options(t *testing.T) {
   595  	client, mux, _, teardown := setup()
   596  	defer teardown()
   597  
   598  	tests := []struct {
   599  		options  *PullRequestOptions
   600  		wantBody string
   601  	}{
   602  		{
   603  			options:  nil,
   604  			wantBody: `{"commit_message":"merging pull request"}`,
   605  		},
   606  		{
   607  			options:  &PullRequestOptions{},
   608  			wantBody: `{"commit_message":"merging pull request"}`,
   609  		},
   610  		{
   611  			options:  &PullRequestOptions{MergeMethod: "rebase"},
   612  			wantBody: `{"commit_message":"merging pull request","merge_method":"rebase"}`,
   613  		},
   614  		{
   615  			options:  &PullRequestOptions{SHA: "6dcb09b5b57875f334f61aebed695e2e4193db5e"},
   616  			wantBody: `{"commit_message":"merging pull request","sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e"}`,
   617  		},
   618  		{
   619  			options: &PullRequestOptions{
   620  				CommitTitle: "Extra detail",
   621  				SHA:         "6dcb09b5b57875f334f61aebed695e2e4193db5e",
   622  				MergeMethod: "squash",
   623  			},
   624  			wantBody: `{"commit_message":"merging pull request","commit_title":"Extra detail","merge_method":"squash","sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e"}`,
   625  		},
   626  	}
   627  
   628  	for i, test := range tests {
   629  		madeRequest := false
   630  		mux.HandleFunc(fmt.Sprintf("/repos/o/r/pulls/%d/merge", i), func(w http.ResponseWriter, r *http.Request) {
   631  			testMethod(t, r, "PUT")
   632  			testBody(t, r, test.wantBody+"\n")
   633  			madeRequest = true
   634  		})
   635  		_, _, _ = client.PullRequests.Merge(context.Background(), "o", "r", i, "merging pull request", test.options)
   636  		if !madeRequest {
   637  			t.Errorf("%d: PullRequests.Merge(%#v): expected request was not made", i, test.options)
   638  		}
   639  	}
   640  }