diff --git a/README.md b/README.md index da207bb..e931013 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@
-
+
diff --git a/test/mock_test.go b/test/mock_test.go
new file mode 100644
index 0000000..b8e52cd
--- /dev/null
+++ b/test/mock_test.go
@@ -0,0 +1,37 @@
+package test
+
+import (
+ "testing"
+
+ "github.com/official-stallion/stallion"
+)
+
+// TestMock
+// testing stallion mock client.
+func TestMock(t *testing.T) {
+ // creating a mock client
+ mock := stallion.NewMockClient()
+ if mock == nil {
+ t.Error("failed to create mock client")
+ }
+
+ // first we subscribe over a topic
+ mock.Subscribe("topic", func(bytes []byte) {
+ t.Log("successful subscribe")
+ })
+
+ // we should be able to publish over that topic
+ err := mock.Publish("topic", []byte("message"))
+ if err != nil {
+ t.Error(err)
+ }
+
+ // now we test unsubscribing
+ mock.Unsubscribe("topic")
+
+ // we should get an error when we publish over this topic again
+ err = mock.Publish("topic", []byte("message"))
+ if err == nil {
+ t.Error("failed to unsubscribe")
+ }
+}
diff --git a/test/server_test.go b/test/server_test.go
new file mode 100644
index 0000000..3207aa4
--- /dev/null
+++ b/test/server_test.go
@@ -0,0 +1,75 @@
+package test
+
+import (
+ "testing"
+
+ "github.com/official-stallion/stallion"
+)
+
+// TestServer
+// testing stallion server.
+func TestServer(t *testing.T) {
+ // creating a server on port 6000
+ go func() {
+ if err := stallion.NewServer(":6000"); err != nil {
+ t.Errorf("server failed to start: %v", err)
+ }
+ }()
+
+ // client does not give a valid url, so we should get error
+ c, err := stallion.NewClient("localhost:6000")
+ if err == nil {
+ t.Error(err)
+ }
+
+ // client should connect
+ c, err = stallion.NewClient("st://localhost:6000")
+ if err != nil {
+ t.Error(err)
+ }
+
+ // subscribe over a topic
+ c.Subscribe("topic", func(bytes []byte) {
+ t.Log("success subscribe")
+ })
+
+ // we should be able to subscribe
+ err = c.Publish("topic", []byte("message"))
+ if err != nil {
+ t.Error(err)
+ }
+}
+
+// TestAuthServer
+// testing stallion server with auth.
+func TestAuthServer(t *testing.T) {
+ // creating a stallion server on port 6001 with user and pass
+ go func() {
+ if err := stallion.NewServer(":6001", "root", "password"); err != nil {
+ t.Errorf("server failed to start: %v", err)
+ }
+ }()
+
+ // client is not authorized we shoud get error
+ c, err := stallion.NewClient("st://r:pass@localhost:6001")
+ if err == nil {
+ t.Error(err)
+ }
+
+ // client should connect
+ c, err = stallion.NewClient("st://root:password@localhost:6001")
+ if err != nil {
+ t.Error(err)
+ }
+
+ // subscribe over a topic
+ c.Subscribe("topic", func(bytes []byte) {
+ t.Log("success subscribe")
+ })
+
+ // we should be able to subscribe
+ err = c.Publish("topic", []byte("message"))
+ if err != nil {
+ t.Error(err)
+ }
+}