Skip to content

Commit

Permalink
Adds request factopry usage to test cases and code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed May 18, 2024
1 parent 6339368 commit cb0f217
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
52 changes: 45 additions & 7 deletions backend/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/ksysoev/wasabi"
"github.com/ksysoev/wasabi/mocks"
"nhooyr.io/websocket"
)
Expand Down Expand Up @@ -47,7 +48,9 @@ var wsHandlerEcho = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request

func TestNewWSBackend(t *testing.T) {
url := "ws://example.com"
b := NewWSBackend(url)
b := NewWSBackend(url, func(r wasabi.Request) (websocket.MessageType, []byte, error) {
return websocket.MessageText, []byte("Hello, world!"), nil
})

if b.URL != url {
t.Errorf("Expected URL to be %q, but got %q", url, b.URL)
Expand All @@ -67,7 +70,9 @@ func TestGetConnectionExistingConnection(t *testing.T) {
defer server.Close()
url := "ws://" + server.Listener.Addr().String()

b := NewWSBackend(url)
b := NewWSBackend(url, func(r wasabi.Request) (websocket.MessageType, []byte, error) {
return websocket.MessageText, []byte("Hello, world!"), nil
})

conn := mocks.NewMockConnection(t)
conn.EXPECT().ID().Return("connection1")
Expand All @@ -90,7 +95,9 @@ func TestGetConnectionNewConnection(t *testing.T) {
defer server.Close()
url := "ws://" + server.Listener.Addr().String()

b := NewWSBackend(url)
b := NewWSBackend(url, func(r wasabi.Request) (websocket.MessageType, []byte, error) {
return websocket.MessageText, []byte("Hello, world!"), nil
})

conn := mocks.NewMockConnection(t)
conn.EXPECT().ID().Return("connection1")
Expand All @@ -115,7 +122,9 @@ func TestGetConnectionDialError(t *testing.T) {
url := "ws://" + server.Listener.Addr().String()
server.Close()

b := NewWSBackend(url)
b := NewWSBackend(url, func(r wasabi.Request) (websocket.MessageType, []byte, error) {
return websocket.MessageText, []byte("Hello, world!"), nil
})

conn := mocks.NewMockConnection(t)
conn.EXPECT().ID().Return("connection1")
Expand Down Expand Up @@ -151,7 +160,9 @@ func TestWSBackend_Handle(t *testing.T) {
r.EXPECT().Data().Return([]byte("Hello, world!"))
r.EXPECT().Context().Return(context.Background())

b := NewWSBackend(url)
b := NewWSBackend(url, func(r wasabi.Request) (websocket.MessageType, []byte, error) {
return websocket.MessageText, r.Data(), nil
})

err := b.Handle(conn, r)

Expand All @@ -173,7 +184,9 @@ func TestWSBackend_Handle_FailToConnect(t *testing.T) {

r := mocks.NewMockRequest(t)

b := NewWSBackend(url)
b := NewWSBackend(url, func(r wasabi.Request) (websocket.MessageType, []byte, error) {
return websocket.MessageText, []byte("Hello, world!"), nil
})

err := b.Handle(conn, r)

Expand All @@ -196,7 +209,9 @@ func TestWSBackend_Handle_CloseConnection(t *testing.T) {

conn.EXPECT().Close(websocket.StatusNormalClosure, "").Return(nil)

b := NewWSBackend(url)
b := NewWSBackend(url, func(r wasabi.Request) (websocket.MessageType, []byte, error) {
return websocket.MessageText, []byte("Hello, world!"), nil
})

wsConn, resp, err := websocket.Dial(ctx, url, nil)
if err != nil {
Expand All @@ -222,3 +237,26 @@ func TestWSBackend_Handle_CloseConnection(t *testing.T) {
t.Error("Expected connection to be closed")
}
}

func TestWSBackend_RequestFactory_Error(t *testing.T) {
server := httptest.NewServer(wsHandlerEcho)
url := "ws://" + server.Listener.Addr().String()

defer server.Close()

conn := mocks.NewMockConnection(t)
conn.EXPECT().ID().Return("connection1")
conn.EXPECT().Context().Return(context.Background())

r := mocks.NewMockRequest(t)

b := NewWSBackend(url, func(r wasabi.Request) (websocket.MessageType, []byte, error) {
return 0, nil, io.EOF
})

err := b.Handle(conn, r)

if err == nil {
t.Errorf("Expected error, but got nil")
}
}
15 changes: 14 additions & 1 deletion examples/passthrough/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@ func main() {

slog.LogAttrs(context.Background(), slog.LevelDebug, "")

backend := backend.NewWSBackend("wss://ws.derivws.com/websockets/v3?app_id=1089")
backend := backend.NewWSBackend(
"wss://ws.derivws.com/websockets/v3?app_id=1089",
func(r wasabi.Request) (wasabi.MessageType, []byte, error) {
switch r.RoutingKey() {
case "text":
return wasabi.MsgTypeText, r.Data(), nil
case "binary":
return wasabi.MsgTypeBinary, r.Data(), nil
default:
var t wasabi.MessageType
return t, nil, fmt.Errorf("unsupported request type: %s", r.RoutingKey())
}
},
)

dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(conn.Context(), msgType, data)
Expand Down

0 comments on commit cb0f217

Please sign in to comment.