github.com/anycable/anycable-go@v1.5.1/protocol/protocol_test.go (about)

     1  package protocol
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/anycable/anycable-go/common"
     9  
    10  	pb "github.com/anycable/anycable-go/protos"
    11  )
    12  
    13  func buildSessionEnv() *common.SessionEnv {
    14  	url := "/cable-test"
    15  	headers := map[string]string{"cookie": "token=secret;"}
    16  
    17  	return &common.SessionEnv{URL: url, Headers: &headers}
    18  }
    19  
    20  func TestNewConnectMessage(t *testing.T) {
    21  	env := buildSessionEnv()
    22  
    23  	msg := NewConnectMessage(env)
    24  
    25  	assert.NotNil(t, msg.Env)
    26  	assert.Equal(t, "/cable-test", msg.Env.Url)
    27  	assert.Equal(t, map[string]string{"cookie": "token=secret;"}, msg.Env.Headers)
    28  }
    29  
    30  func TestNewCommandMessage(t *testing.T) {
    31  	t.Run("Base", func(t *testing.T) {
    32  		env := buildSessionEnv()
    33  
    34  		msg := NewCommandMessage(env, "subscribe", "test_channel", "user=john", "")
    35  
    36  		assert.Equal(t, "subscribe", msg.Command)
    37  		assert.Equal(t, "test_channel", msg.Identifier)
    38  		assert.Equal(t, "user=john", msg.ConnectionIdentifiers)
    39  		assert.NotNil(t, msg.Env)
    40  
    41  		assert.NotNil(t, "/cable-test", msg.Env.Url)
    42  		assert.Equal(t, map[string]string{"cookie": "token=secret;"}, msg.Env.Headers)
    43  	})
    44  
    45  	t.Run("With connection, channel state and data", func(t *testing.T) {
    46  		env := buildSessionEnv()
    47  		cstate := map[string]string{"_s_": "id=42"}
    48  		env.ConnectionState = &cstate
    49  
    50  		istate := map[string]string{"room": "room:1"}
    51  		channels := make(map[string]map[string]string)
    52  		channels["test_channel"] = istate
    53  
    54  		env.ChannelStates = &channels
    55  
    56  		msg := NewCommandMessage(env, "subscribe", "test_channel", "user=john", "action")
    57  
    58  		assert.Equal(t, "subscribe", msg.Command)
    59  		assert.Equal(t, "test_channel", msg.Identifier)
    60  		assert.Equal(t, "user=john", msg.ConnectionIdentifiers)
    61  		assert.Equal(t, "action", msg.Data)
    62  		assert.NotNil(t, msg.Env)
    63  
    64  		assert.Equal(t, cstate, msg.Env.Cstate)
    65  		assert.Equal(t, istate, msg.Env.Istate)
    66  	})
    67  }
    68  
    69  func TestNewDisconnectRequest(t *testing.T) {
    70  	env := buildSessionEnv()
    71  
    72  	cstate := map[string]string{"_s_": "id=42"}
    73  	istate := map[string]string{"test_channel": "{\"room\":\"room:1\"}"}
    74  
    75  	channels := make(map[string]map[string]string)
    76  	channels["test_channel"] = map[string]string{"room": "room:1"}
    77  
    78  	env.ConnectionState = &cstate
    79  	env.ChannelStates = &channels
    80  
    81  	msg := NewDisconnectMessage(env, "user=john", []string{"chat_42"})
    82  
    83  	assert.Equal(t, "user=john", msg.Identifiers)
    84  	assert.Equal(t, []string{"chat_42"}, msg.Subscriptions)
    85  	assert.Equal(t, cstate, msg.Env.Cstate)
    86  	assert.Equal(t, istate, msg.Env.Istate)
    87  }
    88  
    89  func TestParseConnectResponse(t *testing.T) {
    90  	t.Run("Success", func(t *testing.T) {
    91  		res := pb.ConnectionResponse{
    92  			Identifiers:   "user=john",
    93  			Transmissions: []string{"welcome"},
    94  			Status:        pb.Status_SUCCESS,
    95  			Env:           &pb.EnvResponse{Cstate: map[string]string{"_s_": "test-session"}},
    96  		}
    97  
    98  		result, err := ParseConnectResponse(&res)
    99  
   100  		assert.Nil(t, err)
   101  		assert.Equal(t, "user=john", result.Identifier)
   102  		assert.Equal(t, []string{"welcome"}, result.Transmissions)
   103  		assert.Equal(t, map[string]string{"_s_": "test-session"}, result.CState)
   104  		assert.Equal(t, common.SUCCESS, result.Status)
   105  	})
   106  
   107  	t.Run("Failure", func(t *testing.T) {
   108  		res := pb.ConnectionResponse{
   109  			Transmissions: []string{"unauthorized"},
   110  			Status:        pb.Status_FAILURE,
   111  			Env:           &pb.EnvResponse{Cstate: map[string]string{"_s_": "test-session"}},
   112  			ErrorMsg:      "Authentication failed",
   113  		}
   114  
   115  		result, err := ParseConnectResponse(&res)
   116  
   117  		assert.Nil(t, err)
   118  		assert.Equal(t, common.FAILURE, result.Status)
   119  	})
   120  
   121  	t.Run("Error", func(t *testing.T) {
   122  		res := pb.ConnectionResponse{
   123  			Transmissions: []string{"unauthorized"},
   124  			Status:        pb.Status_ERROR,
   125  			ErrorMsg:      "Failed",
   126  		}
   127  
   128  		result, err := ParseConnectResponse(&res)
   129  
   130  		assert.NotNil(t, err)
   131  		assert.Equal(t, common.ERROR, result.Status)
   132  	})
   133  }
   134  
   135  func TestParseCommandResponse(t *testing.T) {
   136  	t.Run("Success", func(t *testing.T) {
   137  		res := pb.CommandResponse{
   138  			Status:         pb.Status_SUCCESS,
   139  			Streams:        []string{"chat_42"},
   140  			StoppedStreams: []string{"chat_41"},
   141  			StopStreams:    true,
   142  			Transmissions:  []string{"message_sent"},
   143  		}
   144  
   145  		result, err := ParseCommandResponse(&res)
   146  
   147  		assert.Nil(t, err)
   148  		assert.Equal(t, []string{"chat_42"}, result.Streams)
   149  		assert.Equal(t, []string{"message_sent"}, result.Transmissions)
   150  		assert.Equal(t, true, result.StopAllStreams)
   151  		assert.Equal(t, []string{"chat_41"}, result.StoppedStreams)
   152  		assert.Equal(t, common.SUCCESS, result.Status)
   153  	})
   154  
   155  	t.Run("Success with connection and channel state", func(t *testing.T) {
   156  		res := pb.CommandResponse{
   157  			Status:        pb.Status_SUCCESS,
   158  			Streams:       []string{"chat_42"},
   159  			Env:           &pb.EnvResponse{Cstate: map[string]string{"_s_": "sentCount=1"}, Istate: map[string]string{"count": "1"}},
   160  			Transmissions: []string{"message_sent"},
   161  		}
   162  
   163  		result, err := ParseCommandResponse(&res)
   164  
   165  		assert.Nil(t, err)
   166  		assert.Equal(t, []string{"chat_42"}, result.Streams)
   167  		assert.Equal(t, []string{"message_sent"}, result.Transmissions)
   168  		assert.Equal(t, map[string]string{"_s_": "sentCount=1"}, result.CState)
   169  		assert.Equal(t, map[string]string{"count": "1"}, result.IState)
   170  	})
   171  
   172  	t.Run("Failure", func(t *testing.T) {
   173  		res := pb.CommandResponse{
   174  			Status:   pb.Status_FAILURE,
   175  			ErrorMsg: "Unknown command",
   176  		}
   177  
   178  		result, err := ParseCommandResponse(&res)
   179  
   180  		assert.Nil(t, err)
   181  		assert.Equal(t, common.FAILURE, result.Status)
   182  	})
   183  
   184  	t.Run("Error", func(t *testing.T) {
   185  		res := pb.CommandResponse{
   186  			Status:   pb.Status_ERROR,
   187  			ErrorMsg: "Unknown command",
   188  		}
   189  
   190  		result, err := ParseCommandResponse(&res)
   191  
   192  		assert.NotNil(t, err)
   193  		assert.Equal(t, common.ERROR, result.Status)
   194  	})
   195  }
   196  
   197  func TestParseDisconnectResponse(t *testing.T) {
   198  	t.Run("Success", func(t *testing.T) {
   199  		res := pb.DisconnectResponse{
   200  			Status: pb.Status_SUCCESS,
   201  		}
   202  
   203  		err := ParseDisconnectResponse(&res)
   204  
   205  		assert.Nil(t, err)
   206  	})
   207  
   208  	t.Run("Failure", func(t *testing.T) {
   209  		res := pb.DisconnectResponse{
   210  			Status: pb.Status_FAILURE,
   211  		}
   212  
   213  		err := ParseDisconnectResponse(&res)
   214  
   215  		assert.Nil(t, err)
   216  	})
   217  
   218  	t.Run("Error", func(t *testing.T) {
   219  		res := pb.DisconnectResponse{
   220  			Status: pb.Status_ERROR,
   221  		}
   222  
   223  		err := ParseDisconnectResponse(&res)
   224  
   225  		assert.NotNil(t, err)
   226  	})
   227  }