| 1 |
package centrifuge |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"net/http" |
| 8 |
"net/http/httptest" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/centrifugal/protocol" |
| 14 |
|
| 15 |
"github.com/gorilla/websocket" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
) |
| 18 |
|
| 19 |
func sockjsData(data []byte) []byte { |
| 20 |
quoted, _ := json.Marshal(string(data)) |
| 21 |
return []byte(fmt.Sprintf("[%s]", string(quoted))) |
| 22 |
} |
| 23 |
|
| 24 |
func TestSockjsHandler(t *testing.T) { |
| 25 |
n, _ := New(Config{}) |
| 26 |
require.NoError(t, n.Run()) |
| 27 |
defer func() { _ = n.Shutdown(context.Background()) }() |
| 28 |
mux := http.NewServeMux() |
| 29 |
|
| 30 |
n.OnConnecting(func(ctx context.Context, event ConnectEvent) (ConnectReply, error) { |
| 31 |
require.Equal(t, transportSockJS, event.Transport.Name()) |
| 32 |
require.Equal(t, ProtocolTypeJSON, event.Transport.Protocol()) |
| 33 |
require.Equal(t, EncodingTypeJSON, event.Transport.Encoding()) |
| 34 |
return ConnectReply{ |
| 35 |
Credentials: &Credentials{UserID: "user"}, |
| 36 |
Data: []byte(`{"SockJS connect response": 1}`), |
| 37 |
}, nil |
| 38 |
}) |
| 39 |
|
| 40 |
doneCh := make(chan struct{}) |
| 41 |
|
| 42 |
n.OnConnect(func(client *Client) { |
| 43 |
err := client.Send([]byte(`{"SockJS write": 1}`)) |
| 44 |
require.NoError(t, err) |
| 45 |
client.Disconnect(DisconnectForceReconnect) |
| 46 |
}) |
| 47 |
|
| 48 |
mux.Handle("/connection/sockjs/", NewSockjsHandler(n, SockjsConfig{ |
| 49 |
HandlerPrefix: "/connection/sockjs", |
| 50 |
})) |
| 51 |
server := httptest.NewServer(mux) |
| 52 |
defer server.Close() |
| 53 |
|
| 54 |
url := "ws" + server.URL[4:] |
| 55 |
|
| 56 |
conn, resp, err := websocket.DefaultDialer.Dial(url+"/connection/sockjs/220/fi0988475/websocket", nil) |
| 57 |
require.NoError(t, err) |
| 58 |
defer func() { _ = resp.Body.Close() }() |
| 59 |
require.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode) |
| 60 |
require.NotNil(t, conn) |
| 61 |
defer func() { _ = conn.Close() }() |
| 62 |
_, p, err := conn.ReadMessage() |
| 63 |
require.NoError(t, err) |
| 64 |
require.Equal(t, "o", string(p)) // open frame of SockJS protocol. |
| 65 |
|
| 66 |
connectRequest := &protocol.ConnectRequest{ |
| 67 |
Token: "boom", |
| 68 |
} |
| 69 |
params, _ := json.Marshal(connectRequest) |
| 70 |
cmd := &protocol.Command{ |
| 71 |
ID: 1, |
| 72 |
Method: protocol.MethodTypeConnect, |
| 73 |
Params: params, |
| 74 |
} |
| 75 |
cmdBytes, _ := json.Marshal(cmd) |
| 76 |
err = conn.WriteMessage(websocket.TextMessage, sockjsData(cmdBytes)) |
| 77 |
require.NoError(t, err) |
| 78 |
|
| 79 |
go func() { |
| 80 |
pos := 0 |
| 81 |
contentExpected := []string{ |
| 82 |
"SockJS connect response", |
| 83 |
"SockJS write", |
| 84 |
"force reconnect", |
| 85 |
} |
| 86 |
|
| 87 |
loop: |
| 88 |
for { |
| 89 |
_, p, err = conn.ReadMessage() |
| 90 |
if err != nil { |
| 91 |
break loop |
| 92 |
} |
| 93 |
|
| 94 |
for { |
| 95 |
if strings.Contains(string(p), contentExpected[pos]) { |
| 96 |
pos++ |
| 97 |
if pos >= len(contentExpected) { |
| 98 |
close(doneCh) |
| 99 |
break loop |
| 100 |
} |
| 101 |
} else { |
| 102 |
break |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
}() |
| 107 |
|
| 108 |
select { |
| 109 |
case <-doneCh: |
| 110 |
case <-time.After(5 * time.Second): |
| 111 |
t.Fatal("timeout waiting for closing done channel") |
| 112 |
} |
| 113 |
} |