Start of tests and comments
This commit is contained in:
parent
a99d0f5495
commit
b9cd9af7fa
4 changed files with 79 additions and 3 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue