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.

70 lines
1.5 KiB

  1. package jsonrpc
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/intel-go/fastjson"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestTakeMethod(t *testing.T) {
  10. mr := NewMethodRepository()
  11. r := &Request{}
  12. _, err := mr.TakeMethod(r)
  13. require.IsType(t, &Error{}, err)
  14. assert.Equal(t, ErrorCodeInvalidParams, err.Code)
  15. r.Method = "test"
  16. _, err = mr.TakeMethod(r)
  17. require.IsType(t, &Error{}, err)
  18. assert.Equal(t, ErrorCodeInvalidParams, err.Code)
  19. r.Version = "2.0"
  20. _, err = mr.TakeMethod(r)
  21. require.IsType(t, &Error{}, err)
  22. assert.Equal(t, ErrorCodeMethodNotFound, err.Code)
  23. require.NoError(t, mr.RegisterMethod("test", SampleHandler(), nil, nil))
  24. f, err := mr.TakeMethod(r)
  25. require.Nil(t, err)
  26. assert.NotEmpty(t, f)
  27. }
  28. func TestRegisterMethod(t *testing.T) {
  29. mr := NewMethodRepository()
  30. err := mr.RegisterMethod("", nil, nil, nil)
  31. require.Error(t, err)
  32. err = mr.RegisterMethod("test", nil, nil, nil)
  33. require.Error(t, err)
  34. err = mr.RegisterMethod("test", SampleHandler(), nil, nil)
  35. require.NoError(t, err)
  36. }
  37. func TestMethods(t *testing.T) {
  38. mr := NewMethodRepository()
  39. err := mr.RegisterMethod("JsonRpc.Sample", SampleHandler(), nil, nil)
  40. require.NoError(t, err)
  41. ml := mr.Methods()
  42. require.NotEmpty(t, ml)
  43. assert.NotEmpty(t, ml["JsonRpc.Sample"].Handler)
  44. }
  45. func SampleHandler() Handler {
  46. h := handler{}
  47. h.F = func(c context.Context, params *fastjson.RawMessage) (result interface{}, err *Error) {
  48. return nil, nil
  49. }
  50. return &h
  51. }