forked from mr-karan/calert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
97 lines (82 loc) · 2.52 KB
/
api_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
package main
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestIndexHandler(t *testing.T) {
var (
httpClient = initClient()
notifier = NewNotifier(*httpClient)
appConfig = &App{notifier}
)
// Create a request to pass to our handler.
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
// Create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.Handler(Handler{appConfig, handleIndex})
// handler satisfy http.Handler since ServeHTTP is implemented
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestHealthCheckHandler(t *testing.T) {
var (
httpClient = initClient()
notifier = NewNotifier(*httpClient)
appConfig = &App{notifier}
)
// Create a request to pass to our handler.
req, err := http.NewRequest("GET", "/ping", nil)
if err != nil {
t.Fatal(err)
}
// Create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.Handler(Handler{appConfig, handleHealthCheck})
// handler satisfy http.Handler since ServeHTTP is implemented
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestNewAlertHandler(t *testing.T) {
var (
httpClient = initClient()
notifier = NewNotifier(*httpClient)
appConfig = &App{notifier}
)
// Create a request to pass to our handler.
// Load mock payload
configFile, err := ioutil.ReadFile("examples/mock_payload.json")
if err != nil {
log.Fatal("Error opening config file", err.Error())
os.Exit(1)
}
req, err := http.NewRequest("POST", "/create?room_name=alertManagerTestRoom", bytes.NewBuffer(configFile))
if err != nil {
t.Fatal(err)
}
// Create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.Handler(Handler{appConfig, handleNewAlert})
// handler satisfy http.Handler since ServeHTTP is implemented
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}