23 | 23 |
return strings.NewReader(in)
|
24 | 24 |
}
|
25 | 25 |
|
|
26 |
func unmarshalResponse(body []byte) (resp jsonrpc.Response, err error) {
|
|
27 |
err = json.Unmarshal(body, &resp)
|
|
28 |
return
|
|
29 |
}
|
|
30 |
|
26 | 31 |
func expectErrorCode(t *testing.T, want int, body []byte) {
|
27 | |
var r jsonrpc.Response
|
28 | |
err := json.Unmarshal(body, &r)
|
29 | |
if err != nil {
|
30 | |
t.Fatalf("Cant' decode response. err=%s, body=%s", err, body)
|
|
32 |
t.Helper()
|
|
33 |
|
|
34 |
r, err := unmarshalResponse(body)
|
|
35 |
if err != nil {
|
|
36 |
t.Fatalf("Can't decode response: %v (%s)", err, body)
|
31 | 37 |
}
|
32 | 38 |
if r.Error == nil {
|
33 | 39 |
t.Fatalf("Expected error on response. Got none: %s", body)
|
34 | 40 |
}
|
35 | 41 |
if have := r.Error.Code; want != have {
|
36 | 42 |
t.Fatalf("Unexpected error code. Want %d, have %d: %s", want, have, body)
|
|
43 |
}
|
|
44 |
}
|
|
45 |
|
|
46 |
func expectValidRequestID(t *testing.T, want int, body []byte) {
|
|
47 |
t.Helper()
|
|
48 |
|
|
49 |
r, err := unmarshalResponse(body)
|
|
50 |
if err != nil {
|
|
51 |
t.Fatalf("Can't decode response: %v (%s)", err, body)
|
|
52 |
}
|
|
53 |
have, err := r.ID.Int()
|
|
54 |
if err != nil {
|
|
55 |
t.Fatalf("Can't get requestID in response. err=%s, body=%s", err, body)
|
|
56 |
}
|
|
57 |
if want != have {
|
|
58 |
t.Fatalf("Request ID: want %d, have %d (%s)", want, have, body)
|
|
59 |
}
|
|
60 |
}
|
|
61 |
|
|
62 |
func expectNilRequestID(t *testing.T, body []byte) {
|
|
63 |
t.Helper()
|
|
64 |
|
|
65 |
r, err := unmarshalResponse(body)
|
|
66 |
if err != nil {
|
|
67 |
t.Fatalf("Can't decode response: %v (%s)", err, body)
|
|
68 |
}
|
|
69 |
if r.ID != nil {
|
|
70 |
t.Fatalf("Request ID: want nil, have %v", r.ID)
|
37 | 71 |
}
|
38 | 72 |
}
|
39 | 73 |
|
|
91 | 125 |
}
|
92 | 126 |
buf, _ := ioutil.ReadAll(resp.Body)
|
93 | 127 |
expectErrorCode(t, jsonrpc.InternalError, buf)
|
|
128 |
expectValidRequestID(t, 1, buf)
|
94 | 129 |
}
|
95 | 130 |
|
96 | 131 |
func TestServerBadEncode(t *testing.T) {
|
|
110 | 145 |
}
|
111 | 146 |
buf, _ := ioutil.ReadAll(resp.Body)
|
112 | 147 |
expectErrorCode(t, jsonrpc.InternalError, buf)
|
|
148 |
expectValidRequestID(t, 1, buf)
|
113 | 149 |
}
|
114 | 150 |
|
115 | 151 |
func TestServerErrorEncoder(t *testing.T) {
|
|
161 | 197 |
}
|
162 | 198 |
buf, _ := ioutil.ReadAll(resp.Body)
|
163 | 199 |
expectErrorCode(t, jsonrpc.ParseError, buf)
|
|
200 |
expectNilRequestID(t, buf)
|
164 | 201 |
}
|
165 | 202 |
|
166 | 203 |
func TestServerUnregisteredMethod(t *testing.T) {
|
|
185 | 222 |
if want, have := http.StatusOK, resp.StatusCode; want != have {
|
186 | 223 |
t.Errorf("want %d, have %d (%s)", want, have, buf)
|
187 | 224 |
}
|
188 | |
var r jsonrpc.Response
|
189 | |
err := json.Unmarshal(buf, &r)
|
190 | |
if err != nil {
|
191 | |
t.Fatalf("Cant' decode response. err=%s, body=%s", err, buf)
|
|
225 |
r, err := unmarshalResponse(buf)
|
|
226 |
if err != nil {
|
|
227 |
t.Fatalf("Can't decode response. err=%s, body=%s", err, buf)
|
192 | 228 |
}
|
193 | 229 |
if r.JSONRPC != jsonrpc.Version {
|
194 | 230 |
t.Fatalf("JSONRPC Version: want=%s, got=%s", jsonrpc.Version, r.JSONRPC)
|