-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
example_test.go
137 lines (109 loc) · 2.86 KB
/
example_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package jsonrpc_test
import (
"bytes"
"context"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"github.com/goccy/go-json"
"github.com/osamingo/jsonrpc/v2"
)
type (
EchoHandler struct{}
EchoParams struct {
Name string `json:"name"`
}
EchoResult struct {
Message string `json:"message"`
}
PositionalHandler struct{}
PositionalParams []int
PositionalResult struct {
Message []int `json:"message"`
}
)
func (h EchoHandler) ServeJSONRPC(_ context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) {
var p EchoParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
return EchoResult{
Message: "Hello, " + p.Name,
}, nil
}
func (h PositionalHandler) ServeJSONRPC(_ context.Context, params *json.RawMessage) (interface{}, *jsonrpc.Error) {
var p PositionalParams
if err := jsonrpc.Unmarshal(params, &p); err != nil {
return nil, err
}
return PositionalResult{
Message: p,
}, nil
}
func ExampleMethodRepository_ServeHTTP() { //nolint: nosnakecase
mr := jsonrpc.NewMethodRepository()
if err := mr.RegisterMethod("Main.Echo", EchoHandler{}, EchoParams{}, EchoResult{}); err != nil {
log.Println(err)
return
}
if err := mr.RegisterMethod("Main.Positional", PositionalHandler{}, PositionalParams{}, PositionalResult{}); err != nil {
log.Println(err)
return
}
http.Handle("/jrpc", mr)
http.HandleFunc("/jrpc/debug", mr.ServeDebug)
srv := httptest.NewServer(http.DefaultServeMux)
defer srv.Close()
contextType := "application/json"
echoVal := `{
"jsonrpc": "2.0",
"method": "Main.Echo",
"params": {
"name": "John Doe"
},
"id": "243a718a-2ebb-4e32-8cc8-210c39e8a14b"
}`
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/jrpc", bytes.NewBufferString(echoVal))
if err != nil {
log.Println(err)
return
}
req.Header.Add("Content-Type", contextType)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
log.Println(err)
return
}
positionalVal := `{
"jsonrpc": "2.0",
"method": "Main.Positional",
"params": [3, 1, 1, 3, 5, 3],
"id": "243a718a-2ebb-4e32-8cc8-210c39e8a14b"
}`
req, err = http.NewRequestWithContext(context.Background(), http.MethodPost, srv.URL+"/jrpc", bytes.NewBufferString(positionalVal))
if err != nil {
log.Println(err)
return
}
req.Header.Add("Content-Type", contextType)
resp, err = http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
log.Println(err)
return
}
// Output:
// {"jsonrpc":"2.0","result":{"message":"Hello, John Doe"},"id":"243a718a-2ebb-4e32-8cc8-210c39e8a14b"}
// {"jsonrpc":"2.0","result":{"message":[3,1,1,3,5,3]},"id":"243a718a-2ebb-4e32-8cc8-210c39e8a14b"}
}