mirror of https://github.com/dapr/go-sdk.git
optimize: grpc methodName & http router (#289)
* fix/typo: modify state annotation Signed-off-by: 1046102779 <seachen@tencent.com> * bugfix&optimize: grpc router bug&http invalid router Signed-off-by: 1046102779 <seachen@tencent.com> * bugfix&optimize: grpc router bug&http invalid router Signed-off-by: 1046102779 <seachen@tencent.com>
This commit is contained in:
parent
d204f691ed
commit
a1648db4ab
|
@ -27,9 +27,14 @@ import (
|
||||||
|
|
||||||
// AddServiceInvocationHandler appends provided service invocation handler with its method to the service.
|
// AddServiceInvocationHandler appends provided service invocation handler with its method to the service.
|
||||||
func (s *Server) AddServiceInvocationHandler(method string, fn cc.ServiceInvocationHandler) error {
|
func (s *Server) AddServiceInvocationHandler(method string, fn cc.ServiceInvocationHandler) error {
|
||||||
if method == "" {
|
if method == "" || method == "/" {
|
||||||
return fmt.Errorf("servie name required")
|
return fmt.Errorf("servie name required")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if method[0] == '/' {
|
||||||
|
method = method[1:]
|
||||||
|
}
|
||||||
|
|
||||||
if fn == nil {
|
if fn == nil {
|
||||||
return fmt.Errorf("invocation handler required")
|
return fmt.Errorf("invocation handler required")
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,10 @@ func TestInvokeErrors(t *testing.T) {
|
||||||
server := getTestServer()
|
server := getTestServer()
|
||||||
err := server.AddServiceInvocationHandler("", nil)
|
err := server.AddServiceInvocationHandler("", nil)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
err = server.AddServiceInvocationHandler("/", nil)
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
err = server.AddServiceInvocationHandler("test", nil)
|
err = server.AddServiceInvocationHandler("test", nil)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
}
|
}
|
||||||
|
@ -90,7 +94,7 @@ func TestInvoke(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
server := getTestServer()
|
server := getTestServer()
|
||||||
err := server.AddServiceInvocationHandler(methodName, testInvokeHandler)
|
err := server.AddServiceInvocationHandler("/"+methodName, testInvokeHandler)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
err = server.AddServiceInvocationHandler(methodNameWithError, testInvokeHandlerWithError)
|
err = server.AddServiceInvocationHandler(methodNameWithError, testInvokeHandlerWithError)
|
||||||
|
|
|
@ -24,9 +24,10 @@ import (
|
||||||
|
|
||||||
// AddServiceInvocationHandler appends provided service invocation handler with its route to the service.
|
// AddServiceInvocationHandler appends provided service invocation handler with its route to the service.
|
||||||
func (s *Server) AddServiceInvocationHandler(route string, fn common.ServiceInvocationHandler) error {
|
func (s *Server) AddServiceInvocationHandler(route string, fn common.ServiceInvocationHandler) error {
|
||||||
if route == "" {
|
if route == "" || route == "/" {
|
||||||
return fmt.Errorf("service route required")
|
return fmt.Errorf("service route required")
|
||||||
}
|
}
|
||||||
|
|
||||||
if fn == nil {
|
if fn == nil {
|
||||||
return fmt.Errorf("invocation handler required")
|
return fmt.Errorf("invocation handler required")
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,15 +30,18 @@ import (
|
||||||
|
|
||||||
func TestInvocationHandlerWithoutHandler(t *testing.T) {
|
func TestInvocationHandlerWithoutHandler(t *testing.T) {
|
||||||
s := newServer("", nil)
|
s := newServer("", nil)
|
||||||
err := s.AddServiceInvocationHandler("/", nil)
|
err := s.AddServiceInvocationHandler("/hello", nil)
|
||||||
assert.Errorf(t, err, "expected error adding event handler")
|
assert.Errorf(t, err, "expected error adding event handler")
|
||||||
|
|
||||||
|
err = s.AddServiceInvocationHandler("/", nil)
|
||||||
|
assert.Errorf(t, err, "expected error adding event handler, invalid router")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvocationHandlerWithToken(t *testing.T) {
|
func TestInvocationHandlerWithToken(t *testing.T) {
|
||||||
data := `{"name": "test", "data": hellow}`
|
data := `{"name": "test", "data": hello}`
|
||||||
_ = os.Setenv(common.AppAPITokenEnvVar, "app-dapr-token")
|
_ = os.Setenv(common.AppAPITokenEnvVar, "app-dapr-token")
|
||||||
s := newServer("", nil)
|
s := newServer("", nil)
|
||||||
err := s.AddServiceInvocationHandler("/", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
|
err := s.AddServiceInvocationHandler("/hello", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
|
||||||
if in == nil || in.Data == nil || in.ContentType == "" {
|
if in == nil || in.Data == nil || in.ContentType == "" {
|
||||||
err = errors.New("nil input")
|
err = errors.New("nil input")
|
||||||
return
|
return
|
||||||
|
@ -50,11 +53,11 @@ func TestInvocationHandlerWithToken(t *testing.T) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
assert.NoErrorf(t, err, "error adding event handler")
|
assert.NoErrorf(t, err, "adding event handler success")
|
||||||
|
|
||||||
// forbbiden.
|
// forbbiden.
|
||||||
req, err := http.NewRequest(http.MethodPost, "/", strings.NewReader(data))
|
req, err := http.NewRequest(http.MethodPost, "/hello", strings.NewReader(data))
|
||||||
assert.NoErrorf(t, err, "error creating request")
|
assert.NoErrorf(t, err, "creating request success")
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
|
@ -70,9 +73,9 @@ func TestInvocationHandlerWithToken(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvocationHandlerWithData(t *testing.T) {
|
func TestInvocationHandlerWithData(t *testing.T) {
|
||||||
data := `{"name": "test", "data": hellow}`
|
data := `{"name": "test", "data": hello}`
|
||||||
s := newServer("", nil)
|
s := newServer("", nil)
|
||||||
err := s.AddServiceInvocationHandler("/", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
|
err := s.AddServiceInvocationHandler("/hello", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
|
||||||
if in == nil || in.Data == nil || in.ContentType == "" {
|
if in == nil || in.Data == nil || in.ContentType == "" {
|
||||||
err = errors.New("nil input")
|
err = errors.New("nil input")
|
||||||
return
|
return
|
||||||
|
@ -84,10 +87,10 @@ func TestInvocationHandlerWithData(t *testing.T) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
assert.NoErrorf(t, err, "error adding event handler")
|
assert.NoErrorf(t, err, "adding event handler success")
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, "/", strings.NewReader(data))
|
req, err := http.NewRequest(http.MethodPost, "/hello", strings.NewReader(data))
|
||||||
assert.NoErrorf(t, err, "error creating request")
|
assert.NoErrorf(t, err, "creating request success")
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
|
@ -95,23 +98,23 @@ func TestInvocationHandlerWithData(t *testing.T) {
|
||||||
assert.Equal(t, http.StatusOK, resp.Code)
|
assert.Equal(t, http.StatusOK, resp.Code)
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
b, err := ioutil.ReadAll(resp.Body)
|
||||||
assert.NoErrorf(t, err, "error reading response body")
|
assert.NoErrorf(t, err, "reading response body success")
|
||||||
assert.Equal(t, data, string(b))
|
assert.Equal(t, data, string(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvocationHandlerWithoutInputData(t *testing.T) {
|
func TestInvocationHandlerWithoutInputData(t *testing.T) {
|
||||||
s := newServer("", nil)
|
s := newServer("", nil)
|
||||||
err := s.AddServiceInvocationHandler("/", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
|
err := s.AddServiceInvocationHandler("/hello", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
|
||||||
if in == nil || in.Data != nil {
|
if in == nil || in.Data != nil {
|
||||||
err = errors.New("nil input")
|
err = errors.New("nil input")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return &common.Content{}, nil
|
return &common.Content{}, nil
|
||||||
})
|
})
|
||||||
assert.NoErrorf(t, err, "error adding event handler")
|
assert.NoErrorf(t, err, "adding event handler success")
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, "/", nil)
|
req, err := http.NewRequest(http.MethodPost, "/hello", nil)
|
||||||
assert.NoErrorf(t, err, "error creating request")
|
assert.NoErrorf(t, err, "creating request success")
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
resp := httptest.NewRecorder()
|
resp := httptest.NewRecorder()
|
||||||
|
@ -119,7 +122,7 @@ func TestInvocationHandlerWithoutInputData(t *testing.T) {
|
||||||
assert.Equal(t, http.StatusOK, resp.Code)
|
assert.Equal(t, http.StatusOK, resp.Code)
|
||||||
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
b, err := ioutil.ReadAll(resp.Body)
|
||||||
assert.NoErrorf(t, err, "error reading response body")
|
assert.NoErrorf(t, err, "reading response body success")
|
||||||
assert.NotNil(t, b)
|
assert.NotNil(t, b)
|
||||||
assert.Equal(t, "", string(b))
|
assert.Equal(t, "", string(b))
|
||||||
}
|
}
|
||||||
|
@ -132,13 +135,13 @@ func TestInvocationHandlerWithInvalidRoute(t *testing.T) {
|
||||||
s := newServer("", nil)
|
s := newServer("", nil)
|
||||||
|
|
||||||
err := s.AddServiceInvocationHandler("no-slash", emptyInvocationFn)
|
err := s.AddServiceInvocationHandler("no-slash", emptyInvocationFn)
|
||||||
assert.NoErrorf(t, err, "error adding no slash route event handler")
|
assert.NoErrorf(t, err, "adding no slash route event handler success")
|
||||||
|
|
||||||
err = s.AddServiceInvocationHandler("", emptyInvocationFn)
|
err = s.AddServiceInvocationHandler("", emptyInvocationFn)
|
||||||
assert.Errorf(t, err, "expected error from adding no route event handler")
|
assert.Errorf(t, err, "expected error from adding no route event handler")
|
||||||
|
|
||||||
err = s.AddServiceInvocationHandler("/a", emptyInvocationFn)
|
err = s.AddServiceInvocationHandler("/a", emptyInvocationFn)
|
||||||
assert.NoErrorf(t, err, "error adding event handler")
|
assert.NoErrorf(t, err, "adding event handler success")
|
||||||
|
|
||||||
makeEventRequest(t, s, "/b", "", http.StatusNotFound)
|
makeEventRequest(t, s, "/b", "", http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
@ -151,7 +154,7 @@ func TestInvocationHandlerWithError(t *testing.T) {
|
||||||
s := newServer("", nil)
|
s := newServer("", nil)
|
||||||
|
|
||||||
err := s.AddServiceInvocationHandler("/error", errorInvocationFn)
|
err := s.AddServiceInvocationHandler("/error", errorInvocationFn)
|
||||||
assert.NoErrorf(t, err, "error adding error event handler")
|
assert.NoErrorf(t, err, "adding error event handler success")
|
||||||
|
|
||||||
makeEventRequest(t, s, "/error", "", http.StatusInternalServerError)
|
makeEventRequest(t, s, "/error", "", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue