forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpchecker_test.go
117 lines (106 loc) · 3.12 KB
/
httpchecker_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
package checkup
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestHTTPChecker(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "I'm up")
}))
endpt := "http://" + srv.Listener.Addr().String()
hc := HTTPChecker{Name: "Test", URL: endpt, Attempts: 2}
// Try an up server
result, err := hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Title, "Test"; got != want {
t.Errorf("Expected result.Title='%s', got '%s'", want, got)
}
if got, want := result.Endpoint, endpt; got != want {
t.Errorf("Expected result.Endpoint='%s', got '%s'", want, got)
}
if got, want := result.Down, false; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
if got, want := result.Degraded, false; got != want {
t.Errorf("Expected result.Degraded=%v, got %v", want, got)
}
if got, want := result.Healthy, true; got != want {
t.Errorf("Expected result.Healthy=%v, got %v", want, got)
}
if got, want := len(result.Times), hc.Attempts; got != want {
t.Errorf("Expected %d attempts, got %d", want, got)
}
ts := time.Unix(0, result.Timestamp)
if time.Since(ts) > 5*time.Second {
t.Errorf("Expected timestamp to be recent, got %s", ts)
}
// Try various different down criteria
hc.UpStatus = 201
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Down, true; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
hc.UpStatus = 200
hc.ThresholdRTT = 1 * time.Nanosecond
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Degraded, true; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
hc.ThresholdRTT = 0
hc.MustContain = "up"
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Down, false; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
hc.MustContain = "online"
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Down, true; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
hc.MustContain = ""
hc.MustNotContain = "down"
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Down, false; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
hc.MustNotContain = "I"
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := result.Down, true; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
// Try when the server is not even online
srv.Listener.Close()
result, err = hc.Check()
if err != nil {
t.Errorf("Didn't expect an error: %v", err)
}
if got, want := len(result.Times), hc.Attempts; got != want {
t.Errorf("Expected %d attempts, got %d", want, got)
}
if got, want := result.Down, true; got != want {
t.Errorf("Expected result.Down=%v, got %v", want, got)
}
}