Skip to content

Commit 79e0f55

Browse files
authored
new service with existing http mux (#52)
1 parent d6de57c commit 79e0f55

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

service/http/binding_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func TestBindingHandlerWithoutData(t *testing.T) {
16-
s := newService("")
16+
s := newServer("", nil)
1717
err := s.AddBindingInvocationHandler("/", func(ctx context.Context, in *common.BindingEvent) (out []byte, err error) {
1818
if in == nil {
1919
return nil, errors.New("nil input")
@@ -37,7 +37,7 @@ func TestBindingHandlerWithoutData(t *testing.T) {
3737

3838
func TestBindingHandlerWithData(t *testing.T) {
3939
data := `{"name": "test"}`
40-
s := newService("")
40+
s := newServer("", nil)
4141
err := s.AddBindingInvocationHandler("/", func(ctx context.Context, in *common.BindingEvent) (out []byte, err error) {
4242
if in == nil {
4343
return nil, errors.New("nil input")

service/http/invoke_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
func TestInvocationHandlerWithData(t *testing.T) {
1717
data := `{"name": "test", "data": hellow}`
18-
s := newService("")
18+
s := newServer("", nil)
1919
err := s.AddServiceInvocationHandler("/", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
2020
if in == nil || in.Data == nil || in.ContentType == "" {
2121
err = errors.New("nil input")
@@ -44,7 +44,7 @@ func TestInvocationHandlerWithData(t *testing.T) {
4444
}
4545

4646
func TestInvocationHandlerWithoutInputData(t *testing.T) {
47-
s := newService("")
47+
s := newServer("", nil)
4848
err := s.AddServiceInvocationHandler("/", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
4949
if in == nil || in.Data != nil {
5050
err = errors.New("nil input")
@@ -69,7 +69,7 @@ func TestInvocationHandlerWithoutInputData(t *testing.T) {
6969
}
7070

7171
func TestInvocationHandlerWithInvalidRoute(t *testing.T) {
72-
s := newService("")
72+
s := newServer("", nil)
7373
err := s.AddServiceInvocationHandler("/a", func(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
7474
return nil, nil
7575
})

service/http/service.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ import (
88

99
// NewService creates new Service
1010
func NewService(address string) common.Service {
11-
return newService(address)
11+
return newServer(address, nil)
1212
}
1313

14-
func newService(address string) *Server {
14+
// NewServiceWithMux creates new Service with existing http mux
15+
func NewServiceWithMux(address string, mux *http.ServeMux) common.Service {
16+
return newServer(address, mux)
17+
}
18+
19+
func newServer(address string, mux *http.ServeMux) *Server {
20+
if mux == nil {
21+
mux = http.NewServeMux()
22+
}
1523
return &Server{
1624
address: address,
17-
mux: http.NewServeMux(),
25+
mux: mux,
1826
topicSubscriptions: make([]*common.Subscription, 0),
1927
}
2028
}

service/http/service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ import (
77
)
88

99
func TestStoppingUnstartedService(t *testing.T) {
10-
s := newService("")
10+
s := newServer("", nil)
1111
assert.NotNil(t, s)
1212
}

service/http/topic_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestEventHandler(t *testing.T) {
2727
"data" : "eyJtZXNzYWdlIjoiaGVsbG8ifQ=="
2828
}`
2929

30-
s := newService("")
30+
s := newServer("", nil)
3131

3232
sub := &common.Subscription{
3333
PubsubName: "messages",

0 commit comments

Comments
 (0)