forked from signalfx/signalfx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
96 lines (79 loc) · 2.48 KB
/
client_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
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
package signalfx
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/signalfx/signalfx-go/chart"
"github.com/stretchr/testify/assert"
)
const TestToken = "abc123"
var (
mux *http.ServeMux
server *httptest.Server
client *Client
)
func fixture(path string) string {
b, err := ioutil.ReadFile("testdata/fixtures/" + path)
if err != nil {
panic(err)
}
return string(b)
}
func setup() func() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
client, _ = NewClient(TestToken, APIUrl(server.URL))
return func() {
server.Close()
}
}
// TODO: Use HTTPSuccess from testify?
func verifyRequest(t *testing.T, method string, expectToken bool, status int, params url.Values, resultPath string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if val, ok := r.Header[AuthHeaderKey]; ok {
assert.Equal(t, []string{TestToken}, val, "Incorrect auth token in headers")
} else {
if expectToken {
assert.Fail(t, "Failed to find auth token in headers")
}
}
if val, ok := r.Header["Content-Type"]; ok {
assert.Equal(t, []string{"application/json"}, val, "Incorrect content-type in headers")
} else {
assert.Fail(t, "Failed to find content type in headers")
}
assert.Equal(t, method, r.Method, "Incorrect HTTP method")
if params != nil {
incomingParams := r.URL.Query()
for k := range params {
assert.Contains(t, incomingParams, k, "Request is missing expected query parameter %q", k)
assert.Equal(t, params.Get(k), incomingParams.Get(k), "Params do match for parameter '"+k+"': '"+incomingParams.Get(k)+"'")
}
for k := range incomingParams {
assert.Contains(t, params, k, "Request contains unexpected query parameter %q", k)
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
// Allow empty bodies
if resultPath != "" {
fmt.Fprintf(w, fixture(resultPath))
}
}
}
func TestPathHandling(t *testing.T) {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
defer server.Close()
client, _ = NewClient(TestToken, APIUrl(server.URL+"/extra/path"))
mux.HandleFunc("/extra/path/v2/chart", verifyRequest(t, "POST", true, http.StatusOK, nil, "chart/create_success.json"))
result, err := client.CreateChart(context.Background(), &chart.CreateUpdateChartRequest{
Name: "string",
})
assert.NoError(t, err, "Unexpected error creating chart")
assert.Equal(t, "string", result.Name, "Name does not match")
}