From a7c58135012b38b4d23bd3d737d0bbee1caef797 Mon Sep 17 00:00:00 2001 From: claudemiro Date: Sun, 6 Mar 2016 17:53:51 -0300 Subject: [PATCH] Testing http handlers --- ipe/connection.go | 5 ++ ipe/handlers_test.go | 207 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 ipe/handlers_test.go diff --git a/ipe/connection.go b/ipe/connection.go index 01a03b0..14876a0 100644 --- a/ipe/connection.go +++ b/ipe/connection.go @@ -28,6 +28,11 @@ func newConnection(socketID string, s *websocket.Conn) *connection { // Publish the message to websocket atached to this client func (conn *connection) Publish(m interface{}) { go func() { + if conn.Socket == nil { + log.Info("Socket is nil. Maybe you are testing this app?") + return + } + if err := conn.Socket.WriteJSON(m); err != nil { log.Errorf("Error publishing message to connection %+v, %s", conn, err) } diff --git a/ipe/handlers_test.go b/ipe/handlers_test.go new file mode 100644 index 0000000..8f3af12 --- /dev/null +++ b/ipe/handlers_test.go @@ -0,0 +1,207 @@ +package ipe + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +var ( + testApp *app + ctx *applicationContext +) + +func init() { + testApp = newTestApp() + + channel := newChannel("presence-c1") + testApp.AddChannel(channel) + testApp.AddChannel(newChannel("c2")) + testApp.AddChannel(newChannel("private-c3")) + + conn := newConnection("123.456", nil) + testApp.Subscribe(channel, conn, "{}") + + conn = newConnection("321.654", nil) + testApp.Subscribe(channel, conn, "{}") + + db := newMemdb() + db.AddApp(testApp) + + ctx = &applicationContext{DB: db} +} + +// All Channels +func Test_getChannels_all(t *testing.T) { + + appID := testApp.AppID + + p := map[string]string{} + p["app_id"] = appID + + r, _ := http.NewRequest("GET", fmt.Sprintf("/apps/%s/channels", appID), nil) + w := httptest.NewRecorder() + + getChannels(ctx, params(p), w, r) + + if w.Code != http.StatusOK { + t.Errorf("w.Code == %d, wants %d", w.Code, http.StatusOK) + } + + data := make(map[string]interface{}) + json.Unmarshal(w.Body.Bytes(), &data) + + channels := data["channels"].(map[string]interface{}) + + if len(channels) != 3 { + t.Errorf("len(%q) == %d, want %d", channels, len(channels), 3) + } +} + +func Test_getChannels_filter_by_presence_prefix(t *testing.T) { + appID := testApp.AppID + + p := map[string]string{} + p["app_id"] = appID + + r, _ := http.NewRequest("GET", fmt.Sprintf("/apps/%s/channels?filter_by_prefix=presence-", appID), nil) + w := httptest.NewRecorder() + + getChannels(ctx, params(p), w, r) + + if w.Code != http.StatusOK { + t.Errorf("w.Code == %d, wants %d", w.Code, http.StatusOK) + } + + data := make(map[string]interface{}) + json.Unmarshal(w.Body.Bytes(), &data) + + channels := data["channels"].(map[string]interface{}) + + if len(channels) != 1 { + t.Errorf("len(%q) == %d, want %d", channels, len(channels), 1) + } +} + +// Only presence channels and user_count +func Test_getChannels_filter_by_presence_prefix_and_user_count(t *testing.T) { + + appID := testApp.AppID + + p := map[string]string{} + p["app_id"] = appID + + r, _ := http.NewRequest("GET", fmt.Sprintf("/apps/%s/channels?filter_by_prefix=presence-&info=user_count", appID), nil) + w := httptest.NewRecorder() + + getChannels(ctx, params(p), w, r) + + if w.Code != http.StatusOK { + t.Errorf("w.Code == %d, wants %d", w.Code, http.StatusOK) + } + + data := make(map[string]interface{}) + json.Unmarshal(w.Body.Bytes(), &data) + + channels := data["channels"].(map[string]interface{}) + + if len(channels) != 1 { + t.Errorf("len(%q) == %d, want %d", channels, len(channels), 1) + } + + c, exists := channels["presence-c1"] + + if !exists { + t.Errorf("!exists == %t, want %t", !exists, false) + } + + _channel := c.(map[string]interface{}) + + if _channel["user_count"] != float64(1) { + t.Errorf("_channel['user_count'] == %f, want %d", _channel["user_count"], 1) + } +} + +// User count only alowed in Presence channels +func Test_getChannels_filter_by_private_prefix_and_info_user_count(t *testing.T) { + appID := testApp.AppID + + p := map[string]string{} + p["app_id"] = appID + + r, _ := http.NewRequest("GET", fmt.Sprintf("/apps/%s/channels?filter_by_prefix=private-&info=user_count", appID), nil) + w := httptest.NewRecorder() + + getChannels(ctx, params(p), w, r) + + if w.Code != http.StatusBadRequest { + t.Errorf("w.Code == %d, wants %d", w.Code, http.StatusBadRequest) + } +} + +func Test_getChannels_filter_by_public_prefix(t *testing.T) { + appID := testApp.AppID + + p := map[string]string{} + p["app_id"] = appID + + r, _ := http.NewRequest("GET", fmt.Sprintf("/apps/%s/channels?filter_by_prefix=public-", appID), nil) + w := httptest.NewRecorder() + + getChannels(ctx, params(p), w, r) + + if w.Code != http.StatusOK { + t.Errorf("w.Code == %d, wants %d", w.Code, http.StatusOK) + } + + data := make(map[string]interface{}) + + json.Unmarshal(w.Body.Bytes(), &data) + + channels := data["channels"].(map[string]interface{}) + + if len(channels) != 1 { + t.Errorf("len(%q) == %d, want %d", channels, len(channels), 1) + } + + _, exists := channels["c2"] + + if !exists { + t.Errorf("!exists == %t, want %t", !exists, false) + } +} + +func Test_getChannels_filter_by_private_prefix(t *testing.T) { + + appID := testApp.AppID + + p := map[string]string{} + p["app_id"] = appID + + r, _ := http.NewRequest("GET", fmt.Sprintf("/apps/%s/channels?filter_by_prefix=private-", appID), nil) + w := httptest.NewRecorder() + + getChannels(ctx, params(p), w, r) + + if w.Code != http.StatusOK { + t.Errorf("w.Code == %d, wants %d", w.Code, http.StatusOK) + } + + data := make(map[string]interface{}) + + json.Unmarshal(w.Body.Bytes(), &data) + + channels := data["channels"].(map[string]interface{}) + + if len(channels) != 1 { + t.Errorf("len(%q) == %d, want %d", channels, len(channels), 1) + } + + _, exists := channels["private-c3"] + + if !exists { + t.Errorf("!exists == %t, want %t", !exists, false) + } +}