-
Notifications
You must be signed in to change notification settings - Fork 3
/
probe_http_test.go
80 lines (66 loc) · 1.88 KB
/
probe_http_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
package poller
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
type successTestHandler struct {
}
type errorTestHandler struct {
}
type timeoutTestHandler struct {
}
func (p successTestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
func (p errorTestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Error(w, "error", 500)
}
func (p timeoutTestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
}
func TestSuccessfullTest(t *testing.T) {
server := httptest.NewServer(successTestHandler{})
defer server.Close()
probe := NewHttpProbe("foobar", 10*time.Second)
c, _ := NewCheck("foobar", "10s", false, "", false, make(map[string]interface{}))
c.Config.Set("url", server.URL)
event := probe.Test(c)
if event.StatusCode != 200 {
t.Error("statusCode should be 200")
}
if event.IsUp() != true {
t.Error("IsUp() should be true")
}
if event.Duration.Nanoseconds() == 0 {
t.Error("Duration can't be equals to 0 nanosecond")
}
}
func TestFailedTest(t *testing.T) {
server := httptest.NewServer(errorTestHandler{})
defer server.Close()
probe := NewHttpProbe("foobar", 10*time.Second)
c, _ := NewCheck("foobar", "10s", false, "", false, make(map[string]interface{}))
c.Config.Set("url", server.URL)
event := probe.Test(c)
if event.StatusCode != 500 {
t.Error("statusCode should be 500")
}
if event.IsUp() != false {
t.Error("IsUp() should be false")
}
}
func TestTimeoutedTest(t *testing.T) {
server := httptest.NewServer(timeoutTestHandler{})
defer server.Close()
probe := NewHttpProbe("foobar", 100*time.Millisecond)
c, _ := NewCheck("foobar", "10s", false, "", false, make(map[string]interface{}))
c.Config.Set("url", server.URL)
event := probe.Test(c)
if event.StatusCode != 0 {
t.Error("statusCode should be 0")
}
if event.IsUp() != false {
t.Error("IsUp() should be false")
}
}