Skip to content

Commit b7a5b80

Browse files
authored
handler checks addressing issue #100 (#101)
* handler checks addressing issue #100 * handler check tests
1 parent 0a93fac commit b7a5b80

File tree

9 files changed

+42
-0
lines changed

9 files changed

+42
-0
lines changed

service/grpc/binding.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ func (s *Server) AddBindingInvocationHandler(name string, fn func(ctx context.Co
1515
if name == "" {
1616
return fmt.Errorf("binding name required")
1717
}
18+
if fn == nil {
19+
return fmt.Errorf("binding handler required")
20+
}
1821
s.bindingHandlers[name] = fn
1922
return nil
2023
}

service/grpc/invoke.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ func (s *Server) AddServiceInvocationHandler(method string, fn func(ctx context.
1515
if method == "" {
1616
return fmt.Errorf("servie name required")
1717
}
18+
if fn == nil {
19+
return fmt.Errorf("invocation handler required")
20+
}
1821
s.invokeHandlers[method] = fn
1922
return nil
2023
}

service/grpc/topic.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func (s *Server) AddTopicEventHandler(sub *common.Subscription, fn func(ctx cont
2121
if sub.PubsubName == "" {
2222
return errors.New("pub/sub name required")
2323
}
24+
if fn == nil {
25+
return fmt.Errorf("topic handler required")
26+
}
2427
key := fmt.Sprintf("%s-%s", sub.PubsubName, sub.Topic)
2528
s.topicSubscriptions[key] = &topicEventHandler{
2629
component: sub.PubsubName,

service/http/binding.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ func (s *Server) AddBindingInvocationHandler(route string, fn func(ctx context.C
1515
if route == "" {
1616
return fmt.Errorf("binding route required")
1717
}
18+
if fn == nil {
19+
return fmt.Errorf("binding handler required")
20+
}
1821

1922
if !strings.HasPrefix(route, "/") {
2023
route = fmt.Sprintf("/%s", route)

service/http/binding_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
)
1414

15+
func TestBindingHandlerWithoutHandler(t *testing.T) {
16+
s := newServer("", nil)
17+
err := s.AddBindingInvocationHandler("/", nil)
18+
assert.Errorf(t, err, "expected error adding nil binding event handler")
19+
}
20+
1521
func TestBindingHandlerWithoutData(t *testing.T) {
1622
s := newServer("", nil)
1723
err := s.AddBindingInvocationHandler("/", func(ctx context.Context, in *common.BindingEvent) (out []byte, err error) {

service/http/invoke.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ func (s *Server) AddServiceInvocationHandler(route string, fn func(ctx context.C
1616
if route == "" {
1717
return fmt.Errorf("service route required")
1818
}
19+
if fn == nil {
20+
return fmt.Errorf("invocation handler required")
21+
}
1922

2023
if !strings.HasPrefix(route, "/") {
2124
route = fmt.Sprintf("/%s", route)

service/http/invoke_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import (
1414
"github.com/stretchr/testify/assert"
1515
)
1616

17+
func TestInvocationHandlerWithoutHandler(t *testing.T) {
18+
s := newServer("", nil)
19+
err := s.AddServiceInvocationHandler("/", nil)
20+
assert.Errorf(t, err, "expected error adding event handler")
21+
}
22+
1723
func TestInvocationHandlerWithData(t *testing.T) {
1824
data := `{"name": "test", "data": hellow}`
1925
s := newServer("", nil)

service/http/topic.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ func (s *Server) AddTopicEventHandler(sub *common.Subscription, fn func(ctx cont
4848
if sub.Route == "" {
4949
return errors.New("handler route name")
5050
}
51+
if fn == nil {
52+
return fmt.Errorf("topic handler required")
53+
}
5154

5255
if !strings.HasPrefix(sub.Route, "/") {
5356
sub.Route = fmt.Sprintf("/%s", sub.Route)

service/http/topic_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ func testErrorTopicFunc(ctx context.Context, e *common.TopicEvent) (retry bool,
2626
return true, errors.New("error to cause a retry")
2727
}
2828

29+
func TestEventNilHandler(t *testing.T) {
30+
s := newServer("", nil)
31+
sub := &common.Subscription{
32+
PubsubName: "messages",
33+
Topic: "test",
34+
Route: "/",
35+
Metadata: map[string]string{},
36+
}
37+
err := s.AddTopicEventHandler(sub, nil)
38+
assert.Errorf(t, err, "expected error adding event handler")
39+
}
40+
2941
func TestEventHandler(t *testing.T) {
3042
data := `{
3143
"specversion" : "1.0",

0 commit comments

Comments
 (0)