new service with existing http mux (#52)

This commit is contained in:
Mark Chmarny 2020-08-18 05:49:38 -07:00 committed by GitHub
parent d6de57c71a
commit 79e0f554b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 10 deletions

View File

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

View File

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

View File

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

View File

@ -7,6 +7,6 @@ import (
)
func TestStoppingUnstartedService(t *testing.T) {
s := newService("")
s := newServer("", nil)
assert.NotNil(t, s)
}

View File

@ -27,7 +27,7 @@ func TestEventHandler(t *testing.T) {
"data" : "eyJtZXNzYWdlIjoiaGVsbG8ifQ=="
}`
s := newService("")
s := newServer("", nil)
sub := &common.Subscription{
PubsubName: "messages",