func TestAdminAuthToken(t *testing.T) {
	app := testApp()
	// first without secret set
	err := app.checkAdminAuthToken("")
	assert.Equal(t, ErrUnauthorized, err)

	// no web secret set
	token, err := app.adminAuthToken()
	if err == nil {
		println(app.config.WebSecret)
		println(token)
	}
	assert.Equal(t, ErrInternalServerError, err)

	app.Lock()
	app.config.WebSecret = "secret"
	app.Unlock()

	err = app.checkAdminAuthToken("")
	assert.Equal(t, ErrUnauthorized, err)

	token, err = app.adminAuthToken()
	assert.Equal(t, nil, err)
	assert.True(t, len(token) > 0)
	err = app.checkAdminAuthToken(token)
	assert.Equal(t, nil, 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"))
}
Exemple #3
0
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)
}
Exemple #4
0
func TestMarshal(t *testing.T) {
	SetDefault("port", 1313)
	Set("name", "Steve")

	type config struct {
		Port int
		Name string
	}

	var C config

	err := Marshal(&C)
	if err != nil {
		t.Fatalf("unable to decode into struct, %v", err)
	}

	assert.Equal(t, &C, &config{Name: "Steve", Port: 1313})

	Set("port", 1234)
	err = Marshal(&C)
	if err != nil {
		t.Fatalf("unable to decode into struct, %v", err)
	}
	assert.Equal(t, &C, &config{Name: "Steve", Port: 1234})
}
Exemple #5
0
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 TestPublish(t *testing.T) {
	// Custom config
	c := newTestConfig()

	// Set custom options for default namespace
	c.ChannelOptions.HistoryLifetime = 10
	c.ChannelOptions.HistorySize = 2
	c.ChannelOptions.HistoryDropInactive = true

	app := testMemoryAppWithConfig(&c)
	createTestClients(app, 10, 1, nil)
	data, _ := json.Marshal(map[string]string{"test": "publish"})
	err := app.Publish(Channel("channel-0"), data, ConnID(""), nil)
	assert.Nil(t, err)

	// Check publish to subscribed channels did result in saved history
	hist, err := app.History(Channel("channel-0"))
	assert.Nil(t, err)
	assert.Equal(t, 1, len(hist))

	// Publishing to a channel no one is subscribed to should be a no-op
	err = app.Publish(Channel("some-other-channel"), data, ConnID(""), nil)
	assert.Nil(t, err)

	hist, err = app.History(Channel("some-other-channel"))
	assert.Nil(t, err)
	assert.Equal(t, 0, len(hist))

}
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"))
}
Exemple #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)
}
Exemple #9
0
func TestAliasInConfigFile(t *testing.T) {
	// the config file specifies "beard".  If we make this an alias for
	// "hasbeard", we still want the old config file to work with beard.
	RegisterAlias("beard", "hasbeard")
	assert.Equal(t, true, Get("hasbeard"))
	Set("hasbeard", false)
	assert.Equal(t, false, Get("beard"))
}
Exemple #10
0
func TestAdminClient(t *testing.T) {
	c, err := newTestAdminClient()
	go c.writer()
	assert.Equal(t, nil, err)
	assert.NotEqual(t, c.uid(), "")
	err = c.send([]byte("message"))
	assert.Equal(t, nil, err)
}
Exemple #11
0
func TestClientResponse(t *testing.T) {
	resp := newClientResponse("test")
	resp.Err(clientError{errors.New("error1"), errorAdviceFix})
	resp.Err(clientError{errors.New("error2"), errorAdviceFix})
	marshalledResponse, err := json.Marshal(resp)
	assert.Equal(t, nil, err)
	assert.Equal(t, true, strings.Contains(string(marshalledResponse), "\"error\":\"error1\""))
}
func TestByteQueueSize(t *testing.T) {
	q := New()
	assert.Equal(t, 0, q.Size())
	q.Add([]byte("1"))
	q.Add([]byte("2"))
	assert.Equal(t, 2, q.Size())
	q.Remove()
	assert.Equal(t, 1, q.Size())
}
Exemple #13
0
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)
}
Exemple #14
0
func TestAPIPresence(t *testing.T) {
	app := testApp()
	cmd := &presenceAPICommand{
		Channel: "channel",
	}
	resp, err := app.presenceCmd(cmd)
	assert.Equal(t, nil, err)
	assert.Equal(t, nil, resp.err)
}
Exemple #15
0
func TestAPIHistory(t *testing.T) {
	app := testApp()
	cmd := &historyAPICommand{
		Channel: "channel",
	}
	resp, err := app.historyCmd(cmd)
	assert.Equal(t, nil, err)
	assert.Equal(t, nil, resp.err)
}
Exemple #16
0
func TestAPIDisconnect(t *testing.T) {
	app := testApp()
	cmd := &disconnectAPICommand{
		User: "******",
	}
	resp, err := app.disconnectCmd(cmd)
	assert.Equal(t, nil, err)
	assert.Equal(t, nil, resp.err)
}
Exemple #17
0
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 TestUpdateMetrics(t *testing.T) {
	app := testMemoryApp()
	createTestClients(app, 10, 1)
	data, _ := json.Marshal(map[string]string{"test": "publish"})
	err := app.Publish(Channel("channel-0"), data, ConnID(""), nil)
	assert.Equal(t, nil, err)
	app.config.NodeMetricsInterval = 1 * time.Millisecond
	app.updateMetricsOnce()
	assert.Equal(t, int64(1), app.metrics.metrics.NumMsgPublished)
}
func TestMemoryChannels(t *testing.T) {
	app := testMemoryApp()
	channels, err := app.engine.channels()
	assert.Equal(t, nil, err)
	assert.Equal(t, 0, len(channels))
	createTestClients(app, 10, 1, nil)
	channels, err = app.engine.channels()
	assert.Equal(t, nil, err)
	assert.Equal(t, 10, len(channels))
}
Exemple #20
0
func TestByteQueueSize(t *testing.T) {
	initialCapacity := 2
	q := New(initialCapacity)
	assert.Equal(t, 0, q.Size())
	q.Add([]byte("1"))
	q.Add([]byte("2"))
	assert.Equal(t, 2, q.Size())
	q.Remove()
	assert.Equal(t, 1, q.Size())
}
Exemple #21
0
func TestAPIPublish(t *testing.T) {
	app := testApp()
	cmd := &publishApiCommand{
		Channel: "channel",
		Data:    []byte("null"),
	}
	resp, err := app.publishCmd(cmd)
	assert.Equal(t, nil, err)
	assert.Equal(t, nil, resp.err)
}
func TestByteQueueResize(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([]byte(strconv.Itoa(i)))
		i++
	}
	assert.Equal(t, initialCapacity, q.Cap())
	q.Add([]byte("resize here"))
	assert.Equal(t, initialCapacity*2, q.Cap())
	q.Remove()
	// back to initial capacity
	assert.Equal(t, initialCapacity, q.Cap())

	q.Add([]byte("new resize here"))
	assert.Equal(t, initialCapacity*2, q.Cap())
	q.Add([]byte("one more item, no resize must happen"))
	assert.Equal(t, initialCapacity*2, q.Cap())

	assert.Equal(t, initialCapacity+2, q.Len())
}
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))
}
Exemple #24
0
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)
}
Exemple #25
0
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)
}
Exemple #26
0
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)
}
Exemple #27
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"))
}
Exemple #28
0
func TestAPIChannels(t *testing.T) {
	app := testApp()
	resp, err := app.channelsCmd()
	assert.Equal(t, nil, err)
	assert.Equal(t, nil, resp.err)
	app = testMemoryApp()
	createTestClients(app, 10, 1)
	resp, err = app.channelsCmd()
	assert.Equal(t, nil, err)
	body := resp.Body.(*ChannelsBody)
	assert.Equal(t, 10, len(body.Data))
}
Exemple #29
0
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([]byte("message"))
	assert.Equal(t, err, nil)
	err = h.remove(c)
	assert.Equal(t, err, nil)
	assert.Equal(t, len(h.connections), 0)
}
func TestRedisChannels(t *testing.T) {
	c := dial()
	defer c.close()
	app := testRedisApp()
	channels, err := app.engine.channels()
	assert.Equal(t, nil, err)
	assert.Equal(t, 0, len(channels))
	createTestClients(app, 10, 1)
	channels, err = app.engine.channels()
	assert.Equal(t, nil, err)
	assert.Equal(t, 10, len(channels))
}