forked from cherryservers/cherrygo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cherrygo_test.go
100 lines (79 loc) · 2.07 KB
/
cherrygo_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package cherrygo
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
)
var (
mux *http.ServeMux
client *Client
server *httptest.Server
teamID int
projectID int
)
var authToken = "myToken"
func setup() {
os.Setenv("CHERRY_AUTH_TOKEN", authToken)
mux = http.NewServeMux()
server = httptest.NewServer(mux)
client, _ = NewClient()
url, _ := url.Parse(server.URL)
client.BaseURL = url
teamID = 123
projectID = 321
}
func teardown() {
server.Close()
}
func testMethod(t *testing.T, r *http.Request, expected string) {
if expected != r.Method {
t.Errorf("Request method = %v, expected %v", r.Method, expected)
}
}
func TestNewClient(t *testing.T) {
setup()
defer teardown()
if client.BaseURL == nil || client.BaseURL.String() != server.URL {
t.Errorf("NewClient BaseURL = %v, expected %v", client.BaseURL, server.URL)
}
if client.UserAgent != userAgent {
t.Errorf("NewClient UserAgent = %v, expected %v", client.UserAgent, userAgent)
}
}
func TestNewClientWithAuthVar(t *testing.T) {
c, _ := NewClient(WithAuthToken(authToken))
if c.AuthToken != authToken {
t.Errorf("NewClient AuthToken = %v, expected %v", client.AuthToken, authToken)
}
}
func TestErrorResponse(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusBadRequest)
fmt.Fprint(writer, `{
"code": 400,
"message": "Bad Request"
}`)
})
_, err := client.MakeRequest(http.MethodGet, "/", nil, nil)
expectedErr := "Error response from API: Bad Request (error code: 400)"
if err.Error() != expectedErr {
t.Fatalf("NewClient() expected error: %v, got %v", expectedErr, err)
}
}
func TestCustomUserAgent(t *testing.T) {
os.Setenv("CHERRY_AUTH_TOKEN", "token")
ua := "testing/1.0"
c, err := NewClient(WithUserAgent(ua))
if err != nil {
t.Fatalf("NewClient() unexpected error: %v", err)
}
expected := fmt.Sprintf("%s %s", ua, userAgent)
if got := c.UserAgent; got != expected {
t.Errorf("NewClient() UserAgent = %s; expected %s", got, expected)
}
}