From 55f38103c1bb973bc6a0b5d60ffcf306fa99b3d2 Mon Sep 17 00:00:00 2001
From: jbhusson <jonah@elixxir.io>
Date: Fri, 26 Aug 2022 11:54:40 -0400
Subject: [PATCH] Add basic unit tests for webconn

---
 connect/webConn_test.go | 123 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 123 insertions(+)
 create mode 100644 connect/webConn_test.go

diff --git a/connect/webConn_test.go b/connect/webConn_test.go
new file mode 100644
index 0000000..798cf80
--- /dev/null
+++ b/connect/webConn_test.go
@@ -0,0 +1,123 @@
+package connect
+
+import (
+	"gitlab.com/xx_network/crypto/csprng"
+	"gitlab.com/xx_network/primitives/id"
+	"testing"
+)
+
+func TestWebConn_Close(t *testing.T) {
+	rng := csprng.NewSystemRNG()
+	hostId, err := id.NewRandomID(rng, id.User)
+	if err != nil {
+		t.Fatal(err)
+	}
+	hostParams := GetDefaultHostParams()
+	hostParams.ConnectionType = Web
+	h, err := NewHost(hostId, "0.0.0.0", nil, hostParams)
+	if err != nil {
+		t.Fatal(err)
+	}
+	wc := webConn{
+		h:          h,
+		connection: nil,
+	}
+	err = wc.Connect()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	err = wc.Close()
+	if err != nil {
+		t.Fatal(err)
+	}
+}
+
+func TestWebConn_Connect(t *testing.T) {
+	rng := csprng.NewSystemRNG()
+	hostId, err := id.NewRandomID(rng, id.User)
+	if err != nil {
+		t.Fatal(err)
+	}
+	hostParams := GetDefaultHostParams()
+	hostParams.ConnectionType = Web
+	h, err := NewHost(hostId, "0.0.0.0", nil, hostParams)
+	if err != nil {
+		t.Fatal(err)
+	}
+	wc := webConn{
+		h:          h,
+		connection: nil,
+	}
+	err = wc.Connect()
+	if err != nil {
+		t.Fatal(err)
+	}
+}
+
+func TestWebConn_GetGrpcConn(t *testing.T) {
+	defer func() {
+		if r := recover(); r == nil {
+			t.Error("Did not receive expected fatal error")
+		}
+	}()
+	rng := csprng.NewSystemRNG()
+	hostId, err := id.NewRandomID(rng, id.User)
+	if err != nil {
+		t.Fatal(err)
+	}
+	hostParams := GetDefaultHostParams()
+	hostParams.ConnectionType = Web
+	h, err := NewHost(hostId, "0.0.0.0", nil, hostParams)
+	if err != nil {
+		t.Fatal(err)
+	}
+	wc := webConn{
+		h:          h,
+		connection: nil,
+	}
+	err = wc.Connect()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	conn := wc.GetGrpcConn()
+	if conn != nil {
+		t.Fatal("Expected panic, received conn instead")
+	}
+
+}
+
+func TestWebConn_GetWebConn(t *testing.T) {
+	rng := csprng.NewSystemRNG()
+	hostId, err := id.NewRandomID(rng, id.User)
+	if err != nil {
+		t.Fatal(err)
+	}
+	hostParams := GetDefaultHostParams()
+	hostParams.ConnectionType = Web
+	h, err := NewHost(hostId, "0.0.0.0", nil, hostParams)
+	if err != nil {
+		t.Fatal(err)
+	}
+	wc := webConn{
+		h:          h,
+		connection: nil,
+	}
+	err = wc.Connect()
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	conn := wc.GetWebConn()
+	if conn == nil {
+		t.Fatal("Expected grpcConn, received nil instead")
+	}
+}
+
+func TestWebConn_IsWeb(t *testing.T) {
+	wc := webConn{}
+	if !wc.IsWeb() {
+		t.Fatal("WebConn is not web")
+	}
+}
-- 
GitLab