diff --git a/service/http/binding_test.go b/service/http/binding_test.go index 86bff7c1..d1410dd7 100755 --- a/service/http/binding_test.go +++ b/service/http/binding_test.go @@ -13,7 +13,7 @@ import ( ) func TestBindingHandlerWithoutData(t *testing.T) { - s := newService("") + s := newServer("", nil) err := s.AddBindingInvocationHandler("/", func(ctx context.Context, in *common.BindingEvent) (out []byte, err error) { if in == nil { return nil, errors.New("nil input") @@ -37,7 +37,7 @@ func TestBindingHandlerWithoutData(t *testing.T) { func TestBindingHandlerWithData(t *testing.T) { data := `{"name": "test"}` - s := newService("") + s := newServer("", nil) err := s.AddBindingInvocationHandler("/", func(ctx context.Context, in *common.BindingEvent) (out []byte, err error) { if in == nil { return nil, errors.New("nil input") diff --git a/service/http/invoke_test.go b/service/http/invoke_test.go index 2cdc39ce..c7635cc8 100755 --- a/service/http/invoke_test.go +++ b/service/http/invoke_test.go @@ -15,7 +15,7 @@ import ( func TestInvocationHandlerWithData(t *testing.T) { data := `{"name": "test", "data": hellow}` - s := newService("") + s := newServer("", nil) err := s.AddServiceInvocationHandler("/", 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") @@ -44,7 +44,7 @@ func TestInvocationHandlerWithData(t *testing.T) { } func TestInvocationHandlerWithoutInputData(t *testing.T) { - s := newService("") + s := newServer("", nil) err := s.AddServiceInvocationHandler("/", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) { if in == nil || in.Data != nil { err = errors.New("nil input") @@ -69,7 +69,7 @@ func TestInvocationHandlerWithoutInputData(t *testing.T) { } func TestInvocationHandlerWithInvalidRoute(t *testing.T) { - s := newService("") + s := newServer("", nil) err := s.AddServiceInvocationHandler("/a", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) { return nil, nil }) diff --git a/service/http/service.go b/service/http/service.go index a1de0575..60809b48 100755 --- a/service/http/service.go +++ b/service/http/service.go @@ -8,13 +8,21 @@ import ( // NewService creates new Service func NewService(address string) common.Service { - return newService(address) + return newServer(address, nil) } -func newService(address string) *Server { +// NewServiceWithMux creates new Service with existing http mux +func NewServiceWithMux(address string, mux *http.ServeMux) common.Service { + return newServer(address, mux) +} + +func newServer(address string, mux *http.ServeMux) *Server { + if mux == nil { + mux = http.NewServeMux() + } return &Server{ address: address, - mux: http.NewServeMux(), + mux: mux, topicSubscriptions: make([]*common.Subscription, 0), } } diff --git a/service/http/service_test.go b/service/http/service_test.go index 4cbc9415..4a7585ab 100644 --- a/service/http/service_test.go +++ b/service/http/service_test.go @@ -7,6 +7,6 @@ import ( ) func TestStoppingUnstartedService(t *testing.T) { - s := newService("") + s := newServer("", nil) assert.NotNil(t, s) } diff --git a/service/http/topic_test.go b/service/http/topic_test.go index 0af08b4b..1b10dda3 100755 --- a/service/http/topic_test.go +++ b/service/http/topic_test.go @@ -27,7 +27,7 @@ func TestEventHandler(t *testing.T) { "data" : "eyJtZXNzYWdlIjoiaGVsbG8ifQ==" }` - s := newService("") + s := newServer("", nil) sub := &common.Subscription{ PubsubName: "messages",