-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathagent_test.go
75 lines (63 loc) · 1.34 KB
/
agent_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
package agent
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"github.com/corenzan/owl/api"
)
type RoundTripFunc func(req *http.Request) *http.Response
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
func NewTestClient(fn RoundTripFunc) *http.Client {
return &http.Client{
Transport: RoundTripFunc(fn),
}
}
func TestNew(t *testing.T) {
c := New("https://api", "123")
if c.apiClient.Endpoint != "https://api" {
t.Fail()
}
if c.apiClient.Key != "123" {
t.Fail()
}
}
func TestAgentCheck(t *testing.T) {
history := []*http.Request{}
testHTTPClient := NewTestClient(func(req *http.Request) *http.Response {
history = append(history, req)
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)),
Header: http.Header{},
}
})
c := New("https://api", "123")
c.checkClient = testHTTPClient
c.apiClient.Client = testHTTPClient
check, err := c.Check(&api.Website{
ID: 1,
URL: "https://website",
})
if err != nil {
t.Fail()
}
if history[0].Method != "GET" {
t.Fail()
}
if history[0].URL.String() != "https://website" {
t.Fail()
}
err = c.Report(check)
if err != nil {
t.Fail()
}
if history[1].Method != "POST" {
t.Fail()
}
if history[1].URL.String() != "https://api/websites/1/checks" {
t.Fail()
}
}