/
test_utils.go
90 lines (75 loc) · 2.8 KB
/
test_utils.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
package git_provider
import (
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-github/v52/github"
"github.com/ktrysmt/go-bitbucket"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
)
const (
// baseURLPath is a non-empty Client.BaseURL path to use during tests,
// to ensure relative URLs are used for all endpoints. See issue #752.
baseURLPath = "/api-v3"
bitbucketBaseURLPath = "/2.0"
)
func setup() (client *github.Client, mux *http.ServeMux, serverURL string, teardown func()) {
mux = http.NewServeMux()
apiHandler := http.NewServeMux()
apiHandler.Handle(baseURLPath+"/", http.StripPrefix(baseURLPath, mux))
apiHandler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(os.Stderr, "FAIL: Client.BaseURL path prefix is not preserved in the request URL:")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "\t"+req.URL.String())
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "\tDid you accidentally use an absolute endpoint URL rather than relative?")
fmt.Fprintln(os.Stderr, "\tSee https://github.com/google/go-github/issues/752 for information.")
http.Error(w, "Client.BaseURL path prefix is not preserved in the request URL.", http.StatusInternalServerError)
})
// 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 = github.NewClient(nil)
url, _ := url.Parse(server.URL + baseURLPath + "/")
client.BaseURL = url
client.UploadURL = url
return client, mux, server.URL, server.Close
}
func testMethod(t *testing.T, r *http.Request, want string) {
t.Helper()
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) {
t.Helper()
want := url.Values{}
for k, v := range values {
want.Set(k, v)
}
err := r.ParseForm()
if err != nil {
t.Errorf("Go error parsing form: %v", err)
}
if got := r.Form; !cmp.Equal(got, want) {
t.Errorf("Request parameters: %v, want %v", got, want)
}
}
func setupBitbucket() (client *bitbucket.Client, mux *http.ServeMux, serverURL string, teardown func()) {
mux = http.NewServeMux()
apiHandler := http.NewServeMux()
apiHandler.Handle(bitbucketBaseURLPath+"/", http.StripPrefix(bitbucketBaseURLPath, mux))
apiHandler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
http.Error(w, "Client.BaseURL path prefix is not preserved in the request URL.", http.StatusInternalServerError)
})
server := httptest.NewServer(apiHandler)
url, _ := url.Parse(server.URL + bitbucketBaseURLPath)
client = bitbucket.NewBasicAuth("username", "password")
client.SetApiBaseURL(*url)
return client, mux, server.URL, server.Close
}