forked from tolleiv/json-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
51 lines (40 loc) · 1.17 KB
/
main_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
49
50
51
package main
import (
"testing"
"net/http"
"net/http/httptest"
"fmt"
"net/url"
"io/ioutil"
"strings"
)
var probeTests = []struct {
in_data string
in_field string
out_http_code int
out_value string
}{
{"{\"field\": \"23.0C\"}", "$.field", 200, "23"},
{"{\"field\": 19}", "$.field", 200, "19"},
{"{\"field\": 19}", "$.undefined", 404, "Jsonpath not found"},
}
func TestProbeHandler(t *testing.T) {
for _, tt := range probeTests {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, tt.in_data)
}))
defer ts.Close()
u := fmt.Sprintf("http://example.com/probe?target=%s&jsonpath.value=%s", url.QueryEscape(ts.URL), url.QueryEscape(tt.in_field))
req := httptest.NewRequest("GET", u, nil)
w := httptest.NewRecorder()
probeHandler(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
if tt.out_http_code != resp.StatusCode {
t.Error(fmt.Sprintf("HTTP Code mismatch - %d expected %d", resp.StatusCode, tt.out_http_code))
}
if ! strings.Contains(string(body), tt.out_value) {
t.Error(fmt.Sprintf("Expected output: %s got\n%s", tt.out_value, string(body)))
}
}
}