diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-03-28 04:58:19 -0400 |
---|---|---|
committer | George Goldberg <george@gberg.me> | 2017-03-28 09:58:19 +0100 |
commit | daca0d93f621bcb1daae149c178af0631bcd120a (patch) | |
tree | 6127936c76dbc6fd20a7377385469980e5bd72e3 /api4/websocket_test.go | |
parent | ca8b8d1245026672b1a56d256bb8ff3c8bb1bba9 (diff) | |
download | chat-daca0d93f621bcb1daae149c178af0631bcd120a.tar.gz chat-daca0d93f621bcb1daae149c178af0631bcd120a.tar.bz2 chat-daca0d93f621bcb1daae149c178af0631bcd120a.zip |
Move WebSocket API to it's own package and add websocket v4 endpoint (#5881)
Diffstat (limited to 'api4/websocket_test.go')
-rw-r--r-- | api4/websocket_test.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/api4/websocket_test.go b/api4/websocket_test.go new file mode 100644 index 000000000..6018bf7da --- /dev/null +++ b/api4/websocket_test.go @@ -0,0 +1,73 @@ +// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package api4 + +import ( + "testing" + "time" + + "github.com/mattermost/platform/model" +) + +func TestWebSocket(t *testing.T) { + th := Setup().InitBasic() + defer TearDown() + WebSocketClient, err := th.CreateWebSocketClient() + if err != nil { + t.Fatal(err) + } + defer WebSocketClient.Close() + + time.Sleep(300 * time.Millisecond) + + // Test closing and reconnecting + WebSocketClient.Close() + if err := WebSocketClient.Connect(); err != nil { + t.Fatal(err) + } + + WebSocketClient.Listen() + + time.Sleep(300 * time.Millisecond) + if resp := <-WebSocketClient.ResponseChannel; resp.Status != model.STATUS_OK { + t.Fatal("should have responded OK to authentication challenge") + } + + WebSocketClient.SendMessage("ping", nil) + time.Sleep(300 * time.Millisecond) + if resp := <-WebSocketClient.ResponseChannel; resp.Data["text"].(string) != "pong" { + t.Fatal("wrong response") + } + + WebSocketClient.SendMessage("", nil) + time.Sleep(300 * time.Millisecond) + if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.web_socket_router.no_action.app_error" { + t.Fatal("should have been no action response") + } + + WebSocketClient.SendMessage("junk", nil) + time.Sleep(300 * time.Millisecond) + if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.web_socket_router.bad_action.app_error" { + t.Fatal("should have been bad action response") + } + + req := &model.WebSocketRequest{} + req.Seq = 0 + req.Action = "ping" + WebSocketClient.Conn.WriteJSON(req) + time.Sleep(300 * time.Millisecond) + if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.web_socket_router.bad_seq.app_error" { + t.Fatal("should have been bad action response") + } + + WebSocketClient.UserTyping("", "") + time.Sleep(300 * time.Millisecond) + if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.websocket_handler.invalid_param.app_error" { + t.Fatal("should have been invalid param response") + } else { + if resp.Error.DetailedError != "" { + t.Fatal("detailed error not cleared") + } + } +} |