handler checks addressing issue #100 (#101)

* handler checks addressing issue #100

* handler check tests
This commit is contained in:
Mark Chmarny 2020-11-03 14:07:40 -08:00 committed by GitHub
parent 0a93facbba
commit b7a5b80a89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 42 additions and 0 deletions

View File

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

View File

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

View File

@ -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,

View File

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

View File

@ -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) {

View File

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

View File

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

View File

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

View File

@ -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",