forked from influxdata/kapacitor
/
slacktest.go
64 lines (58 loc) · 1.15 KB
/
slacktest.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
package slacktest
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) {
sr := Request{
URL: r.URL.String(),
}
dec := json.NewDecoder(r.Body)
dec.Decode(&sr.PostData)
s.mu.Lock()
s.requests = append(s.requests, sr)
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 {
Channel string `json:"channel"`
Username string `json:"username"`
Text string `json:"text"`
Attachments []Attachment `json:"attachments"`
}
type Attachment struct {
Fallback string `json:"fallback"`
Color string `json:"color"`
Text string `json:"text"`
Mrkdwn_in []string `json:"mrkdwn_in"`
}