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.

112 lines
3.1 KiB

  1. package jsonrpc
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/intel-go/fastjson"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestParseRequest(t *testing.T) {
  12. r, _ := http.NewRequest("", "", bytes.NewReader(nil))
  13. _, _, err := ParseRequest(r)
  14. require.IsType(t, &Error{}, err)
  15. assert.Equal(t, ErrorCodeInvalidRequest, err.Code)
  16. r.Header.Set("Content-Type", "application/json")
  17. _, _, err = ParseRequest(r)
  18. require.IsType(t, &Error{}, err)
  19. assert.Equal(t, ErrorCodeInvalidRequest, err.Code)
  20. r, _ = http.NewRequest("", "", bytes.NewReader([]byte("")))
  21. r.Header.Set("Content-Type", "application/json")
  22. _, _, err = ParseRequest(r)
  23. require.IsType(t, &Error{}, err)
  24. assert.Equal(t, ErrorCodeInvalidRequest, err.Code)
  25. r, _ = http.NewRequest("", "", bytes.NewReader([]byte("test")))
  26. r.Header.Set("Content-Type", "application/json")
  27. _, _, err = ParseRequest(r)
  28. require.IsType(t, &Error{}, err)
  29. assert.Equal(t, ErrorCodeParse, err.Code)
  30. r, _ = http.NewRequest("", "", bytes.NewReader([]byte("{}")))
  31. r.Header.Set("Content-Type", "application/json")
  32. rs, batch, err := ParseRequest(r)
  33. require.Nil(t, err)
  34. require.NotEmpty(t, rs)
  35. assert.False(t, batch)
  36. r, _ = http.NewRequest("", "", bytes.NewReader([]byte("[")))
  37. r.Header.Set("Content-Type", "application/json")
  38. _, _, err = ParseRequest(r)
  39. require.IsType(t, &Error{}, err)
  40. assert.Equal(t, ErrorCodeParse, err.Code)
  41. r, _ = http.NewRequest("", "", bytes.NewReader([]byte("[test]")))
  42. r.Header.Set("Content-Type", "application/json")
  43. _, _, err = ParseRequest(r)
  44. require.IsType(t, &Error{}, err)
  45. assert.Equal(t, ErrorCodeParse, err.Code)
  46. r, _ = http.NewRequest("", "", bytes.NewReader([]byte("[{}]")))
  47. r.Header.Set("Content-Type", "application/json")
  48. rs, batch, err = ParseRequest(r)
  49. require.Nil(t, err)
  50. require.NotEmpty(t, rs)
  51. assert.True(t, batch)
  52. }
  53. func TestNewResponse(t *testing.T) {
  54. id := fastjson.RawMessage("test")
  55. r := NewResponse(&Request{
  56. Version: "2.0",
  57. ID: &id,
  58. })
  59. assert.Equal(t, "2.0", r.Version)
  60. assert.Equal(t, "test", string(*r.ID))
  61. }
  62. func TestSendResponse(t *testing.T) {
  63. rec := httptest.NewRecorder()
  64. err := SendResponse(rec, []*Response{}, false)
  65. require.NoError(t, err)
  66. assert.Empty(t, rec.Body.String())
  67. id := fastjson.RawMessage([]byte(`"test"`))
  68. r := &Response{
  69. ID: &id,
  70. Version: "2.0",
  71. Result: struct {
  72. Name string `json:"name"`
  73. }{
  74. Name: "john",
  75. },
  76. }
  77. rec = httptest.NewRecorder()
  78. err = SendResponse(rec, []*Response{r}, false)
  79. require.NoError(t, err)
  80. assert.Equal(t, `{"id":"test","jsonrpc":"2.0","result":{"name":"john"}}
  81. `, rec.Body.String())
  82. rec = httptest.NewRecorder()
  83. err = SendResponse(rec, []*Response{r}, true)
  84. require.NoError(t, err)
  85. assert.Equal(t, `[{"id":"test","jsonrpc":"2.0","result":{"name":"john"}}]
  86. `, rec.Body.String())
  87. rec = httptest.NewRecorder()
  88. err = SendResponse(rec, []*Response{r, r}, false)
  89. require.NoError(t, err)
  90. assert.Equal(t, `[{"id":"test","jsonrpc":"2.0","result":{"name":"john"}},{"id":"test","jsonrpc":"2.0","result":{"name":"john"}}]
  91. `, rec.Body.String())
  92. }