You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

7 years ago
7 years ago
7 years ago
  1. package rpctypes
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "fmt"
  6. "github.com/pkg/errors"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/tendermint/go-amino"
  9. )
  10. type SampleResult struct {
  11. Value string
  12. }
  13. func TestResponses(t *testing.T) {
  14. assert := assert.New(t)
  15. cdc := amino.NewCodec()
  16. a := NewRPCSuccessResponse(cdc, "1", &SampleResult{"hello"})
  17. b, _ := json.Marshal(a)
  18. s := `{"jsonrpc":"2.0","id":"1","result":{"Value":"hello"}}`
  19. assert.Equal(string(s), string(b))
  20. d := RPCParseError("1", errors.New("Hello world"))
  21. e, _ := json.Marshal(d)
  22. f := `{"jsonrpc":"2.0","id":"1","error":{"code":-32700,"message":"Parse error. Invalid JSON","data":"Hello world"}}`
  23. assert.Equal(string(f), string(e))
  24. g := RPCMethodNotFoundError("2")
  25. h, _ := json.Marshal(g)
  26. i := `{"jsonrpc":"2.0","id":"2","error":{"code":-32601,"message":"Method not found"}}`
  27. assert.Equal(string(h), string(i))
  28. }
  29. func TestRPCError(t *testing.T) {
  30. assert.Equal(t, "RPC error 12 - Badness: One worse than a code 11",
  31. fmt.Sprintf("%v", &RPCError{
  32. Code: 12,
  33. Message: "Badness",
  34. Data: "One worse than a code 11",
  35. }))
  36. assert.Equal(t, "RPC error 12 - Badness",
  37. fmt.Sprintf("%v", &RPCError{
  38. Code: 12,
  39. Message: "Badness",
  40. }))
  41. }