-
Notifications
You must be signed in to change notification settings - Fork 19
/
api_test.go
67 lines (57 loc) · 1.94 KB
/
api_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
package lokalise
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
)
const (
baseURLPath = "/api2"
testApiToken = "apiToken"
testProjectID = "3002780358964f9bab5a92.87762498"
assertionTemplate = "%s returned \n%+v\nexpected\n%+v"
)
func setup() (api *Api, 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 := httptest.NewServer(apiHandler)
apiURL, _ := url.Parse(server.URL + baseURLPath + "/")
api, _ = New(testApiToken, WithBaseURL(apiURL.String()))
return api, 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)
}
}
func testHeader(t *testing.T, r *http.Request, header string, want string) {
if got := r.Header.Get(header); got != want {
t.Errorf("Header.Get(%q) returned %q, want %q", header, 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 \n%s\n want\n%s", got, want)
}
}