github.com/keys-pub/mattermost-server@v4.10.10+incompatible/api4/websocket_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  )
    12  
    13  func TestWebSocket(t *testing.T) {
    14  	th := Setup().InitBasic()
    15  	defer th.TearDown()
    16  	WebSocketClient, err := th.CreateWebSocketClient()
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	defer WebSocketClient.Close()
    21  
    22  	time.Sleep(300 * time.Millisecond)
    23  
    24  	// Test closing and reconnecting
    25  	WebSocketClient.Close()
    26  	if err := WebSocketClient.Connect(); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	WebSocketClient.Listen()
    31  
    32  	time.Sleep(300 * time.Millisecond)
    33  	if resp := <-WebSocketClient.ResponseChannel; resp.Status != model.STATUS_OK {
    34  		t.Fatal("should have responded OK to authentication challenge")
    35  	}
    36  
    37  	WebSocketClient.SendMessage("ping", nil)
    38  	time.Sleep(300 * time.Millisecond)
    39  	if resp := <-WebSocketClient.ResponseChannel; resp.Data["text"].(string) != "pong" {
    40  		t.Fatal("wrong response")
    41  	}
    42  
    43  	WebSocketClient.SendMessage("", nil)
    44  	time.Sleep(300 * time.Millisecond)
    45  	if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.web_socket_router.no_action.app_error" {
    46  		t.Fatal("should have been no action response")
    47  	}
    48  
    49  	WebSocketClient.SendMessage("junk", nil)
    50  	time.Sleep(300 * time.Millisecond)
    51  	if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.web_socket_router.bad_action.app_error" {
    52  		t.Fatal("should have been bad action response")
    53  	}
    54  
    55  	req := &model.WebSocketRequest{}
    56  	req.Seq = 0
    57  	req.Action = "ping"
    58  	WebSocketClient.Conn.WriteJSON(req)
    59  	time.Sleep(300 * time.Millisecond)
    60  	if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.web_socket_router.bad_seq.app_error" {
    61  		t.Fatal("should have been bad action response")
    62  	}
    63  
    64  	WebSocketClient.UserTyping("", "")
    65  	time.Sleep(300 * time.Millisecond)
    66  	if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.websocket_handler.invalid_param.app_error" {
    67  		t.Fatal("should have been invalid param response")
    68  	} else {
    69  		if resp.Error.DetailedError != "" {
    70  			t.Fatal("detailed error not cleared")
    71  		}
    72  	}
    73  }