forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
132 lines (109 loc) · 3.01 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
package gapi
import (
"bytes"
"encoding/json"
"net/url"
"testing"
)
func TestNew_basicAuth(t *testing.T) {
c, err := New("user:pass", "http://my-grafana.com")
if err != nil {
t.Errorf("expected error to be nil; got: %s", err.Error())
}
expected := "http://user:[email protected]"
if c.baseURL.String() != expected {
t.Errorf("expected error: %s; got: %s", expected, c.baseURL.String())
}
}
func TestNew_tokenAuth(t *testing.T) {
c, err := New("123", "http://my-grafana.com")
if err != nil {
t.Errorf("expected error to be nil; got: %s", err.Error())
}
expected := "http://my-grafana.com"
if c.baseURL.String() != expected {
t.Errorf("expected error: %s; got: %s", expected, c.baseURL.String())
}
expected = "Bearer 123"
if c.key != expected {
t.Errorf("expected error: %s; got: %s", expected, c.key)
}
}
func TestNew_invalidURL(t *testing.T) {
_, err := New("123", "://my-grafana.com")
expected := "parse \"://my-grafana.com\": missing protocol scheme"
if err.Error() != expected {
t.Errorf("expected error: %v; got: %s", expected, err.Error())
}
}
func TestRequest_200(t *testing.T) {
server, client := gapiTestTools(200, `{"foo":"bar"}`)
defer server.Close()
err := client.request("GET", "/foo", url.Values{}, nil, nil)
if err != nil {
t.Errorf(err.Error())
}
}
func TestRequest_201(t *testing.T) {
server, client := gapiTestTools(201, `{"foo":"bar"}`)
defer server.Close()
err := client.request("GET", "/foo", url.Values{}, nil, nil)
if err != nil {
t.Errorf(err.Error())
}
}
func TestRequest_400(t *testing.T) {
server, client := gapiTestTools(400, `{"foo":"bar"}`)
defer server.Close()
expected := `status: 400, body: {"foo":"bar"}`
err := client.request("GET", "/foo", url.Values{}, nil, nil)
if err.Error() != expected {
t.Errorf("expected error: %v; got: %s", expected, err.Error())
}
}
func TestRequest_500(t *testing.T) {
server, client := gapiTestTools(500, `{"foo":"bar"}`)
defer server.Close()
expected := `status: 500, body: {"foo":"bar"}`
err := client.request("GET", "/foo", url.Values{}, nil, nil)
if err.Error() != expected {
t.Errorf("expected error: %v; got: %s", expected, err.Error())
}
}
func TestRequest_200Unmarshal(t *testing.T) {
server, client := gapiTestTools(200, `{"foo":"bar"}`)
defer server.Close()
result := struct {
Foo string `json:"foo"`
}{}
err := client.request("GET", "/foo", url.Values{}, nil, &result)
if err != nil {
t.Errorf(err.Error())
}
if result.Foo != "bar" {
t.Errorf("expected: bar; got: %s", result.Foo)
}
}
func TestRequest_200UnmarshalPut(t *testing.T) {
server, client := gapiTestTools(200, `{"name":"mike"}`)
defer server.Close()
u := User{
Name: "mike",
}
data, err := json.Marshal(u)
if err != nil {
t.Errorf(err.Error())
}
result := struct {
Name string `json:"name"`
}{}
q := url.Values{}
q.Add("a", "b")
err = client.request("PUT", "/foo", q, bytes.NewBuffer(data), &result)
if err != nil {
t.Errorf(err.Error())
}
if result.Name != "mike" {
t.Errorf("expected: name; got: %s", result.Name)
}
}