-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_test.go
60 lines (55 loc) · 1.64 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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
sensu "github.com/sensu/core/v2"
)
type testHandler struct {
t *testing.T
event *sensu.Event
}
func (h testHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !strings.HasSuffix(req.URL.Path, "/events") {
http.Error(w, "not found", 404)
return
}
var event sensu.Event
if err := json.NewDecoder(req.Body).Decode(&event); err != nil {
http.Error(w, "bad request", 400)
return
}
if got, want := event.Check.Status, uint32(1); got != want {
h.t.Errorf("bad check status: got %d, want %d", got, want)
}
if got, want := event.Check.Output, "output"; got != want {
h.t.Errorf("bad check output: got %q, want %q", got, want)
}
if got, want := event.Check.Name, "bar-failure"; got != want {
h.t.Errorf("bad check name: got %q, want %q", got, want)
}
if got, want := event.Check.Issued, h.event.Check.Issued; got != want {
h.t.Errorf("bad check issued: got %d, want %d", got, want)
}
if got, want := event.Check.Command, h.event.Check.Command; got != want {
h.t.Errorf("bad check command: got %q, want %q", got, want)
}
if got, want := event.Entity, h.event.Entity; !reflect.DeepEqual(got, want) {
h.t.Errorf("bad entity: got %v, want %v", got, want)
}
}
func TestSendEvent(t *testing.T) {
event := sensu.FixtureEvent("foo", "bar")
server := httptest.NewServer(testHandler{t: t, event: event})
defer server.Close()
outputEvent, err := createEvent(event, 1, "{{ .Check.Name }}-failure", "output")
if err != nil {
t.Fatal(err)
}
if err := sendEvent(server.URL+"/events", outputEvent); err != nil {
t.Fatal(err)
}
}