forked from thingio/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagerdutytest.go
59 lines (54 loc) · 1.05 KB
/
pagerdutytest.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
package pagerdutytest
import (
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
)
type Server struct {
mu sync.Mutex
ts *httptest.Server
URL string
requests []Request
closed bool
}
func NewServer() *Server {
s := new(Server)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pr := Request{
URL: r.URL.String(),
}
dec := json.NewDecoder(r.Body)
dec.Decode(&pr.PostData)
s.mu.Lock()
s.requests = append(s.requests, pr)
s.mu.Unlock()
}))
s.ts = ts
s.URL = ts.URL
return s
}
func (s *Server) Requests() []Request {
s.mu.Lock()
defer s.mu.Unlock()
return s.requests
}
func (s *Server) Close() {
if s.closed {
return
}
s.closed = true
s.ts.Close()
}
type Request struct {
URL string
PostData PostData
}
type PostData struct {
ServiceKey string `json:"service_key"`
EventType string `json:"event_type"`
Description string `json:"description"`
Client string `json:"client"`
ClientURL string `json:"client_url"`
Details string `json:"details"`
}