-
Notifications
You must be signed in to change notification settings - Fork 175
/
test_helpers.go
56 lines (47 loc) · 1.47 KB
/
test_helpers.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
package rest
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/access/mock"
"github.com/onflow/flow-go/model/flow"
)
const (
ExpandableFieldPayload = "payload"
ExpandableExecutionResult = "execution_result"
sealedHeightQueryParam = "sealed"
finalHeightQueryParam = "final"
startHeightQueryParam = "start_height"
endHeightQueryParam = "end_height"
heightQueryParam = "height"
)
func executeRequest(req *http.Request, backend *mock.API) (*httptest.ResponseRecorder, error) {
var b bytes.Buffer
logger := zerolog.New(&b)
router, err := newRouter(backend, logger, flow.Testnet.Chain())
if err != nil {
return nil, err
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
return rr, nil
}
func assertOKResponse(t *testing.T, req *http.Request, expectedRespBody string, backend *mock.API) {
assertResponse(t, req, http.StatusOK, expectedRespBody, backend)
}
func assertResponse(t *testing.T, req *http.Request, status int, expectedRespBody string, backend *mock.API) {
rr, err := executeRequest(req, backend)
assert.NoError(t, err)
actualResponseBody := rr.Body.String()
require.JSONEq(t,
expectedRespBody,
actualResponseBody,
fmt.Sprintf("Failed Request: %s\nExpected JSON:\n %s \nActual JSON:\n %s\n", req.URL, expectedRespBody, actualResponseBody),
)
require.Equal(t, status, rr.Code)
}