forked from mdlayher/unifi_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunifiexporter_test.go
48 lines (38 loc) · 1.17 KB
/
unifiexporter_test.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
package unifiexporter
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/mdlayher/unifi"
"github.com/prometheus/client_golang/prometheus"
)
func testUniFiClient(t *testing.T, input []byte) (*unifi.Client, func()) {
unifiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
_, _ = w.Write(input)
}))
c, err := unifi.NewClient(unifiServer.URL, nil)
if err != nil {
t.Fatalf("failed to create UniFi client: %v", err)
}
return c, func() { unifiServer.Close() }
}
func testCollector(t *testing.T, collector prometheus.Collector) []byte {
if err := prometheus.Register(collector); err != nil {
t.Fatalf("failed to register Prometheus collector: %v", err)
}
defer prometheus.Unregister(collector)
promServer := httptest.NewServer(prometheus.Handler())
defer promServer.Close()
resp, err := http.Get(promServer.URL)
if err != nil {
t.Fatalf("failed to GET data from prometheus: %v", err)
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read server response: %v", err)
}
return buf
}