forked from signalfx/signalfx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
74 lines (61 loc) · 1.68 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
package signalfx
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"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.Equal(t, params.Get(k), incomingParams.Get(k), "Params do match for parameter '"+k+"': '"+incomingParams.Get(k)+"'")
}
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
// Allow empty bodies
if resultPath != "" {
fmt.Fprintf(w, fixture(resultPath))
}
}
}