-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package channel | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ksysoev/wasabi" | ||
"github.com/ksysoev/wasabi/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"nhooyr.io/websocket" | ||
) | ||
|
||
func TestNewConnectionWrapper(t *testing.T) { | ||
mockConnection := mocks.NewMockConnection(t) | ||
|
||
wrapper := NewConnectionWrapper(mockConnection) | ||
|
||
assert.NotNil(t, wrapper) | ||
assert.Equal(t, mockConnection, wrapper.connection) | ||
} | ||
|
||
func TestConnectionWrapper_ID(t *testing.T) { | ||
mockConnection := mocks.NewMockConnection(t) | ||
wrapper := NewConnectionWrapper(mockConnection) | ||
|
||
expectedID := "testID" | ||
mockConnection.On("ID").Return(expectedID) | ||
|
||
actualID := wrapper.ID() | ||
|
||
assert.Equal(t, expectedID, actualID) | ||
} | ||
|
||
func TestConnectionWrapper_Context(t *testing.T) { | ||
mockConnection := mocks.NewMockConnection(t) | ||
wrapper := NewConnectionWrapper(mockConnection) | ||
|
||
expectedContext := context.TODO() | ||
mockConnection.On("Context").Return(expectedContext) | ||
|
||
actualContext := wrapper.Context() | ||
|
||
assert.Equal(t, expectedContext, actualContext) | ||
} | ||
|
||
func TestConnectionWrapper_HandleRequests(t *testing.T) { | ||
mockConnection := mocks.NewMockConnection(t) | ||
wrapper := NewConnectionWrapper(mockConnection) | ||
|
||
mockConnection.On("HandleRequests").Once() | ||
|
||
wrapper.HandleRequests() | ||
|
||
mockConnection.AssertExpectations(t) | ||
} | ||
func TestConnectionWrapper_Send_WithOnSendWrapper(t *testing.T) { | ||
mockConnection := mocks.NewMockConnection(t) | ||
wrapper := NewConnectionWrapper(mockConnection) | ||
|
||
expectedMsgType := wasabi.MessageType(1) | ||
expectedMsg := []byte("test message") | ||
|
||
mockOnSendWrapper := func(conn wasabi.Connection, msgType wasabi.MessageType, msg []byte) error { | ||
assert.Equal(t, mockConnection, conn) | ||
assert.Equal(t, expectedMsgType, msgType) | ||
assert.Equal(t, expectedMsg, msg) | ||
|
||
return nil | ||
} | ||
|
||
wrapper.onSendWrapper = mockOnSendWrapper | ||
|
||
err := wrapper.Send(expectedMsgType, expectedMsg) | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func TestConnectionWrapper_Send_WithoutOnSendWrapper(t *testing.T) { | ||
mockConnection := mocks.NewMockConnection(t) | ||
wrapper := NewConnectionWrapper(mockConnection) | ||
|
||
expectedMsgType := wasabi.MessageType(1) | ||
expectedMsg := []byte("test message") | ||
|
||
mockConnection.On("Send", expectedMsgType, expectedMsg).Return(nil) | ||
|
||
err := wrapper.Send(expectedMsgType, expectedMsg) | ||
|
||
assert.NoError(t, err) | ||
mockConnection.AssertExpectations(t) | ||
} | ||
|
||
func TestConnectionWrapper_Close_WithOnCloseWrapper(t *testing.T) { | ||
mockConnection := mocks.NewMockConnection(t) | ||
wrapper := NewConnectionWrapper(mockConnection) | ||
|
||
expectedStatus := websocket.StatusCode(1000) | ||
expectedReason := "test reason" | ||
expectedClosingCtx := context.TODO() | ||
|
||
mockOnCloseWrapper := func(conn wasabi.Connection, status websocket.StatusCode, reason string, closingCtx ...context.Context) error { | ||
assert.Equal(t, mockConnection, conn) | ||
assert.Equal(t, expectedStatus, status) | ||
assert.Equal(t, expectedReason, reason) | ||
assert.Equal(t, expectedClosingCtx, closingCtx[0]) | ||
|
||
return nil | ||
} | ||
|
||
wrapper.onCloseWrapper = mockOnCloseWrapper | ||
|
||
err := wrapper.Close(expectedStatus, expectedReason, expectedClosingCtx) | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func TestConnectionWrapper_Close_WithoutOnCloseWrapper(t *testing.T) { | ||
mockConnection := mocks.NewMockConnection(t) | ||
wrapper := NewConnectionWrapper(mockConnection) | ||
|
||
expectedStatus := websocket.StatusCode(1000) | ||
expectedReason := "test reason" | ||
expectedClosingCtx := context.TODO() | ||
|
||
mockConnection.On("Close", expectedStatus, expectedReason, expectedClosingCtx).Return(nil) | ||
|
||
err := wrapper.Close(expectedStatus, expectedReason, expectedClosingCtx) | ||
|
||
assert.NoError(t, err) | ||
mockConnection.AssertExpectations(t) | ||
} | ||
|
||
func TestConnectionWrapper_WithSendWrapper(t *testing.T) { | ||
mockConnection := mocks.NewMockConnection(t) | ||
wrapper := NewConnectionWrapper(mockConnection) | ||
|
||
if wrapper.onSendWrapper != nil { | ||
t.Error("Expected onSendWrapper to be nil") | ||
} | ||
|
||
cb := func(conn wasabi.Connection, msgType wasabi.MessageType, msg []byte) error { | ||
return nil | ||
} | ||
|
||
wrapper = NewConnectionWrapper(mockConnection, WithSendWrapper(cb)) | ||
|
||
if wrapper.onSendWrapper == nil { | ||
t.Error("Expected onSendWrapper to be set") | ||
} | ||
} | ||
|
||
func TestConnectionWrapper_WithCloseWrapper(t *testing.T) { | ||
mockConnection := mocks.NewMockConnection(t) | ||
wrapper := NewConnectionWrapper(mockConnection) | ||
|
||
if wrapper.onCloseWrapper != nil { | ||
t.Error("Expected onCloseWrapper to be nil") | ||
} | ||
|
||
cb := func(conn wasabi.Connection, status websocket.StatusCode, reason string, closingCtx ...context.Context) error { | ||
return nil | ||
} | ||
|
||
wrapper = NewConnectionWrapper(mockConnection, WithCloseWrapper(cb)) | ||
|
||
if wrapper.onCloseWrapper == nil { | ||
t.Error("Expected onCloseWrapper to be set") | ||
} | ||
} |