(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestJSONRPCSingleEndpoint(t *testing.T) { |
| 14 | t.Run("service-level JSONRPC", func(t *testing.T) { |
| 15 | root := RunJSONRPCDSL(t, func() { |
| 16 | dsl.Service("calc", func() { |
| 17 | dsl.JSONRPC(func() { |
| 18 | dsl.POST("/rpc") |
| 19 | }) |
| 20 | |
| 21 | dsl.Method("add", func() { |
| 22 | dsl.Payload(func() { |
| 23 | dsl.ID("id") |
| 24 | dsl.Attribute("a", dsl.Int) |
| 25 | dsl.Attribute("b", dsl.Int) |
| 26 | }) |
| 27 | dsl.Result(func() { |
| 28 | dsl.ID("id") |
| 29 | dsl.Attribute("sum", dsl.Int) |
| 30 | }) |
| 31 | dsl.JSONRPC(func() {}) |
| 32 | }) |
| 33 | |
| 34 | dsl.Method("subtract", func() { |
| 35 | dsl.Payload(func() { |
| 36 | dsl.ID("id") |
| 37 | dsl.Attribute("a", dsl.Int) |
| 38 | dsl.Attribute("b", dsl.Int) |
| 39 | }) |
| 40 | dsl.Result(func() { |
| 41 | dsl.ID("id") |
| 42 | dsl.Attribute("diff", dsl.Int) |
| 43 | }) |
| 44 | dsl.JSONRPC(func() {}) |
| 45 | }) |
| 46 | }) |
| 47 | }) |
| 48 | |
| 49 | // Check service is marked as JSON-RPC |
| 50 | svc := root.Service("calc") |
| 51 | require.NotNil(t, svc) |
| 52 | require.NotNil(t, svc.Meta) |
| 53 | assert.NotNil(t, svc.Meta["jsonrpc:service"], "service should be marked as JSON-RPC") |
| 54 | |
| 55 | // Check HTTP service |
| 56 | httpSvc := root.API.JSONRPC.Service("calc") |
| 57 | require.NotNil(t, httpSvc) |
| 58 | |
| 59 | // Prepare the service to create routes |
| 60 | httpSvc.Prepare() |
| 61 | |
| 62 | // Check that all JSON-RPC endpoints have exactly one route with the same path |
| 63 | var firstRoute *expr.RouteExpr |
| 64 | jsonrpcEndpointCount := 0 |
| 65 | for _, e := range httpSvc.HTTPEndpoints { |
| 66 | if e.IsJSONRPC() { |
| 67 | jsonrpcEndpointCount++ |
| 68 | assert.Equal(t, 1, len(e.Routes), "each JSON-RPC endpoint should have exactly one route") |
| 69 | if len(e.Routes) > 0 { |
| 70 | if firstRoute == nil { |
nothing calls this directly
no test coverage detected