forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
58 lines (51 loc) · 1.04 KB
/
server.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
package httpposttest
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"github.com/influxdata/kapacitor/alert"
)
type AlertServer struct {
ts *httptest.Server
URL string
data []AlertRequest
closed bool
}
func NewAlertServer(headers map[string]string, raw bool) *AlertServer {
s := new(AlertServer)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := AlertRequest{MatchingHeaders: true}
for k, v := range headers {
nv := r.Header.Get(k)
if nv != v {
req.MatchingHeaders = false
}
}
if raw {
req.Raw, _ = ioutil.ReadAll(r.Body)
json.Unmarshal(req.Raw, &req.Data)
} else {
json.NewDecoder(r.Body).Decode(&req.Data)
}
s.data = append(s.data, req)
}))
s.ts = ts
s.URL = ts.URL
return s
}
type AlertRequest struct {
MatchingHeaders bool
Data alert.Data
Raw []byte
}
func (s *AlertServer) Data() []AlertRequest {
return s.data
}
func (s *AlertServer) Close() {
if s.closed {
return
}
s.closed = true
s.ts.Close()
}