mirror of https://github.com/dapr/go-sdk.git
* handler checks addressing issue #100 * handler check tests
This commit is contained in:
parent
0a93facbba
commit
b7a5b80a89
|
|
@ -15,6 +15,9 @@ func (s *Server) AddBindingInvocationHandler(name string, fn func(ctx context.Co
|
|||
if name == "" {
|
||||
return fmt.Errorf("binding name required")
|
||||
}
|
||||
if fn == nil {
|
||||
return fmt.Errorf("binding handler required")
|
||||
}
|
||||
s.bindingHandlers[name] = fn
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,9 @@ func (s *Server) AddServiceInvocationHandler(method string, fn func(ctx context.
|
|||
if method == "" {
|
||||
return fmt.Errorf("servie name required")
|
||||
}
|
||||
if fn == nil {
|
||||
return fmt.Errorf("invocation handler required")
|
||||
}
|
||||
s.invokeHandlers[method] = fn
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,9 @@ func (s *Server) AddTopicEventHandler(sub *common.Subscription, fn func(ctx cont
|
|||
if sub.PubsubName == "" {
|
||||
return errors.New("pub/sub name required")
|
||||
}
|
||||
if fn == nil {
|
||||
return fmt.Errorf("topic handler required")
|
||||
}
|
||||
key := fmt.Sprintf("%s-%s", sub.PubsubName, sub.Topic)
|
||||
s.topicSubscriptions[key] = &topicEventHandler{
|
||||
component: sub.PubsubName,
|
||||
|
|
|
|||
|
|
@ -15,6 +15,9 @@ func (s *Server) AddBindingInvocationHandler(route string, fn func(ctx context.C
|
|||
if route == "" {
|
||||
return fmt.Errorf("binding route required")
|
||||
}
|
||||
if fn == nil {
|
||||
return fmt.Errorf("binding handler required")
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(route, "/") {
|
||||
route = fmt.Sprintf("/%s", route)
|
||||
|
|
|
|||
|
|
@ -12,6 +12,12 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBindingHandlerWithoutHandler(t *testing.T) {
|
||||
s := newServer("", nil)
|
||||
err := s.AddBindingInvocationHandler("/", nil)
|
||||
assert.Errorf(t, err, "expected error adding nil binding event handler")
|
||||
}
|
||||
|
||||
func TestBindingHandlerWithoutData(t *testing.T) {
|
||||
s := newServer("", nil)
|
||||
err := s.AddBindingInvocationHandler("/", func(ctx context.Context, in *common.BindingEvent) (out []byte, err error) {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,9 @@ func (s *Server) AddServiceInvocationHandler(route string, fn func(ctx context.C
|
|||
if route == "" {
|
||||
return fmt.Errorf("service route required")
|
||||
}
|
||||
if fn == nil {
|
||||
return fmt.Errorf("invocation handler required")
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(route, "/") {
|
||||
route = fmt.Sprintf("/%s", route)
|
||||
|
|
|
|||
|
|
@ -14,6 +14,12 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestInvocationHandlerWithoutHandler(t *testing.T) {
|
||||
s := newServer("", nil)
|
||||
err := s.AddServiceInvocationHandler("/", nil)
|
||||
assert.Errorf(t, err, "expected error adding event handler")
|
||||
}
|
||||
|
||||
func TestInvocationHandlerWithData(t *testing.T) {
|
||||
data := `{"name": "test", "data": hellow}`
|
||||
s := newServer("", nil)
|
||||
|
|
|
|||
|
|
@ -48,6 +48,9 @@ func (s *Server) AddTopicEventHandler(sub *common.Subscription, fn func(ctx cont
|
|||
if sub.Route == "" {
|
||||
return errors.New("handler route name")
|
||||
}
|
||||
if fn == nil {
|
||||
return fmt.Errorf("topic handler required")
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(sub.Route, "/") {
|
||||
sub.Route = fmt.Sprintf("/%s", sub.Route)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,18 @@ func testErrorTopicFunc(ctx context.Context, e *common.TopicEvent) (retry bool,
|
|||
return true, errors.New("error to cause a retry")
|
||||
}
|
||||
|
||||
func TestEventNilHandler(t *testing.T) {
|
||||
s := newServer("", nil)
|
||||
sub := &common.Subscription{
|
||||
PubsubName: "messages",
|
||||
Topic: "test",
|
||||
Route: "/",
|
||||
Metadata: map[string]string{},
|
||||
}
|
||||
err := s.AddTopicEventHandler(sub, nil)
|
||||
assert.Errorf(t, err, "expected error adding event handler")
|
||||
}
|
||||
|
||||
func TestEventHandler(t *testing.T) {
|
||||
data := `{
|
||||
"specversion" : "1.0",
|
||||
|
|
|
|||
Loading…
Reference in New Issue