Files
ipe/rest_test.go_
T
2015-01-02 18:20:27 -03:00

159 lines
3.7 KiB
Plaintext

// Copyright 2014 Claudemiro Alves Feitosa Neto. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func init() {
// Conf = NewConfig(":8080", "123456", "APPID", "Secret", false, false)
channel := NewChannel("presence-c1", "")
channel.addSubscriber(Subscriber{Id: 1, SocketID: "Sock1", Data: "Data1"})
channel.addSubscriber(Subscriber{Id: 2, SocketID: "Sock2", Data: "Data2"})
PresenceChannels["presence-c1"] = channel
PrivateChannels["private-c3"] = NewChannel("private-c3", "")
PublicChannels["c2"] = NewChannel("c2", "")
}
// All Channels
func Test_GetChannels_all(t *testing.T) {
r, _ := http.NewRequest("GET", "/apps/APPID/channels", nil)
w := httptest.NewRecorder()
NewRouter().ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("Must return OK: %s returned", w.Code)
}
channels := make(map[string]interface{})
json.Unmarshal(w.Body.Bytes(), &channels)
if len(channels) != 3 {
t.Error("Must return 3 channels")
}
}
// Only presence channels
func Test_GetChannels_filter_by_presence_prefix(t *testing.T) {
r, _ := http.NewRequest("GET", "/apps/APPID/channels?filter_by_prefix=presence-", nil)
w := httptest.NewRecorder()
NewRouter().ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("Must return OK: %s returned", w.Code)
}
channels := make(map[string]interface{})
json.Unmarshal(w.Body.Bytes(), &channels)
if len(channels) != 1 {
t.Error("Must return 1 channel")
}
}
// Only presence channels and user_count
func Test_GetChannels_filter_by_presence_prefix_and_user_count(t *testing.T) {
r, _ := http.NewRequest("GET", "/apps/APPID/channels?filter_by_prefix=presence-&info=user_count", nil)
w := httptest.NewRecorder()
NewRouter().ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("Must return OK: %s returned", w.Code)
}
channels := make(map[string]struct {
UserCount int `json:"user_count"`
})
json.Unmarshal(w.Body.Bytes(), &channels)
if len(channels) != 1 {
t.Error("Must return 1 channel")
}
channel, exists := channels["presence-c1"]
if !exists {
t.Error("Channel must exist.")
}
if channel.UserCount != 2 {
t.Error("Must be 2 users")
}
}
// User count only alowed in Presence channels
func Test_GetChannels_filter_by_private_prefix_and_info_user_count(t *testing.T) {
r, _ := http.NewRequest("GET", "/apps/APPID/channels?filter_by_prefix=private-&info=user_count", nil)
w := httptest.NewRecorder()
NewRouter().ServeHTTP(w, r)
if w.Code != http.StatusBadRequest {
t.Errorf("Must return BadRequest: %s returned", w.Code)
}
}
func Test_GetChannels_filter_by_public_prefix(t *testing.T) {
r, _ := http.NewRequest("GET", "/apps/APPID/channels?filter_by_prefix=public-", nil)
w := httptest.NewRecorder()
NewRouter().ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("Must return OK: %s returned", w.Code)
}
channels := make(map[string]interface{})
json.Unmarshal(w.Body.Bytes(), &channels)
if len(channels) != 1 {
t.Error("Must return 1 channel")
}
_, exists := channels["c2"]
if !exists {
t.Error("Channel must exist.")
}
}
func Test_GetChannels_filter_by_private_prefix(t *testing.T) {
r, _ := http.NewRequest("GET", "/apps/APPID/channels?filter_by_prefix=private-", nil)
w := httptest.NewRecorder()
NewRouter().ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("Must return OK: %s returned", w.Code)
}
channels := make(map[string]interface{})
json.Unmarshal(w.Body.Bytes(), &channels)
if len(channels) != 1 {
t.Error("Must return 1 channel")
}
_, exists := channels["private-c3"]
if !exists {
t.Error("Channel must exist.")
}
}