Skip to content

Commit

Permalink
new: use options for maniphttp.NewSubscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
primalmotion committed Mar 22, 2019
1 parent 861961b commit 113ef2e
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 34 deletions.
34 changes: 0 additions & 34 deletions maniphttp/push.go

This file was deleted.

86 changes: 86 additions & 0 deletions maniphttp/subscriber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package maniphttp

import (
"crypto/tls"
"fmt"
"strings"

"go.aporeto.io/manipulate"
"go.aporeto.io/manipulate/internal/push"
)

type subscribeConfig struct {
namespace string
endpoint string
recursive bool
tlsConfig *tls.Config
}

func newSubscribeConfig(m *httpManipulator) subscribeConfig {
return subscribeConfig{
endpoint: "events",
namespace: m.namespace,
tlsConfig: m.tlsConfig,
}
}

// SubscriberOption represents option to NewSubscriber.
type SubscriberOption func(*subscribeConfig)

// SubscriberOptionRecursive makes the subscriber to listen
// to events in current namespace and all children.
func SubscriberOptionRecursive(recursive bool) SubscriberOption {
return func(cfg *subscribeConfig) {
cfg.recursive = recursive
}
}

// SubscriberOptionNamespace sets the namespace from where the subscription
// should start.
// By default it is the same as the manipulator.
func SubscriberOptionNamespace(namespace string) SubscriberOption {
return func(cfg *subscribeConfig) {
cfg.namespace = namespace
}
}

// SubscriberOptionEndpoint sets the endpint to connect to.
// By default it is /events.
func SubscriberOptionEndpoint(endpoint string) SubscriberOption {
return func(cfg *subscribeConfig) {
cfg.endpoint = strings.TrimRight(strings.TrimLeft(endpoint, "/"), "/")
}
}

// NewSubscriber returns a new subscription.
func NewSubscriber(manipulator manipulate.Manipulator, options ...SubscriberOption) manipulate.Subscriber {

m, ok := manipulator.(*httpManipulator)
if !ok {
panic("You must pass a HTTP manipulator to maniphttp.NewSubscriber or maniphttp.NewSubscriberWithEndpoint")
}

cfg := newSubscribeConfig(m)
for _, opt := range options {
if opt == nil {
panic("nil passed as subscriber option")
}
opt(&cfg)
}

return push.NewSubscriber(
fmt.Sprintf("%s/%s", m.url, cfg.endpoint),
cfg.namespace,
m.currentPassword(),
m.registerRenewNotifier,
m.unregisterRenewNotifier,
cfg.tlsConfig,
cfg.recursive,
)
}

// NewSubscriberWithEndpoint returns a new subscription connecting to specific endpoint.
func NewSubscriberWithEndpoint(manipulator manipulate.Manipulator, endpoint string, recursive bool) manipulate.Subscriber {

return NewSubscriber(manipulator, SubscriberOptionRecursive(recursive), SubscriberOptionEndpoint(endpoint))
}
59 changes: 59 additions & 0 deletions maniphttp/subscriber_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package maniphttp

import (
"crypto/tls"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestNewSubscriberOption(t *testing.T) {

Convey("Given I have manipulator", t, func() {

m := &httpManipulator{
url: "https://toto.com",
namespace: "mns",
tlsConfig: &tls.Config{},
}

Convey("When I newSubscribeConfig ", func() {

cfg := newSubscribeConfig(m)

Convey("Then cfg should be correct", func() {
So(cfg.recursive, ShouldBeFalse)
So(cfg.endpoint, ShouldEqual, "events")
So(cfg.namespace, ShouldEqual, "mns")
So(cfg.tlsConfig, ShouldEqual, m.tlsConfig)
})

})
})
}

func TestOptions(t *testing.T) {

m := &httpManipulator{
namespace: "mns",
tlsConfig: &tls.Config{},
}

Convey("SubscriberOptionRecursive should work", t, func() {
cfg := newSubscribeConfig(m)
SubscriberOptionRecursive(true)(&cfg)
So(cfg.recursive, ShouldBeTrue)
})

Convey("SubscriberOptionNamespace should work", t, func() {
cfg := newSubscribeConfig(m)
SubscriberOptionNamespace("/toto")(&cfg)
So(cfg.namespace, ShouldEqual, "/toto")
})

Convey("SubscriberOptionEndpoint should work", t, func() {
cfg := newSubscribeConfig(m)
SubscriberOptionEndpoint("/labas/")(&cfg)
So(cfg.endpoint, ShouldEqual, "labas")
})
}

0 comments on commit 113ef2e

Please sign in to comment.