示例#1
0
func main() {
	s := httprpcjson.NewServer()
	s.RegisterCodec(json.NewCodec(), "application/json")
	s.RegisterService(new(Counter), "")
	http.Handle("/jsonrpc/", s)
	fmt.Println("listen 1234 port")
	http.ListenAndServe(":1234", nil)
}
示例#2
0
func TestService(t *testing.T) {
	s := httprpcjson.NewServer()
	s.RegisterCodec(NewCodec(), "application/json")
	s.RegisterService(new(Service1), "")

	var res Service1Response
	if err := execute(t, s, "Service1.Mul", &Service1Request{4, 2}, &res); err != nil {
		t.Errorf("Expected to get nil,but got", err)
	}

	if res.Result != 8 {
		t.Errorf("Wrong response:%v.", res.Result)
	}

	if err := execute(t, s, "Service1.ResponseError", &Service1Request{4, 2}, &res); err == nil {
		t.Errorf("Expected to get %q,but got nil", ErrResponseError)
	} else if err.Error() != ErrResponseError.Error() {
		t.Errorf("Expected to get %q,but got %q", ErrResponseError, err)
	}
}