-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
60 lines (48 loc) · 1.15 KB
/
main_test.go
File metadata and controls
60 lines (48 loc) · 1.15 KB
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"
"os"
"testing"
"github.com/codemonauts/alertmanager-fe2-bridge/alamos"
)
func TestInputHandler(t *testing.T) {
file, err := os.Open("./testdata.json")
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", "/input", file)
if err != nil {
t.Fatal(err)
}
server := alarmosMock()
defer server.Close()
client := alamos.NewClient(server.URL, "", "", true)
debug := true
rr := httptest.NewRecorder()
handler := inputHandler(&client, "", debug)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func alarmosMock() *httptest.Server {
handler := http.NewServeMux()
handler.HandleFunc("/rest/external/http/v2", inputMock)
srv := httptest.NewServer(handler)
return srv
}
func inputMock(w http.ResponseWriter, r *http.Request) {
resp := alamos.Response{
Status: "OK",
}
data, _ := json.Marshal(resp)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := w.Write(data)
if err != nil {
panic(err)
}
}