/
test_helper.go
106 lines (88 loc) · 3.17 KB
/
test_helper.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
)
const (
testGitHubOwner = "shuheiktgw"
testGitHubRepo = "bump-reviewer"
testGitHubToken = "abcdefg12345"
)
// setup sets up a test HTTP server along with a GitHubClient that is
// configured to talk to that test server. Tests should register handlers on
// mux which provide mock responses for the API method being tested.
func setup() (client *GitHubClient, mux *http.ServeMux, serverURL string, tearDown func()) {
// mux is the HTTP request multiplexer used with the test server.
mux = http.NewServeMux()
apiHandler := http.NewServeMux()
apiHandler.Handle("/", mux)
// server is a test HTTP server used to provide mock API responses.
server := httptest.NewServer(apiHandler)
// client is the GitHub client being tested and is
// configured to use test server.
client = NewGitHubClient(testGitHubOwner, testGitHubRepo, testGitHubToken)
u, _ := url.Parse(server.URL + "/")
client.Client.BaseURL = u
return client, mux, server.URL, server.Close
}
func testMethod(t *testing.T, r *http.Request, want string) {
if got := r.Method; got != want {
t.Errorf("Request method: %v, want %v", got, want)
}
}
type values map[string]string
func testFormValues(t *testing.T, r *http.Request, values values) {
want := url.Values{}
for k, v := range values {
want.Set(k, v)
}
r.ParseForm()
if got := r.Form; !reflect.DeepEqual(got, want) {
t.Errorf("Request parameters: %v, want %v", got, want)
}
}
func testBody(t *testing.T, r *http.Request, want string) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("Error reading request body: %v", err)
}
if got := string(b); got != want {
t.Errorf("request Body is %s, want %s", got, want)
}
}
func setupReviewer() (reviewer *Reviewer, mux *http.ServeMux, url string, tearDown func()) {
client, mux, url, tearDown := setup()
return &Reviewer{client}, mux, url, tearDown
}
func setPullRequestFilesHandler(mux *http.ServeMux, number int, files string) {
mux.HandleFunc(fmt.Sprintf("/repos/%v/%v/pulls/%d/files", testGitHubOwner, testGitHubRepo, number), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, files)
})
}
func setCreateReviewHandler(mux *http.ServeMux, number int, state string) {
mux.HandleFunc(fmt.Sprintf("/repos/%v/%v/pulls/%d/reviews", testGitHubOwner, testGitHubRepo, number), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, fmt.Sprintf(`{"state":"%s"}`, state))
})
}
func setReleaseHandler(mux *http.ServeMux, tag string) {
mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/releases/latest", testGitHubOwner, testGitHubRepo), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{"tag_name":"%s"}`, tag)
})
}
func setGetContentHandler(mux *http.ServeMux, version string) {
path := fmt.Sprintf("lib/%s/version.rb", testGitHubRepo)
content := fmt.Sprintf(`
module BumpReviewer
VERSION="%s"
end
`, version)
mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/contents/%s", testGitHubOwner, testGitHubRepo, path), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{"content":"%s","encoding":"base64"}`, base64.StdEncoding.EncodeToString([]byte(content)))
})
}