-
Notifications
You must be signed in to change notification settings - Fork 5
/
reporter.go
69 lines (61 loc) · 1.66 KB
/
reporter.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
65
66
67
68
69
/*
Copyright 2019 Adevinta
*/
package tools
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"github.com/adevinta/vulcan-check-sdk/agent"
)
// Reporter represents a "fake" agent suitable to be used in tests.
type Reporter struct {
srv *httptest.Server
URL string
Msgs chan agent.State
}
// Stop the underlaying HTTPServer and closes the channel used to receive messages.
func (r *Reporter) Stop() {
r.srv.Close()
// The call above only returns when all pending requests are processed, thus is safe to close the channel.
close(r.Msgs)
}
// NewReporter creates a minimal http server that receives and sends to a channel the messages received
// by a check with a given checkID. Should be only used for test pourposes.
func NewReporter(checkID string) *Reporter {
c := make(chan agent.State, 10)
srv := buildHTTPServer(checkID, c)
agentAddress, _ := url.Parse(srv.URL) //nolint
r := &Reporter{
Msgs: c,
srv: srv,
URL: agentAddress.Hostname() + ":" + agentAddress.Port(),
}
return r
}
func buildHTTPServer(checkID string, msgs chan<- agent.State) (s *httptest.Server) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check the the id if the check is present.
parts := strings.Split(r.URL.Path, "/")
if len(parts) < 1 {
w.WriteHeader(http.StatusForbidden)
return
}
if parts[len(parts)-1] != checkID {
w.WriteHeader(http.StatusForbidden)
return
}
decoder := json.NewDecoder(r.Body)
msg := agent.State{}
err := decoder.Decode(&msg)
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
return
}
msgs <- msg
w.WriteHeader(http.StatusOK)
})
return httptest.NewServer(h)
}