-
Notifications
You must be signed in to change notification settings - Fork 3
/
pagerduty_test.go
83 lines (73 loc) · 2.06 KB
/
pagerduty_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
package pagerduty
import (
"net/http"
"net/http/httptest"
"testing"
)
type pagerdutySuccessMock struct {
request *http.Request
}
func (pd *pagerdutySuccessMock) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pd.request = r
w.Write([]byte(`{"status": "success", "message": "Event processed", "incident_key": "foobar" }`))
}
type pagerdutyErrorMock struct {
request *http.Request
}
func (pd *pagerdutyErrorMock) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pd.request = r
http.Error(w, `{"status": "invalid event", "message": "Event processed", "errors": ["foobar", "barfoo"]}`, 500)
}
// All JSON come from the Official API documentation
func TestSuccessfulRoundTrip(t *testing.T) {
handler := &pagerdutySuccessMock{}
server := httptest.NewServer(handler)
Endpoint = server.URL
defer server.Close()
event := NewTriggerEvent("my_service_key", "my_description")
r, statusCode, err := Submit(event)
if err != nil {
t.Error(err)
}
if statusCode != 200 {
t.Error("StatusCode should be 200")
}
if r.Status != "success" || r.Message != "Event processed" || r.IncidentKey != "foobar" {
t.Error(r)
}
if event.IncidentKey != "foobar" {
t.Error(r)
}
}
func TestSuccessfulRoundTripIncidentKeyAlreadySet(t *testing.T) {
handler := &pagerdutySuccessMock{}
server := httptest.NewServer(handler)
Endpoint = server.URL
defer server.Close()
event := NewTriggerEvent("my_service_key", "my_description")
event.IncidentKey = "failure132/http"
r, _, _ := Submit(event)
if event.IncidentKey != "failure132/http" {
t.Error(r)
}
}
func TestErroredRoundTrip(t *testing.T) {
handler := &pagerdutyErrorMock{}
server := httptest.NewServer(handler)
Endpoint = server.URL
defer server.Close()
event := NewTriggerEvent("my_service_key", "my_description")
r, statusCode, err := Submit(event)
if err != nil {
t.Error(err)
}
if statusCode != 500 {
t.Error("StatusCode should be 500")
}
if r.Status != "invalid event" || r.Message != "Event processed" || r.IncidentKey != "" {
t.Error(r)
}
if r.Errors[0] != "foobar" || r.Errors[1] != "barfoo" {
t.Error(r)
}
}