-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
73 lines (66 loc) · 2.18 KB
/
response_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package jsonrpc_test
import (
"encoding/json"
"testing"
"github.com/41north/jsonrpc.go"
"github.com/stretchr/testify/assert"
)
var responseTestCases = []struct {
value *jsonrpc.Response
json string
}{
{
newResponse("hello"),
"{\"result\":\"hello\",\"jsonrpc\":\"2.0\"}",
},
{
newResponse("world", jsonrpc.ResponseNumericId(1456)),
"{\"id\":1456,\"result\":\"world\",\"jsonrpc\":\"2.0\"}",
},
{
newResponse("foo", jsonrpc.ResponseStringId("resp-1")),
"{\"id\":\"resp-1\",\"result\":\"foo\",\"jsonrpc\":\"2.0\"}",
},
{
newResponse([]string{"hello", "world"}, jsonrpc.ResponseNumericId(1456), jsonrpc.ResponseVersion("1.0")),
"{\"id\":1456,\"result\":[\"hello\",\"world\"],\"jsonrpc\":\"1.0\"}",
},
{
newResponseError(jsonrpc.Error{Code: 123, Message: "some error"}),
"{\"error\":{\"code\":123,\"message\":\"some error\"},\"jsonrpc\":\"2.0\"}",
},
{
newResponseError(jsonrpc.Error{Code: 3421, Message: "another error"}, jsonrpc.ResponseNumericId(1456)),
"{\"id\":1456,\"error\":{\"code\":3421,\"message\":\"another error\"},\"jsonrpc\":\"2.0\"}",
},
{
newResponseError(jsonrpc.Error{Code: -123, Message: "some bug"}, jsonrpc.ResponseStringId("resp-1")),
"{\"id\":\"resp-1\",\"error\":{\"code\":-123,\"message\":\"some bug\"},\"jsonrpc\":\"2.0\"}",
},
{
newResponseError(jsonrpc.Error{Code: 552, Message: "sort your code"}, jsonrpc.ResponseNumericId(1456), jsonrpc.ResponseVersion("1.0")),
"{\"id\":1456,\"error\":{\"code\":552,\"message\":\"sort your code\"},\"jsonrpc\":\"1.0\"}",
},
}
func TestResponse_MarshalJSON(t *testing.T) {
for _, tc := range responseTestCases {
bytes, err := json.Marshal(tc.value)
assert.Nil(t, err, "failed to marshal to json")
assert.Equal(t, tc.json, string(bytes))
}
}
func TestResponse_UnmarshalJSON(t *testing.T) {
for _, tc := range responseTestCases {
var resp jsonrpc.Response
err := json.Unmarshal([]byte(tc.json), &resp)
assert.Nil(t, err, "failed to unmarshal from json")
assert.Equal(t, *tc.value, resp)
}
}
func newResponseError(error jsonrpc.Error, options ...jsonrpc.ResponseOption) *jsonrpc.Response {
resp, err := jsonrpc.NewResponseError(error, options...)
if err != nil {
panic(err)
}
return resp
}