Example #1
0
// Call a remote method with given parameters, waits service to respond.
func (c *Client) Call(method string, params ...interface{}) (interface{}, error) {
	req := protocol.Request{
		Method: method,
		Params: utils.ParamsFormat(params...),
	}
	resp, err := c.Send(&req)
	if err != nil {
		return nil, err
	}
	if resp.Error != nil {
		return nil, resp.Error
	}
	return resp.Body, nil
}
Example #2
0
func TestRemoteError(t *testing.T) {
	handler := newStructHandler(&EchoService{})
	req := protocol.Request{
		Method: "fail",
		Params: utils.ParamsFormat("foobar"),
	}
	resp := &dummy.ResponseWriter{}

	handler.Handle(resp, &req)

	if resp.Error == nil || resp.Error.Error() != "Fail!" {
		t.Errorf("expected to fail with 'Fail!' got %s", resp.Error)
	}
}
Example #3
0
func TestUnknownMethod(t *testing.T) {
	handler := newStructHandler(&EchoService{})
	req := protocol.Request{
		Method: "blabla",
		Params: utils.ParamsFormat("foobar"),
	}
	resp := &dummy.ResponseWriter{}

	handler.Handle(resp, &req)

	if resp.Error != protocol.UnknownMethod {
		t.Errorf("Expected handle to fail with %s, got %s", protocol.UnknownMethod, resp.Error)
	}
}
Example #4
0
func TestWrongArgumentsMethod(t *testing.T) {
	handler := newStructHandler(&EchoService{})
	req := protocol.Request{
		Method: "echo",
		Params: utils.ParamsFormat("foobar", 1),
	}
	resp := &dummy.ResponseWriter{}

	handler.Handle(resp, &req)

	if resp.Error != protocol.ParamsError {
		t.Errorf("Expected handle to fail with %s, got %s", protocol.ParamsError, resp.Error)
	}
}
Example #5
0
func TestHandling(t *testing.T) {
	handler := newStructHandler(&EchoService{})
	req := protocol.Request{
		Method: "echo",
		Params: utils.ParamsFormat("foobar"),
	}
	resp := &dummy.ResponseWriter{}

	handler.Handle(resp, &req)

	ret, ok := resp.Data.(string)
	if !ok {
		t.Errorf("Return data is not string")
	}

	if ret != "foobar" {
		t.Errorf("Expected handler to return %s, got %s", "foobar", resp.Data)
	}
}