diff --git a/README.md b/README.md index 6c5cd4b..da207bb 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@
-
+
@@ -22,6 +22,7 @@ Using no external libraries, just internal Golang libraries.
- [Publish](#publish-over-a-topic)
- [Unsubscribe](#unsubscribe-from-a-topic)
- [Auth](#creating-a-server-with-auth)
+- [Mock](#mock)
## How to use?
Get package:
@@ -108,3 +109,16 @@ if err != nil {
}
```
+## Mock
+You can create a mock client to create a stallion client sample:
+```go
+package main
+
+import "github.com/official-stallion/stallion"
+
+func main() {
+ client := stallion.NewMockClient()
+
+ client.Publish("topic", []byte("message"))
+}
+```
diff --git a/mock.go b/mock.go
new file mode 100644
index 0000000..f3fa58b
--- /dev/null
+++ b/mock.go
@@ -0,0 +1,46 @@
+package stallion
+
+import (
+ "fmt"
+
+ "github.com/official-stallion/stallion/internal"
+)
+
+// mockClient
+// mocks stallion client.
+type mockClient struct {
+ // list of the topics with their handlers
+ topics map[string]internal.MessageHandler
+}
+
+// NewMockClient
+// creates a new mock client.
+func NewMockClient() Client {
+ return &mockClient{
+ topics: make(map[string]internal.MessageHandler),
+ }
+}
+
+// Publish
+// send messages over mock client.
+func (m *mockClient) Publish(topic string, data []byte) error {
+ if handler, ok := m.topics[topic]; ok {
+ handler(data)
+
+ return nil
+ }
+
+ return fmt.Errorf("failed to publish")
+}
+
+// Subscribe
+// over a topic.
+func (m *mockClient) Subscribe(topic string, handler internal.MessageHandler) {
+ m.topics[topic] = handler
+}
+
+// Unsubscribe
+// from a topic.
+func (m *mockClient) Unsubscribe(topic string) {
+ delete(m.topics, topic)
+}