-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: use options for maniphttp.NewSubscriber
- Loading branch information
1 parent
861961b
commit 113ef2e
Showing
3 changed files
with
145 additions
and
34 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} |