forked from wavefrontHQ/go-wavefront-management-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
50 lines (46 loc) · 1.58 KB
/
testing.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
package wavefront
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"testing"
)
// Helps expedite the boiler plate code for testing client requests
// iface is the destination type we need marshal data into from the request
// iface can be utilized to ensure the request body is properly being marshalled and values are set as expected
func testDo(t *testing.T, req *http.Request, fixture, method string, iface interface{}) (io.ReadCloser, error) {
response, err := ioutil.ReadFile(fixture)
if err != nil {
t.Fatal(err)
}
assertEqual(t, method, req.Method)
if req.Body != nil {
body, _ := ioutil.ReadAll(req.Body)
err = json.Unmarshal(body, iface)
if err != nil {
t.Fatal(err)
}
} else {
t.Log("Request body was nil, if this is expected please ignore...")
}
return ioutil.NopCloser(bytes.NewReader(response)), nil
}
// Helps expedite the boiler plate code for testing client requests against paginated results
// tMarshal is the destination type we need marshal data into from the response body of the fixture
func testPaginatedDo(t *testing.T, req *http.Request, fixture string, invokedCount *int) (io.ReadCloser, error) {
search := SearchParams{}
resp, err := testDo(t, req, fmt.Sprintf(fixture, *invokedCount), "POST", &search)
assertEqual(t, search.Limit*(*invokedCount), search.Offset)
*invokedCount++
return resp, err
}
func assertEqual(t *testing.T, expected, actual interface{}) {
if expected != actual {
t.Errorf("Expected %v (type %v), actual %v (type %v)", expected, reflect.TypeOf(expected),
actual, reflect.TypeOf(actual))
}
}