-
Notifications
You must be signed in to change notification settings - Fork 492
/
opsgenietest.go
63 lines (58 loc) · 1.3 KB
/
opsgenietest.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
package opsgenie2test
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) {
or := Request{
URL: r.URL.String(),
Authorization: r.Header.Get("Authorization"),
}
dec := json.NewDecoder(r.Body)
dec.Decode(&or.PostData)
s.mu.Lock()
s.requests = append(s.requests, or)
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
Authorization string
}
type PostData struct {
Message string `json:"message"`
Entity string `json:"entity"`
Alias string `json:"alias"`
Note string `json:"note"`
Priority string `json:"priority"`
Description string `json:"description"`
Details map[string]string `json:"details"`
Responders []map[string]string `json:"responders"`
}