github.com/anycable/anycable-go@v1.5.1/router/router_test.go (about)

     1  package router
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/anycable/anycable-go/common"
     8  	"github.com/anycable/anycable-go/mocks"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestRouterControllerWithoutRoutes(t *testing.T) {
    14  	controller := mocks.Controller{}
    15  	env := common.NewSessionEnv("ws://demo.anycable.io/cable", &map[string]string{"cookie": "val=1;"})
    16  
    17  	subject := NewRouterController(&controller)
    18  
    19  	t.Run("Authenticate", func(t *testing.T) {
    20  		expected := &common.ConnectResult{Identifier: "test_ids", Transmissions: []string{"{\"type\":\"welcome\"}"}, Status: common.SUCCESS}
    21  
    22  		controller.On("Authenticate", "2022", env).Return(expected, nil)
    23  
    24  		res, err := subject.Authenticate("2022", env)
    25  
    26  		assert.NoError(t, err)
    27  		assert.Equal(t, expected, res)
    28  		controller.AssertCalled(t, "Authenticate", "2022", env)
    29  	})
    30  
    31  	t.Run("Start", func(t *testing.T) {
    32  		controller.On("Start").Return(nil)
    33  
    34  		assert.Nil(t, subject.Start())
    35  
    36  		controller.AssertCalled(t, "Start")
    37  	})
    38  
    39  	t.Run("Shutdown", func(t *testing.T) {
    40  		controller.On("Shutdown").Return(nil)
    41  
    42  		assert.Nil(t, subject.Shutdown())
    43  
    44  		controller.AssertCalled(t, "Shutdown")
    45  	})
    46  
    47  	t.Run("Disconnect", func(t *testing.T) {
    48  		expectedErr := errors.New("foo")
    49  		controller.On("Disconnect", "42", env, "name=jack", []string{"chat"}).Return(expectedErr)
    50  
    51  		err := subject.Disconnect("42", env, "name=jack", []string{"chat"})
    52  
    53  		assert.Equal(t, expectedErr, err)
    54  
    55  		controller.AssertCalled(t, "Disconnect", "42", env, "name=jack", []string{"chat"})
    56  	})
    57  }
    58  
    59  func TestRouterControllerWithRoutes(t *testing.T) {
    60  	controller := mocks.Controller{}
    61  	env := common.NewSessionEnv("ws://demo.anycable.io/cable", &map[string]string{"cookie": "val=1;"})
    62  	commandResult := &common.CommandResult{Transmissions: []string{"message_sent"}, Streams: []string{"missing"}}
    63  
    64  	subject := NewRouterController(&controller)
    65  
    66  	chatController := mocks.Controller{}
    67  	chatResult := &common.CommandResult{Transmissions: []string{"message_sent"}, Streams: []string{"chat_42"}}
    68  	require.NoError(t, subject.Route("ChatChannel", &chatController))
    69  
    70  	echoController := mocks.Controller{}
    71  	require.NoError(t, subject.Route("EchoChannel", &echoController))
    72  	echoResult := &common.CommandResult{Transmissions: []string{"message_sent"}, Streams: []string{"hi"}}
    73  
    74  	t.Run("Subscribe (channel with params)", func(t *testing.T) {
    75  		channel := "{\"channel\":\"ChatChannel\",\"id\":\"42\"}"
    76  
    77  		controller.On("Subscribe", "42", env, "name=jack", channel).Return(nil, errors.New("Shouldn't be called"))
    78  		chatController.On("Subscribe", "42", env, "name=jack", channel).Return(chatResult, nil)
    79  
    80  		res, err := subject.Subscribe("42", env, "name=jack", channel)
    81  
    82  		require.NoError(t, err)
    83  		assert.Equal(t, chatResult, res)
    84  	})
    85  
    86  	t.Run("Subscribe (fallback)", func(t *testing.T) {
    87  		controller.On("Subscribe", "42", env, "name=jack", "fallback").Return(commandResult, nil)
    88  
    89  		res, err := subject.Subscribe("42", env, "name=jack", "fallback")
    90  
    91  		require.NoError(t, err)
    92  		assert.Equal(t, commandResult, res)
    93  
    94  		controller.AssertCalled(t, "Subscribe", "42", env, "name=jack", "fallback")
    95  	})
    96  
    97  	t.Run("Subscribe (pass)", func(t *testing.T) {
    98  		channel := "{\"channel\":\"ChatChannel\",\"id\":\"2021\"}"
    99  
   100  		controller.On("Subscribe", "42", env, "name=jack", channel).Return(commandResult, nil)
   101  		chatController.On("Subscribe", "42", env, "name=jack", channel).Return(nil, nil)
   102  
   103  		res, err := subject.Subscribe("42", env, "name=jack", channel)
   104  
   105  		require.NoError(t, err)
   106  		assert.Equal(t, commandResult, res)
   107  
   108  		controller.AssertCalled(t, "Subscribe", "42", env, "name=jack", channel)
   109  	})
   110  
   111  	t.Run("Unsubscribe (fallback)", func(t *testing.T) {
   112  		controller.On("Unsubscribe", "42", env, "name=jack", "fallback").Return(commandResult, nil)
   113  
   114  		res, err := subject.Unsubscribe("42", env, "name=jack", "fallback")
   115  
   116  		require.NoError(t, err)
   117  		assert.Equal(t, commandResult, res)
   118  
   119  		controller.AssertCalled(t, "Unsubscribe", "42", env, "name=jack", "fallback")
   120  	})
   121  
   122  	t.Run("Unsubscribe (pass)", func(t *testing.T) {
   123  		channel := "{\"channel\":\"ChatChannel\",\"id\":\"2021\"}"
   124  
   125  		controller.On("Unsubscribe", "42", env, "name=jack", channel).Return(commandResult, nil)
   126  		chatController.On("Unsubscribe", "42", env, "name=jack", channel).Return(nil, nil)
   127  
   128  		res, err := subject.Unsubscribe("42", env, "name=jack", channel)
   129  
   130  		require.NoError(t, err)
   131  		assert.Equal(t, commandResult, res)
   132  
   133  		controller.AssertCalled(t, "Unsubscribe", "42", env, "name=jack", channel)
   134  	})
   135  
   136  	t.Run("Unsubscribe (channel with params)", func(t *testing.T) {
   137  		channel := "{\"channel\":\"ChatChannel\",\"id\":\"42\"}"
   138  
   139  		controller.On("Unsubscribe", "42", env, "name=jack", channel).Return(nil, errors.New("Shouldn't be called"))
   140  		chatController.On("Unsubscribe", "42", env, "name=jack", channel).Return(chatResult, nil)
   141  
   142  		res, err := subject.Unsubscribe("42", env, "name=jack", channel)
   143  
   144  		require.NoError(t, err)
   145  		assert.Equal(t, chatResult, res)
   146  	})
   147  
   148  	t.Run("Perform (channel w/o params)", func(t *testing.T) {
   149  		channel := "{\"channel\":\"EchoChannel\"}"
   150  
   151  		controller.On("Perform", "42", env, "name=jack", channel, "ping").Return(nil, errors.New("Shouldn't be called"))
   152  		echoController.On("Perform", "42", env, "name=jack", channel, "ping").Return(echoResult, nil)
   153  
   154  		res, err := subject.Perform("42", env, "name=jack", channel, "ping")
   155  
   156  		require.NoError(t, err)
   157  		assert.Equal(t, echoResult, res)
   158  	})
   159  
   160  	t.Run("Perform (fallback)", func(t *testing.T) {
   161  		controller.On("Perform", "42", env, "name=jack", "fallback", "ping").Return(commandResult, nil)
   162  
   163  		res, err := subject.Perform("42", env, "name=jack", "fallback", "ping")
   164  
   165  		require.NoError(t, err)
   166  		assert.Equal(t, commandResult, res)
   167  
   168  		controller.AssertCalled(t, "Perform", "42", env, "name=jack", "fallback", "ping")
   169  	})
   170  
   171  	t.Run("Perform (pass)", func(t *testing.T) {
   172  		channel := "{\"channel\":\"EchoChannel\"}"
   173  
   174  		controller.On("Perform", "42", env, "name=jack", channel, "pass").Return(commandResult, nil)
   175  		echoController.On("Perform", "42", env, "name=jack", channel, "pass").Return(nil, nil)
   176  
   177  		res, err := subject.Perform("42", env, "name=jack", channel, "pass")
   178  
   179  		require.NoError(t, err)
   180  		assert.Equal(t, commandResult, res)
   181  
   182  		controller.AssertCalled(t, "Perform", "42", env, "name=jack", channel, "pass")
   183  	})
   184  }