This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromAlert_test.go
62 lines (55 loc) · 1.51 KB
/
promAlert_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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func Test_promAlertHandler(t *testing.T) {
// Test GET requests - should not work
req, err := http.NewRequest("GET", "/alert", nil)
if err != nil {
t.Fatal(err)
}
rr := sendRequest(req)
assertEqual(t, rr.Code, http.StatusMethodNotAllowed, "")
// Test to Post a valid alert
testFile, err := os.Open("promTest.json")
if err != nil {
t.Fatal(err)
}
req, err = http.NewRequest("POST", "/alert", testFile)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
rr = sendRequest(req)
assertEqual(t, rr.Code, http.StatusOK, "")
req, err = http.NewRequest("POST", "/alert", strings.NewReader("{\"abc\": \"\"}"))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
rr = sendRequest(req)
assertEqual(t, rr.Code, http.StatusOK, "")
}
func assertEqual(t *testing.T, a interface{}, b interface{}, message string) {
if a == b {
return
}
if len(message) == 0 {
message = fmt.Sprintf("%v != %v", a, b)
}
t.Fatal(message)
}
func sendRequest(request *http.Request) *httptest.ResponseRecorder {
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
response := httptest.NewRecorder()
handler := http.HandlerFunc(promAlertHandler)
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(response, request)
return response
}