Golang RPC library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

189 lines
4.1 KiB

package rpc
import (
"errors"
"net/http"
"net/http/httptest"
)
// ----------------------------------------------------------------------------
func NewHTTPServerForTesting() (*Client, func()) {
mux := http.NewServeMux()
mux.Handle("/rpc/", NewHandlerFunc(&TestServer{}, ""))
srv := httptest.NewServer(mux)
cl := NewClient(
testCodec, NewHTTPTransport(srv.URL+"/rpc/"+testCodec.ID(), 0))
return cl, func() {
srv.Close()
}
}
// ----------------------------------------------------------------------------
type TestSession struct {
UserID int
}
type TestServer struct{}
func (ts *TestServer) RPCGetSession(token string) TestSession {
switch token {
case "1":
return TestSession{1}
case "2":
return TestSession{2}
case "3":
return TestSession{3}
case "4":
return TestSession{4}
default:
return TestSession{}
}
}
func (ts *TestServer) RPCIsAuthorized(s TestSession, fn string) error {
if fn == "SignIn" {
return nil
}
switch s.UserID {
case 1:
return nil
case 2:
return nil
case 3:
return errors.New("Unexpected!")
default:
return newError("NotAuthorized", "Not authorized!")
}
}
// ----------------------------------------------------------------------------
type TestSignInArgs struct {
Username string
Password string
}
func (ts *TestServer) SignIn(
s TestSession,
args *TestSignInArgs,
resp *string,
) error {
if args.Username == "user1" && args.Password == "pwd1" {
*resp = "1"
return nil
}
if args.Username == "user2" && args.Password == "pwd2" {
*resp = "2"
return nil
}
if args.Username == "user3" && args.Password == "pwd3" {
*resp = "3"
return nil
}
if args.Username == "user4" && args.Password == "pwd4" {
*resp = "4"
return nil
}
return newError("NotAuthorized", "Not authorized!")
}
// ----------------------------------------------------------------------------
func (ts *TestServer) GetUserID(
s TestSession,
args *struct{},
resp *int,
) error {
*resp = s.UserID
return nil
}
// ----------------------------------------------------------------------------
type TestAddArgs struct {
X float64
Y float64
}
func (ts *TestServer) Add(
s TestSession,
args *TestAddArgs,
resp *float64,
) error {
*resp = args.X + args.Y
return nil
}
// ----------------------------------------------------------------------------
type TestQuoArgs struct {
X, Y float64
}
func (ts *TestServer) Quo(s TestSession, args *TestQuoArgs, resp *float64) error {
if args.Y == 0 {
return newError("ErrDivideByZero", "Attempted division by zero")
}
*resp = args.X / args.Y
return nil
}
// ----------------------------------------------------------------------------
// Not exposed: less than four arguments (including receiver).
func (ts *TestServer) Bad0(
s TestSession, args, resp *float64, what int,
) error {
return nil
}
// ----------------------------------------------------------------------------
// Not exposed: First argument isn't a session.
func (ts *TestServer) Bad1(
x int, args *TestQuoArgs, resp *float64,
) error {
return nil
}
// ----------------------------------------------------------------------------
// Not exposed: Second argument not a pointer.
func (ts *TestServer) Bad2(s TestSession, args TestQuoArgs, resp *float64) error {
if args.Y == 0 {
return newError("ErrDivideByZero", "Attempted division by zero")
}
*resp = args.X / args.Y
return nil
}
// ----------------------------------------------------------------------------
// Not exposed: Third argument not a pointer.
func (ts *TestServer) Bad3(s TestSession, args *int, resp int) error {
return nil
}
// ----------------------------------------------------------------------------
// Not exposed: Multiple outs.
func (ts *TestServer) Bad4(s TestSession, args, resp *int) (int, error) {
return 1, nil
}
// ----------------------------------------------------------------------------
// Not exposed: Output not error.
func (ts *TestServer) Bad5(s TestSession, args, resp *int) int {
return 1
}
// ----------------------------------------------------------------------------
// Not exposed: no caps.
func (ts *TestServer) bad6(s TestSession, args, resp *int) int {
return 1
}