forked from anthonydahanne/stash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergepullrequest_test.go
77 lines (67 loc) · 1.99 KB
/
mergepullrequest_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package stash
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestMergePullRequest(t *testing.T) {
var tests = []struct {
project string
slug string
id int
version int
responseCode int
}{
{
project: "PROJ",
slug: "slug",
id: 777,
version: 1,
responseCode: 200,
},
{
project: "PROJ",
slug: "slug",
id: 777,
version: 1,
responseCode: 404,
},
}
for testNumber, test := range tests {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Test %d: wanted POST but found %s\n", testNumber, r.Method)
}
url := *r.URL
wantPath := fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/merge", test.project, test.slug, test.id)
if url.Path != wantPath {
t.Errorf("Test %d: want %s, got %s\n", testNumber, wantPath, url.Path)
}
version := url.Query()["version"][0]
if version != fmt.Sprintf("%d", test.version) {
t.Errorf("Test %d: want %d but got %s\n", testNumber, test.version, version)
}
noCheckHeader := r.Header.Get("X-Atlassian-Token")
if noCheckHeader != "no-check" {
t.Errorf("Test %d: Want X-Atlassian-Token header value no-check, but got %s\n", testNumber, noCheckHeader)
}
if r.Header.Get("Authorization") != "Basic dTpw" {
t.Errorf("Test %d: want Basic dTpw but found %s\n", testNumber, r.Header.Get("Authorization"))
}
w.WriteHeader(test.responseCode)
}))
defer testServer.Close()
url, _ := url.Parse(testServer.URL)
stashClient := NewClient("u", "p", url)
err := stashClient.MergePullRequest(test.project, test.slug, test.id, test.version)
if test.responseCode != 200 {
if err == nil {
t.Fatalf("Test %d: not expecting error for non-200 response code: %v\n", testNumber, err)
}
} else if err != nil {
t.Fatalf("Test %d: not expecting error: %v\n", testNumber, err)
}
}
}