github.com/secure-build/gitlab-runner@v12.5.0+incompatible/session/proxy/proxy_test.go (about)

     1  package proxy
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestPoolInitializer(t *testing.T) {
    10  	assert.Equal(t, Pool{}, NewPool())
    11  }
    12  
    13  func TestProxySettings(t *testing.T) {
    14  	settings := &Settings{
    15  		ServiceName: "serviceName",
    16  		Ports: []Port{
    17  			{
    18  				Number: 80,
    19  				Name:   "port-80",
    20  			},
    21  			{
    22  				Number: 81,
    23  				Name:   "port-81",
    24  			},
    25  		},
    26  	}
    27  
    28  	assert.Equal(t, settings, NewProxySettings(settings.ServiceName, settings.Ports))
    29  }
    30  
    31  func TestPortByNameOrNumber(t *testing.T) {
    32  	port1 := Port{
    33  		Number: 80,
    34  		Name:   "port-80",
    35  	}
    36  
    37  	port2 := Port{
    38  		Number: 81,
    39  		Name:   "port-81",
    40  	}
    41  
    42  	settings := Settings{
    43  		ServiceName: "ServiceName",
    44  		Ports:       []Port{port1, port2},
    45  	}
    46  
    47  	tests := map[string]struct {
    48  		port          string
    49  		expectedPort  Port
    50  		expectedError bool
    51  	}{
    52  		"Port number does not exist": {
    53  			port:          "8080",
    54  			expectedError: true,
    55  		},
    56  		"Port name does not exist": {
    57  			port:          "Foo",
    58  			expectedError: true,
    59  		},
    60  		"Port number exists": {
    61  			port:         "80",
    62  			expectedPort: port1,
    63  		},
    64  		"Port name exists": {
    65  			port:         "port-81",
    66  			expectedPort: port2,
    67  		},
    68  	}
    69  
    70  	for name, test := range tests {
    71  		t.Run(name, func(t *testing.T) {
    72  			result, err := settings.PortByNameOrNumber(test.port)
    73  			if test.expectedError {
    74  				assert.Error(t, err)
    75  				return
    76  			}
    77  
    78  			assert.NoError(t, err)
    79  			assert.Equal(t, test.expectedPort, result)
    80  		})
    81  	}
    82  }
    83  
    84  func TestScheme(t *testing.T) {
    85  	tests := map[string]struct {
    86  		protocol         string
    87  		expectedProtocol string
    88  		expectedError    bool
    89  	}{
    90  		"Port protocol is HTTP": {
    91  			protocol:         "http",
    92  			expectedProtocol: "http",
    93  		},
    94  		"Port protocol is HTTPS": {
    95  			protocol:         "https",
    96  			expectedProtocol: "https",
    97  		},
    98  		"Port protocol does not exist": {
    99  			protocol:      "foo",
   100  			expectedError: true,
   101  		},
   102  		"Port protocol is empty": {
   103  			protocol:      "",
   104  			expectedError: true,
   105  		},
   106  	}
   107  
   108  	for name, test := range tests {
   109  		t.Run(name, func(t *testing.T) {
   110  			port := Port{Protocol: test.protocol}
   111  			scheme, err := port.Scheme()
   112  
   113  			if test.expectedError {
   114  				assert.Error(t, err)
   115  				return
   116  			}
   117  
   118  			assert.NoError(t, err)
   119  			assert.Equal(t, test.expectedProtocol, scheme)
   120  		})
   121  	}
   122  }
   123  
   124  func TestWebsocketProtocolFor(t *testing.T) {
   125  	tests := map[string]struct {
   126  		protocol           string
   127  		expectedWSProtocol string
   128  	}{
   129  		"Protocol is HTTPS": {
   130  			protocol:           "https",
   131  			expectedWSProtocol: "wss",
   132  		},
   133  		"Protocol is HTTP": {
   134  			protocol:           "http",
   135  			expectedWSProtocol: "ws",
   136  		},
   137  	}
   138  
   139  	for name, test := range tests {
   140  		t.Run(name, func(t *testing.T) {
   141  			assert.Equal(t, test.expectedWSProtocol, WebsocketProtocolFor(test.protocol))
   142  		})
   143  	}
   144  }