Skip to content

Commit

Permalink
new service with existing http mux (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchmarny authored Aug 18, 2020
1 parent d6de57c commit 79e0f55
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions service/http/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions service/http/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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
})
Expand Down
14 changes: 11 additions & 3 deletions service/http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down
2 changes: 1 addition & 1 deletion service/http/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
)

func TestStoppingUnstartedService(t *testing.T) {
s := newService("")
s := newServer("", nil)
assert.NotNil(t, s)
}
2 changes: 1 addition & 1 deletion service/http/topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestEventHandler(t *testing.T) {
"data" : "eyJtZXNzYWdlIjoiaGVsbG8ifQ=="
}`

s := newService("")
s := newServer("", nil)

sub := &common.Subscription{
PubsubName: "messages",
Expand Down

0 comments on commit 79e0f55

Please sign in to comment.