github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/request/helpers_test.go (about)

     1  package request
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/onflow/flow-go/utils/unittest"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func Test_GetByID_Parse(t *testing.T) {
    14  	var getByID GetByIDRequest
    15  
    16  	id := unittest.IdentifierFixture()
    17  	err := getByID.Parse(id.String())
    18  	assert.NoError(t, err)
    19  	assert.Equal(t, getByID.ID, id)
    20  
    21  	err = getByID.Parse("1")
    22  	assert.EqualError(t, err, "invalid ID format")
    23  }
    24  
    25  func Test_ParseBody(t *testing.T) {
    26  
    27  	invalid := []struct {
    28  		in  string
    29  		err string
    30  	}{
    31  		{"{f}", "request body contains badly-formed JSON (at position 2)"},
    32  		{"foo", "request body contains badly-formed JSON (at position 2)"},
    33  		{"", "request body must not be empty"},
    34  		{`{"foo": "bar"`, "request body contains badly-formed JSON"},
    35  		{`{"foo": "bar" "foo2":"bar2"}`, "request body contains badly-formed JSON (at position 15)"},
    36  		{`{"foo":"bar"}, {}`, "request body must only contain a single JSON object"},
    37  		{`[][]`, "request body must only contain a single JSON object"},
    38  	}
    39  
    40  	for i, test := range invalid {
    41  		readerIn := strings.NewReader(test.in)
    42  		var out interface{}
    43  		err := parseBody(readerIn, out)
    44  		assert.EqualError(t, err, test.err, fmt.Sprintf("test #%d failed", i))
    45  	}
    46  
    47  	type body struct {
    48  		Foo string
    49  		Bar bool
    50  		Zoo uint64
    51  	}
    52  	var b body
    53  	err := parseBody(strings.NewReader(`{ "foo": "test", "bar": true }`), &b)
    54  	assert.NoError(t, err)
    55  	assert.Equal(t, b.Bar, true)
    56  	assert.Equal(t, b.Foo, "test")
    57  	assert.Equal(t, b.Zoo, uint64(0))
    58  
    59  	err = parseBody(strings.NewReader(`{ "foo": false }`), &b)
    60  	assert.EqualError(t, err, `request body contains an invalid value for the "Foo" field (at position 14)`)
    61  }