func TestClientMessage(t *testing.T) { app := testApp() c, err := newClient(app, &testSession{}) assert.Equal(t, nil, err) // empty message err = c.message([]byte{}) assert.Equal(t, ErrInvalidMessage, err) // malformed message err = c.message([]byte("wroooong")) assert.NotEqual(t, nil, err) var cmds []clientCommand nonConnectFirstCmd := clientCommand{ Method: "subscribe", Params: []byte("{}"), } cmds = append(cmds, nonConnectFirstCmd) cmdBytes, err := json.Marshal(cmds) assert.Equal(t, nil, err) err = c.message(cmdBytes) assert.Equal(t, ErrUnauthorized, err) }
func TestNamespaceKey(t *testing.T) { app := testApp() assert.Equal(t, NamespaceKey("ns"), app.namespaceKey("ns:channel")) assert.Equal(t, NamespaceKey(""), app.namespaceKey("channel")) assert.Equal(t, NamespaceKey("ns"), app.namespaceKey("ns:channel:opa")) assert.Equal(t, NamespaceKey("ns"), app.namespaceKey("ns::channel")) }
func TestToFloat64(t *testing.T) { var eight interface{} = 8 assert.Equal(t, ToFloat64(8), 8.00) assert.Equal(t, ToFloat64(8.31), 8.31) assert.Equal(t, ToFloat64("8.31"), 8.31) assert.Equal(t, ToFloat64(eight), 8.0) }
func TestIndirectPointers(t *testing.T) { x := 13 y := &x z := &y assert.Equal(t, ToInt(y), 13) assert.Equal(t, ToInt(z), 13) }
func TestProjectByKey(t *testing.T) { app := testApp() p, found := app.projectByKey("nonexistent") assert.Equal(t, found, false) p, found = app.projectByKey("test1") assert.Equal(t, found, true) assert.Equal(t, p.Name, ProjectKey("test1")) }
func TestLevels(t *testing.T) { SetStdoutThreshold(LevelError) assert.Equal(t, outputThreshold, LevelError) SetLogThreshold(LevelCritical) assert.Equal(t, logThreshold, LevelCritical) assert.NotEqual(t, outputThreshold, LevelCritical) SetStdoutThreshold(LevelWarn) assert.Equal(t, outputThreshold, LevelWarn) }
func TestGetProjectByKey(t *testing.T) { s := getTestStructure() _, found := s.projectByKey("test3") assert.Equal(t, false, found, "found project that does not exist") _, found = s.projectByKey("test2") assert.Equal(t, true, found) }
func TestMemoryPresenceHub(t *testing.T) { h := newMemoryPresenceHub() assert.Equal(t, 0, len(h.presence)) testCh1 := ChannelID("channel1") testCh2 := ChannelID("channel2") uid := ConnID("uid") info := ClientInfo{ User: "******", Client: "client", } h.add(testCh1, uid, info) assert.Equal(t, 1, len(h.presence)) h.add(testCh2, uid, info) assert.Equal(t, 2, len(h.presence)) h.remove(testCh1, uid) // remove non existing must not fail err := h.remove(testCh1, uid) assert.Equal(t, nil, err) assert.Equal(t, 1, len(h.presence)) p, err := h.get(testCh1) assert.Equal(t, nil, err) assert.Equal(t, 0, len(p)) p, err = h.get(testCh2) assert.Equal(t, nil, err) assert.Equal(t, 1, len(p)) }
func TestStringQueueResize(t *testing.T) { q := New() assert.Equal(t, 0, q.Len()) assert.Equal(t, initialCapacity, q.Cap()) assert.Equal(t, false, q.Closed()) i := 0 for i < initialCapacity { q.Add(strconv.Itoa(i)) i++ } assert.Equal(t, initialCapacity, q.Cap()) q.Add("resize here") assert.Equal(t, initialCapacity*2, q.Cap()) q.Remove() // back to initial capacity assert.Equal(t, initialCapacity, q.Cap()) q.Add("new resize here") assert.Equal(t, initialCapacity*2, q.Cap()) q.Add("one more item, no resize must happen") assert.Equal(t, initialCapacity*2, q.Cap()) assert.Equal(t, initialCapacity+2, q.Len()) }
func TestAPIPresence(t *testing.T) { app := testApp() p, _ := app.projectByKey("test1") cmd := &presenceApiCommand{ Channel: "channel", } resp, err := app.presenceCmd(p, cmd) assert.Equal(t, nil, err) assert.Equal(t, nil, resp.err) }
func TestAPIHistory(t *testing.T) { app := testApp() p, _ := app.projectByKey("test1") cmd := &historyApiCommand{ Channel: "channel", } resp, err := app.historyCmd(p, cmd) assert.Equal(t, nil, err) assert.Equal(t, nil, resp.err) }
func TestAPIDisconnect(t *testing.T) { app := testApp() p, _ := app.projectByKey("test1") cmd := &disconnectApiCommand{ User: "******", } resp, err := app.disconnectCmd(p, cmd) assert.Equal(t, nil, err) assert.Equal(t, nil, resp.err) }
func TestAPIPublish(t *testing.T) { app := testApp() p, _ := app.projectByKey("test1") cmd := &publishApiCommand{ Channel: "channel", Data: []byte("null"), } resp, err := app.publishCmd(p, cmd) assert.Equal(t, nil, err) assert.Equal(t, nil, resp.err) }
func TestAdminHub(t *testing.T) { h := newAdminHub() c := newTestUserCC() err := h.add(c) assert.Equal(t, err, nil) assert.Equal(t, len(h.connections), 1) err = h.broadcast("message") assert.Equal(t, err, nil) err = h.remove(c) assert.Equal(t, err, nil) assert.Equal(t, len(h.connections), 0) }
func TestInfoHandler(t *testing.T) { app := testApp() r := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/info/", nil) app.InfoHandler(r, req) body, _ := ioutil.ReadAll(r.Body) assert.Equal(t, true, strings.Contains(string(body), "nodes")) assert.Equal(t, true, strings.Contains(string(body), "node_name")) assert.Equal(t, true, strings.Contains(string(body), "version")) assert.Equal(t, true, strings.Contains(string(body), "structure")) assert.Equal(t, true, strings.Contains(string(body), "engine")) }
func TestClientRefresh(t *testing.T) { app := testApp() c, err := newClient(app, &testSession{}) assert.Equal(t, nil, err) timestamp := strconv.FormatInt(time.Now().Unix(), 10) cmds := []clientCommand{testConnectCmd(timestamp), testSubscribeCmd("test")} err = c.handleCommands(cmds) assert.Equal(t, nil, err) cmds = []clientCommand{testRefreshCmd(timestamp)} err = c.handleCommands(cmds) assert.Equal(t, nil, err) }
func TestClientPing(t *testing.T) { app := testApp() c, err := newClient(app, &testSession{}) assert.Equal(t, nil, err) timestamp := strconv.FormatInt(time.Now().Unix(), 10) cmds := []clientCommand{testConnectCmd(timestamp)} err = c.handleCommands(cmds) assert.Equal(t, nil, err) resp, err := c.handleCmd(testPingCmd()) assert.Equal(t, nil, err) assert.Equal(t, nil, resp.err) }
func TestDefaultMux(t *testing.T) { app := testApp() mux := DefaultMux(app, "", "path/to/web", "sockjs url") server := httptest.NewServer(mux) defer server.Close() resp, err := http.Get(server.URL + "/connection/info") assert.Equal(t, nil, err) assert.Equal(t, http.StatusOK, resp.StatusCode) app.Shutdown() resp, err = http.Get(server.URL + "/connection/info") assert.Equal(t, nil, err) assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) }
func TestSingleObjectMessage(t *testing.T) { app := testApp() c, err := newClient(app, &testSession{}) assert.Equal(t, nil, err) nonConnectFirstCmd := clientCommand{ Method: "subscribe", Params: []byte("{}"), } cmdBytes, err := json.Marshal(nonConnectFirstCmd) assert.Equal(t, nil, err) err = c.message(cmdBytes) assert.Equal(t, ErrUnauthorized, err) }
func TestGetChannelOptions(t *testing.T) { s := getTestStructure() _, err := s.channelOpts("wrong_project_key", "test") assert.Equal(t, ErrProjectNotFound, err) _, err = s.channelOpts("test1", "test") assert.Equal(t, nil, err) _, err = s.channelOpts("test1", "") assert.Equal(t, nil, err) _, err = s.channelOpts("test1", "wrongnamespacekey") assert.Equal(t, ErrNamespaceNotFound, err) }
func TestShutdown(t *testing.T) { h := newClientHub() c := newTestUserCC() h.add(c) assert.Equal(t, len(h.users), 1) h.shutdown() }
func TestRawWsHandler(t *testing.T) { app := testApp() mux := DefaultMux(app, "", "path/to/web", "sockjs url") server := httptest.NewServer(mux) defer server.Close() url := "ws" + server.URL[4:] conn, resp, err := websocket.DefaultDialer.Dial(url+"/connection/websocket", nil) conn.Close() assert.Equal(t, nil, err) assert.NotEqual(t, nil, conn) assert.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode) app.Shutdown() _, resp, err = websocket.DefaultDialer.Dial(url+"/connection/websocket", nil) assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) }
func TestAdminAuthToken(t *testing.T) { app := testApp() // first without secret set token, err := app.adminAuthToken() assert.Equal(t, nil, err) assert.True(t, len(token) > 0) err = app.checkAdminAuthToken("") assert.Equal(t, ErrUnauthorized, err) err = app.checkAdminAuthToken(token) assert.Equal(t, ErrUnauthorized, err) // now with secret set app.config.WebSecret = "test" token, err = app.adminAuthToken() assert.Equal(t, nil, err) err = app.checkAdminAuthToken(token) assert.Equal(t, nil, err) }
func TestControlMessages(t *testing.T) { app := testApp() app.Run() // command from this node cmd := testPingControlCmd(app.uid) err := app.controlMsg(cmd) assert.Equal(t, nil, err) cmd = testPingControlCmd("another_node") err = app.controlMsg(cmd) assert.Equal(t, nil, err) err = app.controlMsg(testWrongControlCmd("another node")) assert.Equal(t, ErrInvalidMessage, err) err = app.controlMsg(testUnsubscribeControlCmd("another node")) assert.Equal(t, nil, err) err = app.controlMsg(testDisconnectControlCmd("another node")) assert.Equal(t, nil, err) }
func TestSubHub(t *testing.T) { h := newClientHub() c := newTestUserCC() h.addSub("test1", c) h.addSub("test2", c) assert.Equal(t, 2, h.nChannels()) channels := []string{} for _, ch := range h.channels() { channels = append(channels, string(ch)) } assert.Equal(t, stringInSlice("test1", channels), true) assert.Equal(t, stringInSlice("test2", channels), true) err := h.broadcast("test1", "message") assert.Equal(t, err, nil) h.removeSub("test1", c) h.removeSub("test2", c) assert.Equal(t, len(h.subs), 0) }
func TestClientSubscribePrivate(t *testing.T) { app := testApp() c, err := newClient(app, &testSession{}) assert.Equal(t, nil, err) timestamp := strconv.FormatInt(time.Now().Unix(), 10) cmds := []clientCommand{testConnectCmd(timestamp)} _ = c.handleCommands(cmds) resp, err := c.handleCmd(testSubscribeCmd("$test")) assert.Equal(t, nil, err) assert.Equal(t, ErrPermissionDenied, resp.err) resp, err = c.handleCmd(testSubscribePrivateCmd("$test", c.UID)) assert.Equal(t, nil, err) assert.Equal(t, nil, resp.err) }
func TestMemoryEngine(t *testing.T) { e := testMemoryEngine() assert.NotEqual(t, nil, e.historyHub) assert.NotEqual(t, nil, e.presenceHub) assert.NotEqual(t, e.name(), "") assert.Equal(t, nil, e.publish(ChannelID("channel"), []byte("{}"))) assert.Equal(t, nil, e.subscribe(ChannelID("channel"))) assert.Equal(t, nil, e.unsubscribe(ChannelID("channel"))) assert.Equal(t, nil, e.addPresence(ChannelID("channel"), "uid", ClientInfo{})) p, err := e.presence(ChannelID("channel")) assert.Equal(t, nil, err) assert.Equal(t, 1, len(p)) assert.Equal(t, nil, e.addHistory(ChannelID("channel"), Message{}, 1, 1)) h, err := e.history(ChannelID("channel")) assert.Equal(t, nil, err) assert.Equal(t, 1, len(h)) err = e.removePresence(ChannelID("channel"), "uid") assert.Equal(t, nil, err) }
func TestAdminWebsocketHandler(t *testing.T) { app := testApp() mux := DefaultMux(app, "", "path/to/web", "sockjs url") server := httptest.NewServer(mux) defer server.Close() url := "ws" + server.URL[4:] conn, resp, err := websocket.DefaultDialer.Dial(url+"/socket", nil) data := map[string]interface{}{ "method": "ping", "params": map[string]string{}, } conn.WriteJSON(data) var response interface{} conn.ReadJSON(&response) conn.Close() assert.Equal(t, nil, err) assert.NotEqual(t, nil, conn) assert.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode) }
func TestClientConnect(t *testing.T) { app := testApp() c, err := newClient(app, &testSession{}) assert.Equal(t, nil, err) var cmd clientCommand var cmds []clientCommand cmd = clientCommand{ Method: "connect", Params: []byte("{}"), } cmds = []clientCommand{cmd} err = c.handleCommands(cmds) assert.Equal(t, ErrProjectNotFound, err) cmd = clientCommand{ Method: "connect", Params: []byte(`{"project": "test1"}`), } cmds = []clientCommand{cmd} err = c.handleCommands(cmds) assert.Equal(t, ErrInvalidToken, err) timestamp := strconv.FormatInt(time.Now().Unix(), 10) cmds = []clientCommand{testConnectCmd(timestamp)} err = c.handleCommands(cmds) assert.Equal(t, nil, err) assert.Equal(t, true, c.authenticated) ts, err := strconv.Atoi(timestamp) assert.Equal(t, int64(ts), c.timestamp) clientInfo := c.info(Channel("")) assert.Equal(t, UserID("user1"), clientInfo.User) assert.Equal(t, 1, len(app.clients.conns)) err = c.clean() assert.Equal(t, nil, err) assert.Equal(t, 0, len(app.clients.conns)) }
func TestAPIUnsubscribe(t *testing.T) { app := testApp() p, _ := app.projectByKey("test1") cmd := &unsubscribeApiCommand{ User: "******", Channel: "channel", } resp, err := app.unsubcribeCmd(p, cmd) assert.Equal(t, nil, err) assert.Equal(t, nil, resp.err) // unsubscribe from all channels cmd = &unsubscribeApiCommand{ User: "******", Channel: "", } resp, err = app.unsubcribeCmd(p, cmd) assert.Equal(t, nil, err) assert.Equal(t, nil, resp.err) }