good morning!!!!

Skip to content
Snippets Groups Projects
Commit 9f9adb92 authored by Garet Halliday's avatar Garet Halliday
Browse files

fix test

parent 4a53fa35
Branches
Tags
1 merge request!34fix invalid version / request
Pipeline #33072 passed
...@@ -12,9 +12,10 @@ import ( ...@@ -12,9 +12,10 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"golang.org/x/net/http2"
"gfx.cafe/open/jrpc/pkg/jjson" "gfx.cafe/open/jrpc/pkg/jjson"
"gfx.cafe/open/jrpc/pkg/jsonrpc" "gfx.cafe/open/jrpc/pkg/jsonrpc"
"golang.org/x/net/http2"
) )
var ( var (
......
...@@ -172,8 +172,10 @@ func (c *clientSub) Unsubscribe() error { ...@@ -172,8 +172,10 @@ func (c *clientSub) Unsubscribe() error {
c.engine.mu.Unlock() c.engine.mu.Unlock()
// TODO: dont use context background here... // TODO: dont use context background here...
var result string var result bool
err := c.conn.Do(context.Background(), &result, c.namespace+serviceMethodSeparator+unsubscribeMethodSuffix, nil) err := c.conn.Do(context.Background(), &result, c.namespace+serviceMethodSeparator+unsubscribeMethodSuffix, []string{
c.id,
})
if err != nil { if err != nil {
return err return err
} }
......
...@@ -2,7 +2,6 @@ package subscription ...@@ -2,7 +2,6 @@ package subscription
import ( import (
"context" "context"
"net/http"
"net/http/httptest" "net/http/httptest"
_ "net/http/pprof" _ "net/http/pprof"
"strings" "strings"
...@@ -102,12 +101,6 @@ func TestUnsubscribeNoRead(t *testing.T) { ...@@ -102,12 +101,6 @@ func TestUnsubscribeNoRead(t *testing.T) {
} }
func TestWrapClient(t *testing.T) { func TestWrapClient(t *testing.T) {
go func() {
if err := http.ListenAndServe(":6060", nil); err != nil {
panic(err)
}
}()
engine := NewEngine() engine := NewEngine()
r := jmux.NewRouter() r := jmux.NewRouter()
r.Use(engine.Middleware()) r.Use(engine.Middleware())
......
...@@ -62,7 +62,7 @@ func (e *Engine) Middleware() func(jsonrpc.Handler) jsonrpc.Handler { ...@@ -62,7 +62,7 @@ func (e *Engine) Middleware() func(jsonrpc.Handler) jsonrpc.Handler {
h.ServeRPC(w, r) h.ServeRPC(w, r)
case strings.HasSuffix(r.Method, serviceMethodSeparator+unsubscribeMethodSuffix): case strings.HasSuffix(r.Method, serviceMethodSeparator+unsubscribeMethodSuffix):
// read the subscription id to close // read the subscription id to close
resp := []SubID{} var resp []SubID
err := json.Unmarshal(r.Params, &resp) err := json.Unmarshal(r.Params, &resp)
if err != nil { if err != nil {
w.Send(false, err) w.Send(false, err)
......
...@@ -3,6 +3,8 @@ package jsonrpc ...@@ -3,6 +3,8 @@ package jsonrpc
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"github.com/go-faster/jx"
) )
// http.ResponseWriter interface, but for jrpc // http.ResponseWriter interface, but for jrpc
...@@ -39,10 +41,13 @@ func NewRawRequest(ctx context.Context, id *ID, method string, params json.RawMe ...@@ -39,10 +41,13 @@ func NewRawRequest(ctx context.Context, id *ID, method string, params json.RawMe
// NewRequest makes a new request // NewRequest makes a new request
func NewRequest(ctx context.Context, id *ID, method string, params any) (r *Request, err error) { func NewRequest(ctx context.Context, id *ID, method string, params any) (r *Request, err error) {
raw, err := json.Marshal(params) var raw json.RawMessage
if params != nil {
raw, err = json.Marshal(params)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
return NewRawRequest(ctx, id, method, raw), nil return NewRawRequest(ctx, id, method, raw), nil
} }
...@@ -64,3 +69,24 @@ func (r *Request) WithContext(ctx context.Context) *Request { ...@@ -64,3 +69,24 @@ func (r *Request) WithContext(ctx context.Context) *Request {
r2.Peer = r.Peer r2.Peer = r.Peer
return r2 return r2
} }
func (r Request) MarshalJSON() ([]byte, error) {
enc := jx.GetEncoder()
enc.Obj(func(e *jx.Encoder) {
e.FieldStart("jsonrpc")
e.Str(VersionString)
if r.ID != nil {
e.FieldStart("id")
e.Raw(*r.ID)
}
if r.Method != "" {
e.FieldStart("method")
e.Str(r.Method)
}
if r.Params != nil {
e.FieldStart("params")
e.Raw(r.Params)
}
})
return enc.Bytes(), nil
}
...@@ -6,9 +6,10 @@ import ( ...@@ -6,9 +6,10 @@ import (
"errors" "errors"
"sync" "sync"
"github.com/mailgun/multibuf"
"gfx.cafe/open/jrpc/pkg/jjson" "gfx.cafe/open/jrpc/pkg/jjson"
"gfx.cafe/open/jrpc/pkg/jsonrpc" "gfx.cafe/open/jrpc/pkg/jsonrpc"
"github.com/mailgun/multibuf"
) )
// Server is an RPC server. // Server is an RPC server.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment