github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/test_helpers.go (about)

     1  package rest
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/rs/zerolog"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/koko1123/flow-go-1/access/mock"
    15  	"github.com/koko1123/flow-go-1/model/flow"
    16  )
    17  
    18  const (
    19  	ExpandableFieldPayload    = "payload"
    20  	ExpandableExecutionResult = "execution_result"
    21  	sealedHeightQueryParam    = "sealed"
    22  	finalHeightQueryParam     = "final"
    23  	startHeightQueryParam     = "start_height"
    24  	endHeightQueryParam       = "end_height"
    25  	heightQueryParam          = "height"
    26  )
    27  
    28  func executeRequest(req *http.Request, backend *mock.API) (*httptest.ResponseRecorder, error) {
    29  	var b bytes.Buffer
    30  	logger := zerolog.New(&b)
    31  	router, err := newRouter(backend, logger, flow.Testnet.Chain())
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	rr := httptest.NewRecorder()
    37  	router.ServeHTTP(rr, req)
    38  	return rr, nil
    39  }
    40  
    41  func assertOKResponse(t *testing.T, req *http.Request, expectedRespBody string, backend *mock.API) {
    42  	assertResponse(t, req, http.StatusOK, expectedRespBody, backend)
    43  }
    44  
    45  func assertResponse(t *testing.T, req *http.Request, status int, expectedRespBody string, backend *mock.API) {
    46  	rr, err := executeRequest(req, backend)
    47  	assert.NoError(t, err)
    48  
    49  	actualResponseBody := rr.Body.String()
    50  	require.JSONEq(t,
    51  		expectedRespBody,
    52  		actualResponseBody,
    53  		fmt.Sprintf("Failed Request: %s\nExpected JSON:\n %s \nActual JSON:\n %s\n", req.URL, expectedRespBody, actualResponseBody),
    54  	)
    55  	require.Equal(t, status, rr.Code)
    56  }