From b9cd9af7fa83ad2fd7cca4727d5adc1be51fa384 Mon Sep 17 00:00:00 2001 From: Jose Garcia Date: Fri, 23 Jul 2021 01:17:32 +0000 Subject: [PATCH] Start of tests and comments --- client.go | 5 ++++ client_test.go | 73 +++++++++++++++++++++++++++++++++++++++++++++-- connection.go | 1 + port_forwarder.go | 3 ++ 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index d0e6e84a8..435dd6775 100644 --- a/client.go +++ b/client.go @@ -7,6 +7,7 @@ import ( "golang.org/x/crypto/ssh" ) +// A Client capable of joining a liveshare connection type Client struct { connection Connection @@ -14,8 +15,10 @@ type Client struct { rpc *rpcClient } +// A ClientOption is a function that modifies a client type ClientOption func(*Client) error +// NewClient accepts a range of options, applies them and returns a client func NewClient(opts ...ClientOption) (*Client, error) { client := new(Client) @@ -28,6 +31,7 @@ func NewClient(opts ...ClientOption) (*Client, error) { return client, nil } +// WithConnection is a ClientOption that accepts a Connection func WithConnection(connection Connection) ClientOption { return func(c *Client) error { if err := connection.validate(); err != nil { @@ -39,6 +43,7 @@ func WithConnection(connection Connection) ClientOption { } } +// Join is a method that joins the client to the liveshare session func (c *Client) Join(ctx context.Context) (err error) { clientSocket := newSocket(c.connection) if err := clientSocket.connect(ctx); err != nil { diff --git a/client_test.go b/client_test.go index 8d118974d..86f732637 100644 --- a/client_test.go +++ b/client_test.go @@ -1,22 +1,89 @@ package liveshare import ( + "fmt" + "net/http" + "net/http/httptest" "testing" + + "github.com/gorilla/websocket" ) +func TestNewClient(t *testing.T) { + client, err := NewClient() + if err != nil { + t.Errorf("error creating new client: %v", err) + } + if client == nil { + t.Error("client is nil") + } +} + +func TestNewClientValidConnection(t *testing.T) { + connection := Connection{"1", "2", "3", "4"} + + client, err := NewClient(WithConnection(connection)) + if err != nil { + t.Errorf("error creating new client: %v", err) + } + if client == nil { + t.Error("client is nil") + } +} + +func TestNewClientWithInvalidConnection(t *testing.T) { + connection := Connection{} + + if _, err := NewClient(WithConnection(connection)); err == nil { + t.Error("err is nil") + } +} + +var upgrader = websocket.Upgrader{} + +func newMockLiveShareServer() *httptest.Server { + endpoint := func(w http.ResponseWriter, req *http.Request) { + c, err := upgrader.Upgrade(w, req, nil) + if err != nil { + fmt.Println(err) + return + } + defer c.Close() + + for { + mt, message, err := c.ReadMessage() + if err != nil { + fmt.Println(err) + break + } + + err = c.WriteMessage(mt, message) + if err != nil { + fmt.Println(err) + break + } + + } + } + + return httptest.NewTLSServer(http.HandlerFunc(endpoint)) +} + func TestClientJoin(t *testing.T) { + // server := newMockLiveShareServer() + // defer server.Close() + // connection := Connection{ // SessionID: "session-id", // SessionToken: "session-token", - // RelayEndpoint: "relay-endpoint", // RelaySAS: "relay-sas", + // RelayEndpoint: "sb" + strings.TrimPrefix(server.URL, "https"), // } // client, err := NewClient(WithConnection(connection)) // if err != nil { - // t.Errorf("error creating client: %v", err) + // t.Errorf("error creating new client: %v", err) // } - // ctx := context.Background() // if err := client.Join(ctx); err != nil { // t.Errorf("error joining client: %v", err) diff --git a/connection.go b/connection.go index eda050f63..c1a4632c8 100644 --- a/connection.go +++ b/connection.go @@ -6,6 +6,7 @@ import ( "strings" ) +// A Connection represents a set of values necessary to join a liveshare connection type Connection struct { SessionID string SessionToken string diff --git a/port_forwarder.go b/port_forwarder.go index 8d42f3c05..6d459b4d6 100644 --- a/port_forwarder.go +++ b/port_forwarder.go @@ -8,6 +8,7 @@ import ( "strconv" ) +// A PortForwader can forward ports from a remote liveshare host to localhost type PortForwarder struct { client *Client server *Server @@ -15,6 +16,7 @@ type PortForwarder struct { errCh chan error } +// NewPortForwarder creates a new PortForwader with a given client, server and port func NewPortForwarder(client *Client, server *Server, port int) *PortForwarder { return &PortForwarder{ client: client, @@ -24,6 +26,7 @@ func NewPortForwarder(client *Client, server *Server, port int) *PortForwarder { } } +// Start is a method to start forwarding the server to a localhost port func (l *PortForwarder) Start(ctx context.Context) error { ln, err := net.Listen("tcp", ":"+strconv.Itoa(l.port)) if err != nil {