github.com/vmware/transport-go@v1.3.4/plank/pkg/server/endpointer_handler_factory_test.go (about)

     1  package server
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/google/uuid"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/vmware/transport-go/bus"
     9  	"github.com/vmware/transport-go/model"
    10  	"github.com/vmware/transport-go/service"
    11  	"net/http"
    12  	"os"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func TestBuildEndpointHandler_Timeout(t *testing.T) {
    18  	b := bus.ResetBus()
    19  	service.ResetServiceRegistry()
    20  	msgChan := make(chan *model.Message, 1)
    21  	_ = b.GetChannelManager().CreateChannel("test-chan")
    22  	port := GetTestPort()
    23  	config := GetBasicTestServerConfig(os.TempDir(), "stdout", "stdout", "stderr", port, true)
    24  	ps := NewPlatformServer(config).(*platformServer)
    25  	ps.eventbus = b
    26  	assert.HTTPBodyContains(t, ps.buildEndpointHandler("test-chan", func(w http.ResponseWriter, r *http.Request) model.Request {
    27  		return model.Request{
    28  			Payload: nil,
    29  			Request: "test-request",
    30  		}
    31  	}, 5*time.Millisecond, msgChan), "GET", "http://localhost", nil, "request timed out")
    32  }
    33  
    34  func TestBuildEndpointHandler_ChanResponseErr(t *testing.T) {
    35  	b := bus.ResetBus()
    36  	service.ResetServiceRegistry()
    37  	msgChan := make(chan *model.Message, 1)
    38  	_ = b.GetChannelManager().CreateChannel("test-chan")
    39  	port := GetTestPort()
    40  	config := GetBasicTestServerConfig(os.TempDir(), "stdout", "stdout", "stderr", port, true)
    41  	ps := NewPlatformServer(config).(*platformServer)
    42  	ps.eventbus = b
    43  	assert.HTTPErrorf(t, ps.buildEndpointHandler("test-chan", func(w http.ResponseWriter, r *http.Request) model.Request {
    44  		uId := &uuid.UUID{}
    45  		msgChan <- &model.Message{Error: fmt.Errorf("test error")}
    46  		return model.Request{
    47  			Id:      uId,
    48  			Payload: nil,
    49  			Request: "test-request",
    50  		}
    51  	}, 5*time.Second, msgChan), "GET", "http://localhost", nil, "test error")
    52  }
    53  
    54  func TestBuildEndpointHandler_SuccessResponse(t *testing.T) {
    55  	b := bus.ResetBus()
    56  	service.ResetServiceRegistry()
    57  	msgChan := make(chan *model.Message, 1)
    58  	_ = b.GetChannelManager().CreateChannel("test-chan")
    59  	port := GetTestPort()
    60  	config := GetBasicTestServerConfig(os.TempDir(), "stdout", "stdout", "stderr", port, true)
    61  	ps := NewPlatformServer(config).(*platformServer)
    62  	ps.eventbus = b
    63  	assert.HTTPBodyContains(t, ps.buildEndpointHandler("test-chan", func(w http.ResponseWriter, r *http.Request) model.Request {
    64  		uId := &uuid.UUID{}
    65  		msgChan <- &model.Message{Payload: &model.Response{
    66  			Id:      uId,
    67  			Payload: []byte("{\"error\": false}"),
    68  		}}
    69  		return model.Request{
    70  			Id:      uId,
    71  			Payload: nil,
    72  			Request: "test-request",
    73  		}
    74  	}, 5*time.Second, msgChan), "GET", "http://localhost", nil, "{\"error\": false}")
    75  }
    76  
    77  func TestBuildEndpointHandler_ErrorResponse(t *testing.T) {
    78  	b := bus.ResetBus()
    79  	service.ResetServiceRegistry()
    80  	_ = b.GetChannelManager().CreateChannel("test-chan")
    81  	port := GetTestPort()
    82  	config := GetBasicTestServerConfig(os.TempDir(), "stdout", "stdout", "stderr", port, true)
    83  	ps := NewPlatformServer(config).(*platformServer)
    84  	ps.eventbus = b
    85  
    86  	msgChan := make(chan *model.Message, 1)
    87  	uId := &uuid.UUID{}
    88  	rsp := &model.Response{
    89  		Id:        uId,
    90  		Payload:   "{\"error\": true}",
    91  		ErrorCode: 500,
    92  		Error:     true,
    93  	}
    94  	expected, _ := json.Marshal(rsp.Payload)
    95  
    96  	assert.HTTPBodyContains(t, ps.buildEndpointHandler("test-chan", func(w http.ResponseWriter, r *http.Request) model.Request {
    97  		msgChan <- &model.Message{Payload: rsp}
    98  		return model.Request{
    99  			Id:      uId,
   100  			Payload: nil,
   101  			Request: "test-request",
   102  		}
   103  
   104  	}, 5*time.Second, msgChan), "GET", "http://localhost", nil, string(expected))
   105  }
   106  
   107  func TestBuildEndpointHandler_ErrorResponseAlternative(t *testing.T) {
   108  	b := bus.ResetBus()
   109  	service.ResetServiceRegistry()
   110  	msgChan := make(chan *model.Message, 1)
   111  	_ = b.GetChannelManager().CreateChannel("test-chan")
   112  	port := GetTestPort()
   113  	config := GetBasicTestServerConfig(os.TempDir(), "stdout", "stdout", "stderr", port, true)
   114  	ps := NewPlatformServer(config).(*platformServer)
   115  	ps.eventbus = b
   116  
   117  	uId := &uuid.UUID{}
   118  	rsp := &model.Response{
   119  		Id:        uId,
   120  		ErrorCode: 418,
   121  		Error:     true,
   122  	}
   123  
   124  	assert.HTTPBodyContains(t, ps.buildEndpointHandler("test-chan", func(w http.ResponseWriter, r *http.Request) model.Request {
   125  		msgChan <- &model.Message{Payload: rsp}
   126  		return model.Request{
   127  			Id:      uId,
   128  			Payload: nil,
   129  			Request: "test-request",
   130  		}
   131  
   132  	}, 5*time.Second, msgChan), "GET", "http://localhost", nil, "418")
   133  }
   134  
   135  func TestBuildEndpointHandler_CatchPanic(t *testing.T) {
   136  	b := bus.ResetBus()
   137  	service.ResetServiceRegistry()
   138  	msgChan := make(chan *model.Message, 1)
   139  	_ = b.GetChannelManager().CreateChannel("test-chan")
   140  	port := GetTestPort()
   141  	config := GetBasicTestServerConfig(os.TempDir(), "stdout", "stdout", "stderr", port, true)
   142  	ps := NewPlatformServer(config).(*platformServer)
   143  	ps.eventbus = b
   144  	assert.HTTPBodyContains(t, ps.buildEndpointHandler("test-chan", func(w http.ResponseWriter, r *http.Request) model.Request {
   145  		panic("peekaboo")
   146  		return model.Request{
   147  			Payload: nil,
   148  			Request: "test-request",
   149  		}
   150  	}, 5*time.Second, msgChan), "GET", "http://localhost", nil, "Internal Server Error")
   151  }