forked from cloudradar/frontman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
109 lines (87 loc) · 2.86 KB
/
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
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
package frontman
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPingHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/ping", nil)
assert.Equal(t, nil, err)
rr := httptest.NewRecorder()
handler := http.HandlerFunc(pingHandler)
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
expected := `{"alive": true}`
assert.Equal(t, expected, rr.Body.String())
}
func TestHttpCheckHandler(t *testing.T) {
checks := `{
"webChecks": [{
"checkUUID": "web_head_status_matched",
"check": { "url": "https://www.google.com", "method": "head", "expectedHttpStatus": 200}
}]
}`
cfg := NewConfig()
fm := helperCreateFrontman(t, cfg)
reader := strings.NewReader(checks)
req, err := http.NewRequest("POST", "/check", reader)
assert.Equal(t, nil, err)
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fm.checkHandler)
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
data := rr.Body.Bytes()
assert.Equal(t, true, len(data) > 2)
var f interface{}
err = json.Unmarshal(data, &f)
assert.Equal(t, nil, err)
dec1 := f.([]interface{})
dec := dec1[0].(map[string]interface{})
measurements := dec["measurements"].(map[string]interface{})
assert.Equal(t, nil, dec["message"])
assert.Equal(t, "webCheck", dec["checkType"])
assert.Equal(t, "web_head_status_matched", dec["checkUuid"])
assert.Equal(t, 200., measurements["http.head.httpStatusCode"])
assert.Equal(t, 1., measurements["http.head.success"])
assert.Equal(t, map[string]interface{}{
"dontFollowRedirects": false,
"expectedHttpStatus": 200.,
"method": "head",
"searchHtmlSource": false,
"url": "https://www.google.com",
}, dec["check"])
}
func TestHttpCheckInvalidHost(t *testing.T) {
checks := `{
"webChecks": [{
"checkUUID": "webcheck_broken",
"check": { "url": "http://notfound.site.com/", "method": "get", "expectedHttpStatus": 200}
}]
}`
cfg := NewConfig()
fm := helperCreateFrontman(t, cfg)
reader := strings.NewReader(checks)
req, err := http.NewRequest("POST", "/check", reader)
assert.Equal(t, nil, err)
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(fm.checkHandler)
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
data := rr.Body.Bytes()
assert.Equal(t, true, len(data) > 2)
var f interface{}
err = json.Unmarshal(data, &f)
assert.Equal(t, nil, err)
dec1 := f.([]interface{})
dec := dec1[0].(map[string]interface{})
assert.NotEqual(t, nil, dec["measurements"])
measurements := dec["measurements"].(map[string]interface{})
assert.Equal(t, "webCheck", dec["checkType"])
assert.Equal(t, "webcheck_broken", dec["checkUuid"])
assert.Equal(t, 0., measurements["http.get.success"])
}