github.com/minio/console@v1.4.1/api/ws_handle_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"testing"
    23  
    24  	"github.com/minio/websocket"
    25  )
    26  
    27  // Common mocks for WSConn interface
    28  // assigning mock at runtime instead of compile time
    29  var (
    30  	connWriteMessageMock func(messageType int, data []byte) error
    31  	connReadMessageMock  func() (messageType int, p []byte, err error)
    32  )
    33  
    34  // The Conn type represents a WebSocket connection.
    35  type mockConn struct{}
    36  
    37  func (c mockConn) writeMessage(messageType int, data []byte) error {
    38  	return connWriteMessageMock(messageType, data)
    39  }
    40  
    41  func (c mockConn) readMessage() (messageType int, p []byte, err error) {
    42  	return connReadMessageMock()
    43  }
    44  
    45  func (c mockConn) close() error {
    46  	return nil
    47  }
    48  
    49  func (c mockConn) remoteAddress() string {
    50  	return "127.0.0.1"
    51  }
    52  
    53  func TestWSHandle(_ *testing.T) {
    54  	// assert := assert.New(t)
    55  	mockWSConn := mockConn{}
    56  
    57  	// mock function of conn.ReadMessage(), returns unexpected Close Error CloseAbnormalClosure
    58  	connReadMessageMock = func() (messageType int, p []byte, err error) {
    59  		return 0, []byte{}, &websocket.CloseError{Code: websocket.CloseAbnormalClosure, Text: ""}
    60  	}
    61  	parentCtx := context.Background()
    62  	ctx := wsReadClientCtx(parentCtx, mockWSConn)
    63  
    64  	<-ctx.Done()
    65  	// closed ctx correctly
    66  
    67  	// mock function of conn.ReadMessage(), returns unexpected Close Error CloseAbnormalClosure
    68  	connReadMessageMock = func() (messageType int, p []byte, err error) {
    69  		return 0, []byte{}, errors.New("error")
    70  	}
    71  	ctx2 := wsReadClientCtx(parentCtx, mockWSConn)
    72  	<-ctx2.Done()
    73  	// closed ctx correctly
    74  
    75  	// mock function of conn.ReadMessage(), returns unexpected Close Error CloseAbnormalClosure
    76  	connReadMessageMock = func() (messageType int, p []byte, err error) {
    77  		return 0, []byte{}, &websocket.CloseError{Code: websocket.CloseGoingAway, Text: ""}
    78  	}
    79  	ctx3 := wsReadClientCtx(parentCtx, mockWSConn)
    80  	<-ctx3.Done()
    81  	// closed ctx correctly
    82  }