forked from wavefrontHQ/wavefront-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server_test.go
More file actions
109 lines (99 loc) · 2.51 KB
/
test_server_test.go
File metadata and controls
109 lines (99 loc) · 2.51 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package senders
import (
"bufio"
"compress/gzip"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
)
func startTestServer(useTLS bool) *testServer {
ts := &testServer{}
handler := http.NewServeMux()
handler.HandleFunc("/api/v2/event", ts.EventAPIEndpoint)
handler.HandleFunc("/", ts.ReportEndpoint)
if useTLS {
ts.httpServer = httptest.NewTLSServer(handler)
} else {
ts.httpServer = httptest.NewServer(handler)
}
ts.URL = ts.httpServer.URL
return ts
}
type testServer struct {
MetricLines []string
EventLines []string
AuthHeaders []string
httpServer *httptest.Server
URL string
RequestURLs []string
}
func (s *testServer) TLSConfig() *tls.Config {
certPool := x509.NewCertPool()
certPool.AddCert(s.httpServer.Certificate())
return &tls.Config{
RootCAs: certPool,
}
}
func (s *testServer) ReportEndpoint(writer http.ResponseWriter, request *http.Request) {
newLines, err := decodeLines(request)
if err != nil {
writer.WriteHeader(500)
return
}
s.MetricLines = append(s.MetricLines, newLines...)
s.AuthHeaders = append(s.AuthHeaders, request.Header.Get("Authorization"))
s.RequestURLs = append(s.RequestURLs, request.URL.String())
writer.WriteHeader(200)
}
func (s *testServer) EventAPIEndpoint(writer http.ResponseWriter, request *http.Request) {
fmt.Println(request.URL.Path)
newLines, err := decodeLines(request)
if err != nil {
writer.WriteHeader(500)
return
}
s.EventLines = append(s.EventLines, newLines...)
s.AuthHeaders = append(s.AuthHeaders, request.Header.Get("Authorization"))
s.RequestURLs = append(s.RequestURLs, request.URL.String())
writer.WriteHeader(200)
}
func decodeLines(request *http.Request) ([]string, error) {
var metricLines []string
var bodyReader io.Reader
defer request.Body.Close()
if request.Header.Get("Content-Encoding") == "gzip" {
r, err := gzip.NewReader(request.Body)
if err != nil {
return metricLines, err
}
defer r.Close()
bodyReader = r
} else {
bodyReader = request.Body
}
scanner := bufio.NewScanner(bodyReader)
for scanner.Scan() {
line := scanner.Text()
metricLines = append(metricLines, line)
}
if scanner.Err() != nil {
return metricLines, scanner.Err()
}
return metricLines, nil
}
func (s *testServer) Close() {
s.httpServer.Close()
}
func (s *testServer) hasReceivedLine(lineSubstring string) bool {
internalMetricFound := false
for _, line := range s.MetricLines {
if strings.Contains(line, lineSubstring) {
internalMetricFound = true
}
}
return internalMetricFound
}