forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
134 lines (111 loc) · 3.2 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package wavefront
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestClientGet(t *testing.T) {
params := &map[string]string{
"s": "144242525262",
"e": "142252272822",
"includeObsoleteMetrics": "true",
}
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if r.URL.Path != "/api/v2/test/thing" {
t.Errorf("request path, expected /api/v2/test/thing, got %s", r.URL.Path)
}
if header, ok := r.Header["Authorization"]; ok {
if header[0] != "Bearer 123456789" {
t.Errorf("Authorization header, expected 'Bearer 123456789', got %s", header[0])
}
} else {
t.Errorf("no Authorization header set")
}
for k, v := range *params {
if r.Form.Get(k) != v {
t.Errorf("request param, expected %s, got %s", v, r.Form.Get(k))
}
}
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
client, err := NewClient(&Config{
Address: strings.TrimLeft(srv.URL, "https://"),
Token: "123456789",
SkipTLSVerify: true,
})
if err != nil {
t.Fatal("error initiating client:", err)
}
req, err := client.NewRequest("GET", "test/thing", params, nil)
if err != nil {
t.Fatal("error creating request:", err)
}
resp, err := client.Do(req)
if err != nil {
t.Fatal("error executing request:", err)
}
output, _ := ioutil.ReadAll(resp)
fmt.Println(string(output))
}
func TestClientPost(t *testing.T) {
params := &map[string]string{
"s": "144242525262",
"e": "142252272822",
"includeObsoleteMetrics": "true",
}
body := []byte(`{ "some" : "json" }`)
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
if r.URL.Path != "/api/v2/test/thing" {
t.Errorf("request path, expected /api/v2/test/thing, got %s", r.URL.Path)
}
if header, ok := r.Header["Authorization"]; ok {
if header[0] != "Bearer 123456789" {
t.Errorf("Authorization header, expected 'Bearer 123456789', got %s", header[0])
}
} else {
t.Errorf("no Authorization header set")
}
if header, ok := r.Header["Content-Type"]; ok {
if header[0] != "application/json" {
t.Errorf("Authorization header, expected 'application/json', got %s", header[0])
}
} else {
t.Errorf("no Content-Type header set")
}
actualBody, _ := ioutil.ReadAll(r.Body)
if string(actualBody) != string(body) {
t.Errorf("request body, expected %s got %s", string(body), string(actualBody))
}
for k, v := range *params {
if r.Form.Get(k) != v {
t.Errorf("request param, expected %s, got %s", v, r.Form.Get(k))
}
}
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
client, err := NewClient(&Config{
Address: strings.TrimLeft(srv.URL, "https://"),
Token: "123456789",
SkipTLSVerify: true,
})
if err != nil {
t.Fatal("error initiating client:", err)
}
req, err := client.NewRequest("POST", "test/thing", params, body)
if err != nil {
t.Fatal("error creating request:", err)
}
resp, err := client.Do(req)
if err != nil {
t.Fatal("error executing request:", err)
}
output, _ := ioutil.ReadAll(resp)
fmt.Println(string(output))
}