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

     1  package request
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/koko1123/flow-go-1/engine/access/rest/util"
    11  )
    12  
    13  const validBody = "pub fun main() { }"
    14  
    15  var validBodyEncoded = util.ToBase64([]byte(validBody))
    16  
    17  func TestScript_InvalidParse(t *testing.T) {
    18  	test := map[string]string{
    19  		"":                                     "request body must not be empty",
    20  		"foo":                                  "request body contains badly-formed JSON (at position 2)",
    21  		`{ "script": "123", "arguments": [] }`: "invalid script source encoding",
    22  		fmt.Sprintf(`{ "script": "%s", "arguments": [123] }`, validBodyEncoded): `request body contains an invalid value for the "arguments" field (at position 57)`,
    23  	}
    24  
    25  	for in, errOut := range test {
    26  		body := strings.NewReader(in)
    27  		var script Script
    28  		err := script.Parse(body)
    29  		assert.EqualError(t, err, errOut, in)
    30  	}
    31  }
    32  
    33  func TestScript_ValidParse(t *testing.T) {
    34  	arg1 := []byte(`{"type": "String", "value": "hello" }`)
    35  	body := strings.NewReader(fmt.Sprintf(
    36  		`{ "script": "%s", "arguments": ["%s"] }`,
    37  		validBodyEncoded,
    38  		util.ToBase64(arg1),
    39  	))
    40  
    41  	var script Script
    42  	err := script.Parse(body)
    43  
    44  	assert.NoError(t, err)
    45  	assert.Equal(t, 1, len(script.Args))
    46  	assert.Equal(t, arg1, script.Args[0])
    47  	assert.Equal(t, validBody, string(script.Source))
    48  }