github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/request/get_script_test.go (about) 1 package request 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/koko1123/flow-go-1/engine/access/rest/util" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestGetScript_InvalidParse(t *testing.T) { 14 var getScript GetScript 15 16 validScript := fmt.Sprintf(`{ "script": "%s", "arguments": [] }`, util.ToBase64([]byte(`pub fun main() {}`))) 17 tests := []struct { 18 height string 19 id string 20 script string 21 err string 22 }{ 23 {"", "", "", "request body must not be empty"}, 24 {"1", "7bc42fe85d32ca513769a74f97f7e1a7bad6c9407f0d934c2aa645ef9cf613c7", validScript, "can not provide both block ID and block height"}, 25 {"final", "7bc42fe85d32ca513769a74f97f7e1a7bad6c9407f0d934c2aa645ef9cf613c7", validScript, "can not provide both block ID and block height"}, 26 {"", "2", validScript, "invalid ID format"}, 27 {"1", "", `{ "foo": "zoo" }`, `request body contains unknown field "foo"`}, 28 } 29 30 for i, test := range tests { 31 err := getScript.Parse(test.height, test.id, strings.NewReader(test.script)) 32 assert.EqualError(t, err, test.err, fmt.Sprintf("test #%d failed", i)) 33 } 34 } 35 36 func TestGetScript_ValidParse(t *testing.T) { 37 var getScript GetScript 38 39 source := "pub fun main() {}" 40 validScript := strings.NewReader(fmt.Sprintf(`{ "script": "%s", "arguments": [] }`, util.ToBase64([]byte(source)))) 41 42 err := getScript.Parse("1", "", validScript) 43 assert.NoError(t, err) 44 assert.Equal(t, getScript.BlockHeight, uint64(1)) 45 assert.Equal(t, string(getScript.Script.Source), source) 46 47 validScript1 := strings.NewReader(fmt.Sprintf(`{ "script": "%s", "arguments": [] }`, util.ToBase64([]byte(source)))) 48 err = getScript.Parse("", "", validScript1) 49 assert.NoError(t, err) 50 assert.Equal(t, getScript.BlockHeight, SealedHeight) 51 }