-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_monitor_test.go
95 lines (87 loc) · 2.46 KB
/
http_monitor_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 gerty
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"testing"
"time"
)
func TestShouldPingValidUrl(t *testing.T) {
url := "http://www.google.com"
monitor := NewHttpMonitor("Google Home monitor", "this monitor pings google home page", url)
status := monitor.Check()
if status != OK {
t.Fatalf("error while checking url %s", url)
}
}
func TestShouldFailOnTimeout(t *testing.T) {
// non-routeable IP address.
url := "http://10.255.255.1/resource"
opts := HttpMonitorOptions{Checks: 5, Method: "GET", Timeout: 10 * time.Millisecond}
monitor := NewHttpMonitorWithOptions("Timeout Monitor", "This monitor should timeout", url, &opts)
status := monitor.Check()
if status != NOK {
t.Fatalf("http monitor should timeout and fail")
}
}
func TestShouldFailOnBadStatusCode(t *testing.T) {
url := "http://httpstat.us/500"
monitor := NewHttpMonitor("Always 500 Monitor", "This monitor should fail because of the 500 response", url)
status := monitor.Check()
if status != NOK {
t.Fatalf("http monitor should fail")
}
}
func tenBytes(t *testing.T) SuccessChecker {
return func(response *http.Response) bool {
length := response.Header.Get("Content-Length")
bytes, err := strconv.Atoi(length)
if err != nil {
t.Logf("failed to convert %s to an int", length)
return false
} else {
return bytes == 10
}
}
}
func TestCustomSuccessChecker(t *testing.T) {
url := "http://httpbin.org/bytes/10"
opts := HttpMonitorOptions{Successful: tenBytes(t)}
monitor := NewHttpMonitorWithOptions("Always 10 Bytes", "This monitor should fail if content length is different from 10", url, &opts)
status := monitor.Check()
if status != OK {
t.Fatalf("http monitor should not fail")
}
}
func checkBodyHasData(value string) SuccessChecker {
return func(response *http.Response) bool {
bs, err := ioutil.ReadAll(response.Body)
if err != nil {
return false
}
var data struct {
Data string `json:"data"`
}
err = json.Unmarshal(bs, &data)
if err != nil {
return false
} else {
return data.Data == value
}
}
}
func TestRequestWithBody(t *testing.T) {
url := "http://httpbin.org/post"
bodyString := "request-body"
opts := HttpMonitorOptions{
Method: "POST",
Body: bodyString,
Successful: checkBodyHasData(bodyString),
}
monitor := NewHttpMonitorWithOptions("Post with Body", "This monitor should send a POST with body", url, &opts)
status := monitor.Check()
if status != OK {
t.Fatalf("http monitor should not fail")
}
}