|
8 | 8 | "io" |
9 | 9 | "io/ioutil" |
10 | 10 | "net" |
| 11 | + "net/http" |
11 | 12 | "net/http/httptest" |
12 | 13 | "reflect" |
13 | 14 | "strconv" |
@@ -45,6 +46,12 @@ type TestOut struct { |
45 | 46 | Ok bool |
46 | 47 | } |
47 | 48 |
|
| 49 | +func (h *SimpleServerHandler) Inc() error { |
| 50 | + h.n++ |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
48 | 55 | func (h *SimpleServerHandler) Add(in int) error { |
49 | 56 | if in == -3546 { |
50 | 57 | return errors.New("test") |
@@ -72,6 +79,37 @@ func (h *SimpleServerHandler) StringMatch(t TestType, i2 int64) (out TestOut, er |
72 | 79 | return |
73 | 80 | } |
74 | 81 |
|
| 82 | +func TestRawRequests(t *testing.T) { |
| 83 | + rpcHandler := SimpleServerHandler{} |
| 84 | + |
| 85 | + rpcServer := NewServer() |
| 86 | + rpcServer.Register("SimpleServerHandler", &rpcHandler) |
| 87 | + |
| 88 | + testServ := httptest.NewServer(rpcServer) |
| 89 | + defer testServ.Close() |
| 90 | + |
| 91 | + tc := func(req, resp string, n int) func(t *testing.T) { |
| 92 | + return func(t *testing.T) { |
| 93 | + rpcHandler.n = 0 |
| 94 | + |
| 95 | + res, err := http.Post(testServ.URL, "application/json", strings.NewReader(req)) |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + b, err := ioutil.ReadAll(res.Body) |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + assert.Equal(t, resp, strings.TrimSpace(string(b))) |
| 102 | + require.Equal(t, n, rpcHandler.n) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + t.Run("inc", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": [], "id": 1}`, `{"jsonrpc":"2.0","id":1}`, 1)) |
| 107 | + t.Run("inc-null", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": null, "id": 1}`, `{"jsonrpc":"2.0","id":1}`, 1)) |
| 108 | + t.Run("inc-noparam", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "id": 2}`, `{"jsonrpc":"2.0","id":2}`, 1)) |
| 109 | + t.Run("add", tc(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Add", "params": [10], "id": 4}`, `{"jsonrpc":"2.0","id":4}`, 10)) |
| 110 | + |
| 111 | +} |
| 112 | + |
75 | 113 | func TestReconnection(t *testing.T) { |
76 | 114 | var rpcClient struct { |
77 | 115 | Add func(int) error |
|
0 commit comments