Skip to content

Commit

Permalink
optimize: grpc methodName & http router (dapr#289)
Browse files Browse the repository at this point in the history
* fix/typo: modify state  annotation

Signed-off-by: 1046102779 <[email protected]>

* bugfix&optimize: grpc router bug&http invalid router

Signed-off-by: 1046102779 <[email protected]>

* bugfix&optimize: grpc router bug&http invalid router

Signed-off-by: 1046102779 <[email protected]>
  • Loading branch information
1046102779 authored Jun 10, 2022
1 parent d204f69 commit a1648db
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
7 changes: 6 additions & 1 deletion service/grpc/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ import (

// AddServiceInvocationHandler appends provided service invocation handler with its method to the service.
func (s *Server) AddServiceInvocationHandler(method string, fn cc.ServiceInvocationHandler) error {
if method == "" {
if method == "" || method == "/" {
return fmt.Errorf("servie name required")
}

if method[0] == '/' {
method = method[1:]
}

if fn == nil {
return fmt.Errorf("invocation handler required")
}
Expand Down
6 changes: 5 additions & 1 deletion service/grpc/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func TestInvokeErrors(t *testing.T) {
server := getTestServer()
err := server.AddServiceInvocationHandler("", nil)
assert.Error(t, err)

err = server.AddServiceInvocationHandler("/", nil)
assert.Error(t, err)

err = server.AddServiceInvocationHandler("test", nil)
assert.Error(t, err)
}
Expand Down Expand Up @@ -90,7 +94,7 @@ func TestInvoke(t *testing.T) {
ctx := context.Background()

server := getTestServer()
err := server.AddServiceInvocationHandler(methodName, testInvokeHandler)
err := server.AddServiceInvocationHandler("/"+methodName, testInvokeHandler)
assert.Nil(t, err)

err = server.AddServiceInvocationHandler(methodNameWithError, testInvokeHandlerWithError)
Expand Down
3 changes: 2 additions & 1 deletion service/http/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (

// AddServiceInvocationHandler appends provided service invocation handler with its route to the service.
func (s *Server) AddServiceInvocationHandler(route string, fn common.ServiceInvocationHandler) error {
if route == "" {
if route == "" || route == "/" {
return fmt.Errorf("service route required")
}

if fn == nil {
return fmt.Errorf("invocation handler required")
}
Expand Down
43 changes: 23 additions & 20 deletions service/http/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ import (

func TestInvocationHandlerWithoutHandler(t *testing.T) {
s := newServer("", nil)
err := s.AddServiceInvocationHandler("/", nil)
err := s.AddServiceInvocationHandler("/hello", nil)
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) {
data := `{"name": "test", "data": hellow}`
data := `{"name": "test", "data": hello}`
_ = os.Setenv(common.AppAPITokenEnvVar, "app-dapr-token")
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 == "" {
err = errors.New("nil input")
return
Expand All @@ -50,11 +53,11 @@ func TestInvocationHandlerWithToken(t *testing.T) {
}
return
})
assert.NoErrorf(t, err, "error adding event handler")
assert.NoErrorf(t, err, "adding event handler success")

// forbbiden.
req, err := http.NewRequest(http.MethodPost, "/", strings.NewReader(data))
assert.NoErrorf(t, err, "error creating request")
req, err := http.NewRequest(http.MethodPost, "/hello", strings.NewReader(data))
assert.NoErrorf(t, err, "creating request success")
req.Header.Set("Content-Type", "application/json")

resp := httptest.NewRecorder()
Expand All @@ -70,9 +73,9 @@ func TestInvocationHandlerWithToken(t *testing.T) {
}

func TestInvocationHandlerWithData(t *testing.T) {
data := `{"name": "test", "data": hellow}`
data := `{"name": "test", "data": hello}`
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 == "" {
err = errors.New("nil input")
return
Expand All @@ -84,42 +87,42 @@ func TestInvocationHandlerWithData(t *testing.T) {
}
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))
assert.NoErrorf(t, err, "error creating request")
req, err := http.NewRequest(http.MethodPost, "/hello", strings.NewReader(data))
assert.NoErrorf(t, err, "creating request success")
req.Header.Set("Content-Type", "application/json")

resp := httptest.NewRecorder()
s.mux.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)

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))
}

func TestInvocationHandlerWithoutInputData(t *testing.T) {
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 {
err = errors.New("nil input")
return
}
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)
assert.NoErrorf(t, err, "error creating request")
req, err := http.NewRequest(http.MethodPost, "/hello", nil)
assert.NoErrorf(t, err, "creating request success")
req.Header.Set("Content-Type", "application/json")

resp := httptest.NewRecorder()
s.mux.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)

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.Equal(t, "", string(b))
}
Expand All @@ -132,13 +135,13 @@ func TestInvocationHandlerWithInvalidRoute(t *testing.T) {
s := newServer("", nil)

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)
assert.Errorf(t, err, "expected error from adding no route event handler")

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)
}
Expand All @@ -151,7 +154,7 @@ func TestInvocationHandlerWithError(t *testing.T) {
s := newServer("", nil)

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)
}

0 comments on commit a1648db

Please sign in to comment.