Example #1
0
func TestService(t *testing.T) {
	s := rpc.NewServer()
	s.RegisterCodec(NewCodec(), "application/json")
	s.RegisterService(new(Service1), "")

	var res Service1Response
	if _, err := execute(t, s, "Service1.Multiply", &Service1Request{4, 2}, &res); err != nil {
		t.Error("Expected err to be nil, but got:", err)
	}
	if res.Result != 8 {
		t.Error("Expected res.Result to be 8, but got:", res.Result)
	}
	if res.ErrorMessage != "" {
		t.Error("Expected error_message to be empty, but got:", res.ErrorMessage)
	}
	if code, err := execute(t, s, "Service1.ResponseError", &Service1Request{4, 2}, &res); err != nil || code != 500 {
		t.Errorf("Expected code to be 500 and error to be nil, but got %v (%v)", code, err)
	}
	if res.ErrorMessage == "" {
		t.Errorf("Expected error_message to be %q, but got %q", ErrResponseError, res.ErrorMessage)
	}
	if code, _ := execute(t, s, "Service1.Multiply", nil, &res); code != 400 {
		t.Error("Expected http response code 400, but got", code)
	}
}
Example #2
0
func TestServiceBeforeAfter(t *testing.T) {
	s := rpc.NewServer()
	s.RegisterCodec(NewCodec(), "application/json")
	service := &Service1{}
	service.beforeAfterContext = map[string]string{}
	s.RegisterService(service, "")

	s.RegisterBeforeFunc(func(i *rpc.RequestInfo) {
		service.beforeAfterContext["before"] = "Before is true"
	})
	s.RegisterAfterFunc(func(i *rpc.RequestInfo) {
		service.beforeAfterContext["after"] = "After is true"
	})

	var res Service1Response
	if err := execute(t, s, "Service1.BeforeAfter", &Service1Request{}, &res); err != nil {
		t.Error("Expected err to be nil, but got:", err)
	}

	if res.Result != 1 {
		t.Errorf("Expected Result = 1, got %d", res.Result)
	}

	if afterValue, ok := service.beforeAfterContext["after"]; !ok || afterValue != "After is true" {
		t.Errorf("Expected after in context to be 'After is true', got %s", afterValue)
	}
}
Example #3
0
func TestService(t *testing.T) {
	s := rpc.NewServer()
	s.RegisterCodec(NewCodec(), "application/json")
	s.RegisterService(new(Service1), "")

	var res Service1Response
	if err := execute(t, s, "Service1.Multiply", &Service1Request{4, 2}, &res); err != nil {
		t.Error("Expected err to be 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)
	}

	if code := executeRaw(t, s, &Service1BadRequest{"Service1.Multiply"}, &res); code != 400 {
		t.Errorf("Expected http response code 400, but got %v", code)
	}
}