-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
95 lines (85 loc) · 2.27 KB
/
client_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
package lolp
import (
"bytes"
"log"
"net/http"
"os"
"strings"
"testing"
)
func TestNew(t *testing.T) {
currentToken := os.Getenv("LOLP_TOKEN")
currentEndpoint := os.Getenv("LOLP_ENDPOINT")
os.Unsetenv("LOLP_TOKEN")
os.Unsetenv("LOLP_ENDPOINT")
defer os.Setenv("LOLP_TOKEN", currentToken)
defer os.Setenv("LOLP_ENDPOINT", currentEndpoint)
c := New()
if c.URL.String() != "https://api.mc.lolipop.jp/" {
t.Errorf("client URL is wrong: %s", c.URL)
}
if c.Token != "" {
t.Errorf("API token expects empty, but got %s", c.Token)
}
ct := strings.Join(c.DefaultHeader["Content-Type"], "")
cte := "application/json"
if ct != cte {
t.Errorf("Content-Type header expects %s, but got %s", cte, ct)
}
ua := strings.Join(c.DefaultHeader["User-Agent"], "")
uae := "lolp/" + Version + " (+https://github.com/pepabo/golipop; go"
if !strings.HasPrefix(ua, uae) {
t.Errorf("User-Agent header expects %s, but got %s", uae, ua)
}
dummyEndpoint := "https://example.com/"
os.Setenv("LOLP_ENDPOINT", dummyEndpoint)
cc := New()
if cc.URL.String() != dummyEndpoint {
t.Errorf("client URL is wrong: %s", cc.URL)
}
}
func TestNewClient(t *testing.T) {
_, err := NewClient("")
if err == nil {
t.Errorf("empty string as argument expects error")
}
dummyEndpoint := "https://example.com/"
c, err := NewClient(dummyEndpoint)
if c.URL.String() != dummyEndpoint {
t.Errorf("client URL is wrong: %s", c.URL)
}
}
func TestClientInit(t *testing.T) {
os.Setenv("LOLP_TLS_NOVERIFY", "true")
defer os.Unsetenv("LOLP_TLS_NOVERIFY")
c := &Client{
DefaultHeader: make(http.Header),
}
c.init()
if !c.HTTPClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
t.Errorf("skip verify expects true")
}
}
func TestClientRequest(t *testing.T) {
o := new(bytes.Buffer)
log.SetOutput(o)
c, err := NewClient("https://api.example.com/")
if err != nil {
t.Fatal(err)
}
c.Token = "secret"
req, err := c.Request("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
if req.Method != "GET" {
t.Errorf("HTTP method is wrong: %s", req.Method)
}
if req.URL.String() != "https://api.example.com/test" {
t.Errorf("HTTP URL is wrong: %s", req.URL)
}
a := strings.Join(req.Header["Authorization"], "")
if a != "Bearer secret" {
t.Errorf("Authorization header is wrong: %s", a)
}
}