This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
pingdom_test.go
110 lines (89 loc) · 2.12 KB
/
pingdom_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
package pingdom
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
mux *http.ServeMux
client *Client
server *httptest.Server
)
func setup() {
// test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)
// test client
client, _ = NewClientWithConfig(ClientConfig{
APIToken: "my_api_key",
})
url, _ := url.Parse(server.URL)
client.BaseURL = url
}
func teardown() {
server.Close()
}
func testMethod(t *testing.T, r *http.Request, want string) {
assert.Equal(t, want, r.Method)
}
func TestNewClientWithConfig(t *testing.T) {
c, err := NewClientWithConfig(ClientConfig{
APIToken: "key",
})
assert.NoError(t, err)
assert.Equal(t, http.DefaultClient, c.client)
assert.Equal(t, defaultBaseURL, c.BaseURL.String())
assert.NotNil(t, c.Checks)
}
func TestNewRequest(t *testing.T) {
setup()
defer teardown()
req, err := client.NewRequest("GET", "/checks", nil)
assert.NoError(t, err)
assert.Equal(t, "GET", req.Method)
assert.Equal(t, client.BaseURL.String()+"/checks", req.URL.String())
}
func TestDo(t *testing.T) {
setup()
defer teardown()
type foo struct {
A string
}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if m := "GET"; m != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, m)
}
fmt.Fprint(w, `{"A":"a"}`)
})
req, _ := client.NewRequest("GET", "/", nil)
body := new(foo)
want := &foo{"a"}
client.Do(req, body)
assert.Equal(t, want, body)
}
func TestValidateResponse(t *testing.T) {
valid := &http.Response{
Request: &http.Request{},
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader("OK")),
}
assert.NoError(t, validateResponse(valid))
invalid := &http.Response{
Request: &http.Request{},
StatusCode: http.StatusBadRequest,
Body: ioutil.NopCloser(strings.NewReader(`{
"error" : {
"statuscode": 400,
"statusdesc": "Bad Request",
"errormessage": "This is an error"
}
}`)),
}
want := &PingdomError{400, "Bad Request", "This is an error"}
assert.Equal(t, want, validateResponse(invalid))
}