github.com/anycable/anycable-go@v1.5.1/mocks/mock_controller.go (about)

     1  package mocks
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/anycable/anycable-go/common"
     7  )
     8  
     9  // MockController implements controller interface for tests
    10  type MockController struct {
    11  	Started bool
    12  }
    13  
    14  // NewMockController builds new mock controller instance
    15  func NewMockController() MockController {
    16  	return MockController{Started: true}
    17  }
    18  
    19  func (c *MockController) Start() error {
    20  	return nil
    21  }
    22  
    23  // Authenticate emulates authentication process:
    24  // - if path is equal to "failure" then authentication failed
    25  // - otherwise returns value of headers['id'] as identifier
    26  func (c *MockController) Authenticate(sid string, env *common.SessionEnv) (*common.ConnectResult, error) {
    27  	if env.URL == "/failure" {
    28  		return &common.ConnectResult{Status: common.FAILURE, Transmissions: []string{"unauthorized"}}, nil
    29  	}
    30  
    31  	if env.URL == "/error" {
    32  		return &common.ConnectResult{Status: common.ERROR}, errors.New("Unknown")
    33  	}
    34  
    35  	res := common.ConnectResult{Identifier: (*env.Headers)["id"], Transmissions: []string{"welcome"}}
    36  
    37  	if (*env.Headers)["x-session-test"] != "" {
    38  		res.CState = map[string]string{"_s_": (*env.Headers)["x-session-test"]}
    39  	}
    40  
    41  	return &res, nil
    42  }
    43  
    44  // Subscribe emulates subscription process:
    45  // - if channel is equal to "failure" then returns subscription error
    46  // - if channel is equal to "disconnect" then returns result with disconnect set to true
    47  // - if channel is equal to "stream" then add "stream" to result.Streams
    48  // - otherwise returns success result with one transmission equal to sid
    49  func (c *MockController) Subscribe(sid string, env *common.SessionEnv, id string, channel string) (*common.CommandResult, error) {
    50  	if channel == "error" {
    51  		return nil, errors.New("Subscription Failure")
    52  	}
    53  
    54  	res := NewMockResult(sid)
    55  
    56  	if channel == "failure" {
    57  		res.Status = common.FAILURE
    58  		return res, nil
    59  	}
    60  
    61  	if channel == "disconnect" {
    62  		res.Disconnect = true
    63  		return res, nil
    64  	}
    65  
    66  	if channel == "with_stream" {
    67  		res.Streams = []string{"stream"}
    68  	}
    69  
    70  	return res, nil
    71  }
    72  
    73  // Unsubscribe returns command result
    74  func (c *MockController) Unsubscribe(sid string, env *common.SessionEnv, id string, channel string) (*common.CommandResult, error) {
    75  	if channel == "failure" {
    76  		return nil, errors.New("Unsubscription Failure")
    77  	}
    78  
    79  	res := NewMockResult(sid)
    80  	res.Transmissions = nil
    81  	return res, nil
    82  }
    83  
    84  // Perform return result with Transmissions containing data (i.e. emulates "echo" action)
    85  func (c *MockController) Perform(sid string, env *common.SessionEnv, id string, channel string, data string) (*common.CommandResult, error) {
    86  	if channel == "failure" {
    87  		return nil, errors.New("Perform Failure")
    88  	}
    89  
    90  	res := NewMockResult(sid)
    91  	res.Transmissions = []string{data}
    92  
    93  	if data == "session" {
    94  		res.CState = map[string]string{"_s_": "performed"}
    95  	}
    96  
    97  	if data == "stop_stream" {
    98  		res.StoppedStreams = []string{data}
    99  		res.Transmissions = nil
   100  	}
   101  
   102  	if data == "stop_and_start_streams" {
   103  		res.StopAllStreams = true
   104  		res.Streams = []string{"all"}
   105  		res.Transmissions = nil
   106  	}
   107  
   108  	if data == "channel_state" {
   109  		res.IState = map[string]string{"_c_": "performed"}
   110  	}
   111  
   112  	return res, nil
   113  }
   114  
   115  // Disconnect method stub
   116  func (c *MockController) Disconnect(sid string, env *common.SessionEnv, id string, subscriptions []string) error {
   117  	return nil
   118  }
   119  
   120  // Shutdown changes Started to false
   121  func (c *MockController) Shutdown() error {
   122  	c.Started = false
   123  	return nil
   124  }