-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommons_test.go
63 lines (50 loc) · 1.49 KB
/
commons_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
package actuator
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"runtime/pprof"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
var (
tempEncodeJSONFunction = encodeJSONFunction
tempPprofLookupFunction = pprofLookupFunction
)
func setupMuxAndGetResponse(t *testing.T, endpoint int, path string) *httptest.ResponseRecorder {
return setupMuxWithConfigAndGetResponseForMethod(t, &Config{Endpoints: []int{endpoint}}, http.MethodGet, path)
}
func setupMuxWithConfigAndGetResponseForMethod(t *testing.T, config *Config, method, path string) *httptest.ResponseRecorder {
mux := &http.ServeMux{}
mux.Handle("/", GetActuatorHandler(config))
request, err := http.NewRequest(method, path, nil)
assert.NoError(t, err)
w := httptest.NewRecorder()
mux.ServeHTTP(w, request)
return w
}
func getTypedJSONBody(t *testing.T, body *bytes.Buffer, v interface{}) {
decoder := jsoniter.NewDecoder(bytes.NewReader(body.Bytes()))
err := decoder.Decode(v)
assert.NoError(t, err)
}
func mockEncodeJSONWithError() {
tempEncodeJSONFunction = encodeJSONFunction
encodeJSONFunction = func(interface{}) ([]byte, error) {
return nil, errors.New("error")
}
}
func unMockEncodeJSON() {
encodeJSONFunction = tempEncodeJSONFunction
}
func mockPprofLookupWithError() {
tempPprofLookupFunction = pprofLookupFunction
pprofLookupFunction = func(name string) *pprof.Profile {
return nil
}
}
func unMockPprofLookup() {
pprofLookupFunction = tempPprofLookupFunction
}